Skip to content

Instantly share code, notes, and snippets.

@SebDeclercq
Created November 27, 2020 14:05
Show Gist options
  • Save SebDeclercq/33d45012a0ddd1e99bb5e009bcf5307e to your computer and use it in GitHub Desktop.
Save SebDeclercq/33d45012a0ddd1e99bb5e009bcf5307e to your computer and use it in GitHub Desktop.
class JSONConverter:
@classmethod
def json_dumps(cls, obj) -> str:
return str(cls._json_dumps(obj))
@classmethod
def _json_dumps(cls, obj):
if isinstance(obj, list):
return [cls._json_dumps(el) for el in obj]
elif isinstance(obj, dict):
return {
cls._json_dumps(key): cls._json_dumps(value)
for key, value in obj.items()
}
else:
if isinstance(obj, bool):
return str(obj).lower()
elif obj is None:
return 'null'
else:
return str(obj)
json_dumps = JSONConverter.json_dumps
print(json_dumps([1, [1, 2], {1: None}, [1, {2: True}]]))
assert json_dumps([1, [1, 2], {1: None}, [1, {2: True}]]) == "['1', ['1', '2'], {'1': 'null'}, ['1', {'2': 'true'}]]"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment