JSON Decoder and Encoder with type information.
Project description
StrongJson
A more faithful python json encoder/decoder.
Install
pip install strong_json
or directly from this repository
pip install git+git://github.com/piti118/strong-json.git
Features
In addition to the standard json.dumps/loads, this module offer the following additonal behavior.
-
Simple interface to allow class to dump to json.
-
class User(ToJsonable): def __init__(self, first, last): self.first = first self.last = last
-
-
Preserve type information.
User('f', 'l')
->{'__type__': 'User', 'first':'f', 'last':'l'}
-
More faithful dictionary dumps/loads
- Treat dictionary as OrderedDictionary when encode. See Python 3.6 Release Note.
{'a':'b', 'c':'d'}
->{ '__type__':'dict' '__data__':[ {'key': 'a', 'value': 'b'}, {'key': 'c', 'value': 'd'}, ] }
- Decoder will accept both traditional form(
{'a':'b','c':'d'}
) and the form above.
- Allow any hashable object as key
{User('f', 'l'): 1, User('a','b'):2}
->{ '__type__': 'dict' '__data__': [ { 'key': {'__type__': 'User', 'first': 'f', 'last':'l'}, 'value: 1 }, { 'key': {'__type__': 'User', 'first': 'a', 'last':'b'}, 'value: 2 } ] }
- Treat dictionary as OrderedDictionary when encode. See Python 3.6 Release Note.
-
Distinguish tuple from List
[1,2,3]
->[1,2,3]
(1,2,3)
->{'__type__':'tuple', '__data__':[1,2,3]}
-
Custom class decoder whitelist via class_map
-
from strong_json import StrongJson s = {'__type__': 'User', 'first':'f', 'last':'l'} class_map = {'User', User} custom_json = StrongJson(class_map=class_map) custom_json.from_json(s)
- By default, strong json pass all the argument by name to the constructor.
- You could also override
StrongJson
or implement interfaceFromJsonable
for custom decoder. - You could also use strong_json.ClassMapBuilder to save some typing.
-
-
Support for date and datetime.
datetime.date(2019,8,23)
->
{ '__type__': 'date', 'year': 2019, 'month': 8, 'day': 23 }
-
Support for Enum.
-
->class Color(Enum): RED='redd' BLUE='blueee' strong_json.to_json(Color.RED)
{'__type__': 'Color' '__data__':'RED'}
-
Basic Usage
From Object to JSON
Builtin Object
from strong_json import strong_json
obj = {'a': [1,2,3], 'b':[2,3,4]}
s = strong_json.to_json(obj)
# if you want indentation you could do
s_indent = strong_json.to_json(obj, indent=2)
Custom Class
from strong_json import strong_json, ToJsonable
class User(ToJsonable):
def __init__(self, first, last):
self.first = first
self.last = last
obj = {'a': User('a', 'aa'), 'b':User('b', 'bb')}
s = strong_json.to_json(object)
From JSON to object
Builtin Object
from strong_json import strong_json
s = """{'a': 'b', 'c':'d'}"""
obj = strong_json.from_json(s)
Custom Class
from strong_json import StrongJson
class User: # it doesn't have to be ToJsonable
def __init__(self, first, last):
self.first = first
self.last = last
s = """
{
'__type__': 'User',
'first': 'hello',
'last': 'world'
}
"""
class_map = {'User': User}
custom_json = StrongJson(class_map=class_map)
obj = custom_json.to_json(s, class_map)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
strong_json-1.0.3.tar.gz
(10.3 kB
view details)
Built Distribution
File details
Details for the file strong_json-1.0.3.tar.gz
.
File metadata
- Download URL: strong_json-1.0.3.tar.gz
- Upload date:
- Size: 10.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.7.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 977b2d2867b06a2a6273b86149f36d87c47e9f3761b3c9f6e8dbacb818ee7b9c |
|
MD5 | c32ca91fdb615637d3315dcd0774f58f |
|
BLAKE2b-256 | 14894a17ca2c94bbae7ba135c1ef0432d4aa6699e7121357372853bf0a6b60b0 |
File details
Details for the file strong_json-1.0.3-py3-none-any.whl
.
File metadata
- Download URL: strong_json-1.0.3-py3-none-any.whl
- Upload date:
- Size: 6.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.7.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e6e46cae7c99771b142c2e3086cd487bedc0cfbc239685b45b91aa4dd1bc5159 |
|
MD5 | c32a4dbde6bc657aad8bd266b91e13c8 |
|
BLAKE2b-256 | 9f26d8524b0ca9bea67f6fad4cc49d9254ac0e3d0278fcf2ea4497453a4239d8 |