Skip to content

Commit 9a87c23

Browse files
SubhenduShekharGupta
and
Gupta
authored
Issue 49 (#54)
* python in progress * runtime vars refined * closes #49 --------- Co-authored-by: Gupta <[email protected]>
1 parent 3e2d3ad commit 9a87c23

File tree

6 files changed

+97
-8
lines changed

6 files changed

+97
-8
lines changed

python/cjson/src/cjson.py

+67-5
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,6 @@ def __init__(self, file_path: str):
2626
self.__obj = None
2727
self.__file_path = file_path
2828
self.__content = read(self.__file_path)
29-
self.__decode_keywords()
30-
'''Call this object to unlock native JSON functions
31-
'''
32-
self.json = Json(self.__obj)
33-
self.__decode_relative_paths(self.__content)
3429

3530
def __decode_keywords(self):
3631
is_changed: bool = False
@@ -85,6 +80,23 @@ def __decode_relative_paths(self, content: str):
8580
self.__refine_obj(content=content)
8681

8782
def deserialize(self):
83+
''' Deserializes CJSON context and returns dictionary object in JSON context.
84+
85+
Please Note, if any key is detected for runtime variable injections is
86+
replaced with `<-tag->`. No error/warning is thrown.
87+
88+
To inject runtime variables, use `inject` function
89+
'''
90+
self.__decode_keywords()
91+
92+
runtime_keys: list[str] = re.findall(Keywords.runtime_vals_regex, self.__content)
93+
'''Call this object to unlock native JSON functions
94+
'''
95+
self.__content = self.__refine_runtime_vals(self.__content, runtime_keys=runtime_keys)
96+
self.json = Json(self.__obj)
97+
98+
self.__decode_relative_paths(self.__content)
99+
88100
''' Returns the JSON compiled object for the given `CJSON` file. '''
89101
return self.__obj
90102

@@ -102,3 +114,53 @@ def __decode_single_line_comment(self, line_item: str):
102114
for i in range(0, len(line_split)):
103115
if line_split[i].strip() != "" and line_split[i].strip().startswith(Keywords.single_line_comment):
104116
self.__content = self.__content.replace(line_split[i], "")
117+
118+
def __refine_runtime_vals(self, content: str, runtime_keys: list[str]):
119+
unique_keys: list[str] = []
120+
121+
for each_runtime_keys in runtime_keys:
122+
if(each_runtime_keys not in unique_keys):
123+
unique_keys.append(each_runtime_keys)
124+
125+
for each_runtime_keys in unique_keys:
126+
content = content.replace(each_runtime_keys, "\"<-" + each_runtime_keys.split("<")[1].split(">")[0] + "->\"")
127+
128+
return content
129+
130+
def inject(self, injecting_obj: dict):
131+
self.__decode_keywords()
132+
133+
runtime_keys: list[str] = re.findall(Keywords.runtime_vals_regex, self.__content)
134+
135+
self.__content = self.__refine_runtime_vals(content=self.__content, runtime_keys=runtime_keys)
136+
'''Call this object to unlock native JSON functions
137+
'''
138+
self.json = Json(self.__obj)
139+
self.__decode_relative_paths(self.__content)
140+
141+
for each_key in injecting_obj.keys():
142+
if type(injecting_obj[each_key]) != str:
143+
self.__content = self.__content.replace("\"<-" + each_key + "->\"", json.dumps(injecting_obj[each_key]))
144+
else:
145+
self.__content = self.__content.replace("<-" + each_key + "->", str(injecting_obj[each_key]))
146+
147+
self.__refine_obj(self.__content)
148+
149+
return self.__obj
150+
151+
152+
# cjson = Cjson(r"C:\Users\632400\Desktop\projects\cjson\tests\test-files\VariableInjection.cjson")
153+
# injec_data = {
154+
# "fruit": "apple",
155+
# "quantity": 1,
156+
# "jsonTypeData": {
157+
# "secondaryData": {
158+
# "type": "fruit",
159+
# "seeds": "yes"
160+
# }
161+
# }
162+
# }
163+
164+
# data = cjson.inject(injecting_obj=injec_data)
165+
166+
# print(data)

python/cjson/src/utils/_json.py

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from utils import file
2+
from utils.keywords import Keywords
23
import json
34
from typing import Union
45
from types import NoneType

python/cjson/src/utils/keywords.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ class Keywords:
44
multiLine_comment_start: str = "/*"
55
multiLine_comment_end: str = "*/"
66
relative_jpath: str = "$."
7-
relative_jpath_regex: str = "[$][.][.A-Za-z0-9]*"
7+
relative_jpath_regex: str = "[$][.][.A-Za-z0-9]*"
8+
runtime_vals_regex: str = "[<][A-Za-z0-9]*[>]"

python/setup.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55

66
setuptools.setup(
77
name="codedjson",
8-
version="1.1.1",
9-
description="Coded JSON files. Save your files with .cjson extension to unlock these features",
8+
version="1.2.0",
9+
description="CJSON is a data file format(inspired from JSON), but supports logical expressions too. Having extended language support to NodeJS, Python and Java, users has experienced data reusability. For features and examples, please refer to this documentation as base document.",
1010
long_description=__description,
1111
long_description_content_type="text/markdown",
1212
author="Shubhendu Shekhar Gupta",

tests/python/cjson_tests.py

+24
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ def get_file_path(file_name: str):
1111
json_file_path = get_file_path("source.json")
1212
pure_json_file_path = get_file_path("pure.json")
1313
relative_target_cjson = get_file_path("targetRelativeCalls.cjson")
14+
variable_injection = get_file_path("VariableInjection.cjson")
1415

1516
class Result(Enum):
1617
Pass = "Pass"
@@ -61,6 +62,27 @@ def test4(self):
6162
self.assertEqual(digit_array_import[i], cjson.json.parse("target.digitArrayImport")[i])
6263

6364
add_resut(Result.Pass, "I should be able to deserialize relative path to local variable")
65+
66+
def test5(self):
67+
cjson = Cjson(variable_injection)
68+
69+
injec_data = {
70+
"fruit": "apple",
71+
"quantity": 1,
72+
"jsonTypeData": {
73+
"secondaryData": {
74+
"type": "fruit",
75+
"seeds": "yes"
76+
}
77+
}
78+
}
79+
80+
data = cjson.inject(injecting_obj=injec_data)
81+
82+
self.assertEqual(cjson.json.parse("target.fruit"), injec_data["fruit"])
83+
self.assertEqual(cjson.json.parse("target.quantity"), injec_data["quantity"])
84+
self.assertEqual(cjson.json.parse("jsonInjection.secondaryData.type"), injec_data["jsonTypeData"]["secondaryData"]["type"])
85+
6486

6587
class JSONTests(unittest.TestCase):
6688
def test1(self):
@@ -70,12 +92,14 @@ def test1(self):
7092

7193
def test2(self):
7294
cjson = Cjson(cjson_file_path)
95+
cjson.deserialize()
7396
value = cjson.json.parse("source.quiz.sport.q1.question")
7497
self.assertEqual(value, "Which one is correct team name in NBA?")
7598
add_resut(Result.Pass, "I should be able to parse jpath using `obj< Cjson >.json.parse(\"Valid.JPATH\")`")
7699

77100
def test3(self):
78101
cjson = Cjson(cjson_file_path)
102+
cjson.deserialize()
79103
value = cjson.json.parse()
80104
self.assertEqual(value, cjson.deserialize())
81105

tests/test-files/VariableInjection.cjson

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"target": {
33
"types": "asd",
44
"fruit": <fruit>,
5+
"quantity": <quantity>,
56
"size": "Large",
67
"fruitType": $.target.fruit,
78
"color": "Red",

0 commit comments

Comments
 (0)