Skip to content

Commit

Permalink
Merge branch 'master' into bug_func_ctrlflow_leak
Browse files Browse the repository at this point in the history
  • Loading branch information
vEpiphyte committed Jan 6, 2025
2 parents 0f48d66 + c926109 commit 40a9851
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
6 changes: 6 additions & 0 deletions changes/3ef24cd8510986bf45e39e390b383077.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
desc: Added ``indent`` kwarg to ``$lib.json.save()`` to indent serialized json with a number of spaces or a specified string.
prs:
- 4052
type: feat
...
7 changes: 5 additions & 2 deletions synapse/lib/stormlib/json.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ class JsonLib(s_stormtypes.Lib):
'type': {'type': 'function', '_funcname': '_jsonSave',
'args': (
{'name': 'item', 'type': 'any', 'desc': 'The item to be serialized as a JSON string.', },
{'name': 'indent', 'type': 'int', 'desc': 'Specify a number of spaces to indent with.', 'default': None},
),
'returns': {'type': 'str', 'desc': 'The JSON serialized object.', }}},
{'name': 'schema', 'desc': 'Get a JS schema validation object.',
Expand All @@ -115,10 +116,12 @@ def getObjLocals(self):
}

@s_stormtypes.stormfunc(readonly=True)
async def _jsonSave(self, item):
async def _jsonSave(self, item, indent=None):
indent = await s_stormtypes.toint(indent, noneok=True)

try:
item = await s_stormtypes.toprim(item)
return json.dumps(item)
return json.dumps(item, indent=indent)
except Exception as e:
mesg = f'Argument is not JSON compatible: {item}'
raise s_exc.MustBeJsonSafe(mesg=mesg)
Expand Down
20 changes: 20 additions & 0 deletions synapse/tests/test_lib_stormlib_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,26 @@ async def test_stormlib_json(self):

self.eq(((1, 2, 3)), await core.callStorm('return($lib.json.load("[1, 2, 3]"))'))
self.eq(('["foo", "bar", "baz"]'), await core.callStorm('return($lib.json.save((foo, bar, baz)))'))
self.eq(('{"foo": 1, "bar": {"baz": "hello"}}'), await core.callStorm('return($lib.json.save(({"foo": 1, "bar": {"baz": "hello"}})))'))
self.eq(('{"foo": 1, "bar": {"baz": "hello"}}'), await core.callStorm('return($lib.json.save(({"foo": 1, "bar": {"baz": "hello"}}), (null)))'))
self.eq((
'''{
"foo": 1,
"bar": {
"baz": "hello"
}
}'''), await core.callStorm('return($lib.json.save(({"foo": 1, "bar": {"baz": "hello"}}), indent=(4)))'))

self.eq((
'''{
"foo": 1,
"bar": {
"baz": "hello"
}
}'''), await core.callStorm('return($lib.json.save(({"foo": 1, "bar": {"baz": "hello"}}), indent=2))'))

with self.raises(s_exc.BadCast):
await core.callStorm('return($lib.json.save(({"foo": 1, "bar": {"baz": "hello"}}), indent=x))')

with self.raises(s_exc.BadJsonText):
await core.callStorm('return($lib.json.load(foo))')
Expand Down

0 comments on commit 40a9851

Please sign in to comment.