Skip to content

Commit bec3d87

Browse files
author
Vinit Kumar
authored
Merge pull request #23 from vinitkumar/feature/add-conversation-from-string-support
fix crash in test and build
2 parents dfdeadc + 536ab60 commit bec3d87

File tree

5 files changed

+40
-6
lines changed

5 files changed

+40
-6
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
.idea
12
*.pyc
23
json2xml.egg-info
34
build

readme.md

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,17 @@ pip3 install json2xml
1313

1414
### Usage
1515

16-
#### Command Line
16+
### Command Line
1717

1818
```
1919
python -m src.cli --file="examples/example.json"
2020
python -m src.cli --url="https://coderwall.com/vinitcool76.json"
21+
python -m src.cli --data '{"login":"mojombo","id":1,"avatar_url":"https://avatars0.githubusercontent.com/u/1?v=4"}'
2122
```
2223

23-
#### Inline in Code
24+
### Inline in Code
2425

25-
- From a file
26+
#### From a file
2627

2728
```python
2829
from src.json2xml import Json2xml
@@ -31,7 +32,7 @@ data_object = Json2xml(data)
3132
data_object.json2xml() #xml output
3233
```
3334

34-
- From an URL
35+
#### From an URL
3536

3637
```python
3738
from src.json2xml import Json2xml
@@ -40,6 +41,14 @@ data_object = Json2xml(data)
4041
data_object.json2xml() #xml output
4142
```
4243

44+
#### From JSON string
45+
46+
```python
47+
from src.json2xml import Json2xml
48+
data = Json2xml.fromstring('{"login":"mojombo","id":1,"avatar_url":"https://avatars0.githubusercontent.com/u/1?v=4"}').data
49+
data_object = Json2xml(data)
50+
data_object.json2xml() #xml output
51+
```
4352

4453
### Bugs, features
4554

setup.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
from setuptools import setup, find_packages
22

3-
version = '1.2.5'
3+
version = '1.3.0'
44

55
setup(
66
name='json2xml',
77
version=version,
8-
description='A simple python package to convert json to xml data',
8+
description='A simple python package to convert json from file, URL or string to xml data',
99
author='Vinit Kumar',
1010
author_email='[email protected]',
1111
url='https://github.com/vinitkumar/json2xml',

src/cli.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ def main(argv=None):
77
parser = argparse.ArgumentParser(description='Utility to convert json to valid xml.')
88
parser.add_argument('--url', dest='url', action='store')
99
parser.add_argument('--file', dest='file', action='store')
10+
parser.add_argument('--data', dest='data', action='store')
1011
args = parser.parse_args()
1112

1213
if args.url:
@@ -19,6 +20,11 @@ def main(argv=None):
1920
data = Json2xml.fromjsonfile(file)
2021
print(Json2xml.json2xml(data))
2122

23+
if args.data:
24+
str_data = args.data
25+
data = Json2xml.fromstring(str_data)
26+
print(Json2xml.json2xml(data))
27+
2228

2329
if __name__ == "__main__":
2430
main(sys.argv)

src/json2xml.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import requests
55
import simplejson
66
import dict2xml
7+
import json
78
from bs4 import BeautifulSoup
89

910

@@ -52,6 +53,18 @@ def fromurl(cls, url: str):
5253
else:
5354
raise Exception("Bad URl, Can't get JSON response")
5455

56+
@classmethod
57+
def fromstring(cls, data: str):
58+
if type(data) is not str:
59+
raise("Sorry but it doesn't seem to be valid string")
60+
try:
61+
data = json.loads(data)
62+
except Exception as e:
63+
print("Sorry, failed to load json, seems the JSON is not right")
64+
data = []
65+
return cls(data)
66+
67+
5568
# -------------------------------
5669
##
5770
# @Synopsis This method actually
@@ -71,6 +84,7 @@ def main(argv=None):
7184
parser = argparse.ArgumentParser(description='Utility to convert json to valid xml.')
7285
parser.add_argument('--url', dest='url', action='store')
7386
parser.add_argument('--file', dest='file', action='store')
87+
parser.add_argument('--data', dest='data', action='store')
7488
args = parser.parse_args()
7589

7690
if args.url:
@@ -83,6 +97,10 @@ def main(argv=None):
8397
data = Json2xml.fromjsonfile(file)
8498
print(Json2xml.json2xml(data))
8599

100+
if args.data:
101+
str_data = args.data
102+
data = Json2xml.fromstring(str_data)
103+
print(Json2xml.json2xml(data))
86104

87105
if __name__ == "__main__":
88106
main(sys.argv)

0 commit comments

Comments
 (0)