py-json-serialize
Serialize in JSON format
Features:
- Simple api: @json_serialize, to_json(), from_json();
- Version support: version-based data migration;
Install
pip install py-json-serialize
Test
in the project's root folder, run pytest:
pytest
Dependencies
None
API
-
class decortator @json_serialize
- format 1: no parameter
from py_json_serialize import json_serialize @json_serialize class A(object):pass
- format 2: with parameter
@json_serialize(clsid = "", [version=0*|n])
- clsid: the unique string to identify the class. the class-id will be the name of decorated if not specified.
- version: optional parameter to specify the version of serialized data format. the default value is 0 if not specified.
from py_json_serialize import json_serialize # old version @json_serialize("app-config", version=1) class AppConfigV1(object): servers = [] # new version @json_serialize("app-config", version=2) class AppConfigV2(AppConfigV1): timeout = 600
-
to_json()/from_json()
The decorated class will have two new functions:
-
to_json(): convert class instance to json string
@json_serialize class Hello(object): def __init__(self, who = "World"): self.who = who a = Hello() print(a.to_json())
outputs:
{ "_clsid_": "Hello", "who": "World" }
-
from_json(str|dict): reads json string or dictionary object to return an class object.
It is actually a staticmethod that can be called to return an object of any type deduced from the class-id in the data string. so don't be surprised that you might get an object of different type if the input json data string is serialized from another class.
So the from_json() is simply a handy helper method to make your code more readable if your app only handles one type of data.
-
-
function json_decode(jstr: str|dict)-> object: convert json string or dictionary to python object
This is a function to decode the serialized json string, its typical usage is as following:
class Task(object):pass @staticmethod def from_json(jstr): return json_decode(jstr) @json_serialize class CopyFile(Task):pass @json_serialize class UploadFile(Task):pass task1 = Task.from_json({ CLASS_ID:'CopyFile' }) assert isinstance(task1, CopyFile) task2 = Task.from_json({ CLASS_ID:'UploadFile' }) assert isinstance(task2, UploadFile)
-
function json_encode : convert python object to json string
def json_encode(obj, pretty = True, encode_all_fields = False)
When pretty is True, the fields are sorted by their name and the json string is intented properly for human reading, otherwise the json string can save some storage space and more efficient for machine processing.
If encode_all_fields is true, then all class fields are serialized, otherwise the internal fields (field name starts with '_') are ignored.
-
CLASS_ID: the unique key indicating which class is serialized.
It is published in case a json string or dictionary object needs to be prepared manually as input to json_decode()/from_json().
The internal constant value of CLASS_ID is "_CLSID_"
json_decode({ CLASS_ID: "Hello", who: "World" }) json_decode('{ "%s": "Hello", "who": "World" }' % CLASS_ID )
Metadata
Release files for py-json-serialize 0.10.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| py-json-serialize-0.10.0.tar.gz | 5.3 kB | Without provenance | Details for py-json-serialize-0.10.0.tar.gz |
Release files / py-json-serialize-0.10.0.tar.gz
| Download URL | py-json-serialize-0.10.0.tar.gz |
|---|---|
| Size | 5.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
ff769eeb4066063cf224ccb291b6875bc1100bba1981f71a8b61ad4f192d44de
|
|
BLAKE2b-256 checksum How to use checksums |
36acbf482b91a0085bf1a9ce831b04b260d10235713896875870746358eeeecd
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/3.8.2
|
Security for py-json-serialize 0.10.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.