Easily deserialize and serialize complex objects to json.
Project description
json-coder: Quickly serialize and deserialize python to json objects
The default library python json
module supports custom decoders and encoders
allowing for saving and restoring custom objects into json. In comparison to
pickling objects, this is more portable and easier to manually edit.
This library supports the following use-cases:
-
Class with json loading and dumping functions (define
.json
property or.to_json()
method for dumping, define.from_json()
constructor or proper init for reading) -
python
dataclass
classes -
custom loading and dumping functions for classes defined elsewhere
Fundamentally classes are assigned an identifier and json objects of the following structure are created:
{
'__<identifier>__': <object data>
}
Usage examples
Custom class using predefined schema:
import json
import json_coder
@json.jsonify("testclass")
class TestClass:
def __init__(self, a: int):
self.a = a
def to_json(self):
return {"a": self.a}
# Usage
a = json.loads('{"__testclass__": {"a": 10}}')
print(a)
# <__main__.TestClass object at 0x10eb26810>
print(json.dumps(a))
# {"__testclass__": {"a": 10}}
Dataclasses do not require any additional methods to work properly.
from dataclasses import dataclass
import json
import json_coder
@json_coder.jsonify("testdataclass")
@dataclass
class TestDataClass:
a: int
a = json.loads('{"__testdataclass__": {"a": 10}}')
print(type(a)) # should now be an object of testdataclass
print(json.dumps(a))
Custom dumper and reading functions can also be added to existing classes by registering manually:
import datetime
import json
import json_coder
json_coder.register("datetime", datetime.datetime, datetime.datetime.fromisoformat, datetime.datetime.isoformat)
d = json.loads('{"__datetime__": "2018-10-10"}')
print(d)
print(json.dumps(d))
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file json-coder-0.5.1.tar.gz
.
File metadata
- Download URL: json-coder-0.5.1.tar.gz
- Upload date:
- Size: 4.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/53.0.0 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.7.9
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7fc981df33dd2d546bb30f68ee45fa283dc35f4e3b89127bba79b69a49d42d17 |
|
MD5 | 5a7e36cf3a1d78b641ffec89ea6febe1 |
|
BLAKE2b-256 | a8d777cb8c5a7c3dfdda8884f0d8baa6ae93a36e4e4e56e22eb36ed760bdca11 |
File details
Details for the file json_coder-0.5.1-py3-none-any.whl
.
File metadata
- Download URL: json_coder-0.5.1-py3-none-any.whl
- Upload date:
- Size: 4.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/53.0.0 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.7.9
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 40fb54400ed6eb8496ec171ec86bfd4ccd873e75520ea8694d582dff6405be5d |
|
MD5 | b0eea5fe453f9db6637aa0a27015efc7 |
|
BLAKE2b-256 | 7f3d88c6a4ae27923e048785acd260a49a3644263749030fe35b5d575c8d8e55 |