Skip to content

Commit

Permalink
[test] add real unit tests, polish jsonpath
Browse files Browse the repository at this point in the history
  • Loading branch information
fangq committed Apr 1, 2024
1 parent 2647fc9 commit 57cb6cb
Show file tree
Hide file tree
Showing 4 changed files with 340 additions and 45 deletions.
2 changes: 2 additions & 0 deletions jdata/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
save,
loadurl,
show,
dumpb,
loadt,
savet,
loadts,
Expand All @@ -57,6 +58,7 @@
"save",
"loadurl",
"show",
"dumpb",
"loadt",
"savet",
"loadts",
Expand Down
18 changes: 18 additions & 0 deletions jdata/jfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"save",
"loadurl",
"show",
"dumpb",
"loadt",
"savet",
"loadts",
Expand Down Expand Up @@ -236,6 +237,23 @@ def show(data, opt={}, **kwargs):
print(str)


def dumpb(data, opt={}, **kwargs):
"""@brief Printing native python data in binary JSON stream
@param[in] data: data to be saved
@param[in] opt: options, if opt['encode']=True or 1 (default), call jdata.encode() before printing
"""

try:
import bjdata
except ImportError:
raise ImportError(
'To read/write binary JData files, you must install the bjdata module by "pip install bjdata"'
)
else:
return bjdata.dumpb(data)


##====================================================================================
## Loading and saving binary JData (i.e. UBJSON) files
##====================================================================================
Expand Down
20 changes: 14 additions & 6 deletions jdata/jpath.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,15 @@ def getonelevel(input_data, paths, pathid, opt):

if arrayrange["start"] is not None:
if arrayrange["start"] < 0:
arrayrange["start"] = len(input_data) + arrayrange["start"]
arrayrange["start"] = len(input_data) + arrayrange["start"] + 1
else:
arrayrange["start"] += 1
else:
arrayrange["start"] = 1

if arrayrange["end"] is not None:
if arrayrange["end"] < 0:
arrayrange["end"] = len(input_data) + arrayrange["end"]
arrayrange["end"] = len(input_data) + arrayrange["end"] + 1
else:
arrayrange["end"] += 1
else:
Expand All @@ -97,7 +97,7 @@ def getonelevel(input_data, paths, pathid, opt):
firstidx += 1
arrayrange["start"] = arrayrange["end"] = firstidx
elif re.match(r"^\*$", arraystr):
pass
arrayrange = {"start": 1, "end": len(input_data)}

if (
"arrayrange" in locals()
Expand All @@ -117,7 +117,13 @@ def getonelevel(input_data, paths, pathid, opt):
item, paths[:pathid] + [searchkey], pathid, opt
)
if isfound:
newobj.extend(val)
if isinstance(val, list):
if len(val) > 1:
newobj.extend(val)
else:
newobj.append(val)
else:
newobj.append(val)
if newobj:
obj = newobj
if isinstance(obj, list) and len(obj) == 1:
Expand All @@ -130,7 +136,6 @@ def getonelevel(input_data, paths, pathid, opt):
if stpath in input_data:
obj = [input_data[stpath]]

deepscan = False
if obj is None or deepscan:
items = input_data.keys()

Expand All @@ -141,7 +146,10 @@ def getonelevel(input_data, paths, pathid, opt):
if isfound:
obj = obj or []
if isinstance(val, list):
obj.extend(val)
if len(val) > 1:
obj.extend(val)
else:
obj.append(val)
else:
obj.append(val)

Expand Down
Loading

0 comments on commit 57cb6cb

Please sign in to comment.