Skip to content

Commit 0031c59

Browse files
dwapstranottyo
authored andcommitted
New keyword 'Convert string to json' and some fixes (#13)
* Python 3 fixes and cleanup * Added convert string to json keyword * Fix setup version lookup * Unittest for convert string to json
1 parent ce3b834 commit 0031c59

File tree

4 files changed

+34
-7
lines changed

4 files changed

+34
-7
lines changed

JSONLibrary/JSONLibraryKeywords.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
# -*- coding: utf-8 -*-
2+
import json
3+
import os.path
24
from robot.api import logger
35
from robot.api.deco import keyword
4-
from version import VERSION
5-
import os.path
6-
import json
76
from jsonpath_rw import Index, Fields
87
from jsonpath_rw_ext import parse
8+
from .version import VERSION
99

1010
__author__ = 'Traitanit Huangsri'
1111
__email__ = '[email protected]'
@@ -72,7 +72,7 @@ def get_value_from_json(self, json_object, json_path):
7272
Return array of values
7373
7474
Examples:
75-
| ${values}= | Get Value From Jsonpath | ${json} | $..phone_number |
75+
| ${values}= | Get Value From Json | ${json} | $..phone_number |
7676
"""
7777
json_path_expr = parse(json_path)
7878
return [match.value for match in json_path_expr.find(json_object)]
@@ -136,3 +136,17 @@ def convert_json_to_string(self, json_object):
136136
"""
137137
return json.dumps(json_object)
138138

139+
@keyword('Convert String to JSON')
140+
def convert_string_to_json(self, json_string):
141+
"""Convert String to JSON object
142+
143+
Arguments:
144+
- json_string: JSON string
145+
146+
Return new json_object
147+
148+
Examples:
149+
| ${json_object}= | Convert String to JSON | ${json_string} |
150+
"""
151+
return json.loads(json_string)
152+

JSONLibrary/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# -*- coding: utf-8 -*-
2-
from JSONLibraryKeywords import JSONLibraryKeywords
3-
from version import VERSION
2+
from .JSONLibraryKeywords import JSONLibraryKeywords
3+
from .version import VERSION
44

55
__author__ = 'Traitanit Huangsri'
66
__email__ = '[email protected]'

setup.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,17 @@
11
#!/usr/bin/env python
22
# -*- coding: utf-8 -*-
33

4+
import re
45
from setuptools import setup
5-
from JSONLibrary.version import VERSION
6+
7+
# Read version from file without loading the module
8+
with open('JSONLibrary/version.py', 'r') as version_file:
9+
version_match = re.search(r"^VERSION ?= ?['\"]([^'\"]*)['\"]",
10+
version_file.read(), re.M)
11+
if version_match:
12+
VERSION=version_match.group(1)
13+
else:
14+
VERSION='0.1' #
615

716
requirements = [
817
'tox',

tests/test_JSONLibrary.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,5 +67,9 @@ def test_convert_json_to_string(self):
6767
json_str = self.test.convert_json_to_string(self.json)
6868
self.assertTrue(isinstance(json_str, str))
6969

70+
def test_convert_string_to_json(self):
71+
json_obj = self.test.convert_string_to_json('{"firstName": "John"}')
72+
self.assertTrue("firstName" in json_obj)
73+
7074
def tearDown(self):
7175
pass

0 commit comments

Comments
 (0)