Skip to main content

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))

Metadata

Release files for json-coder 0.5.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for json-coder 0.5.1
File Size Uploaded Provenance Actions
json-coder-0.5.1.tar.gz 4.6 kB Without provenance Details for json-coder-0.5.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for json-coder 0.5.1
File Interpreter ABI Platform Actions
json_coder-0.5.1-py3-none-any.whl Python 3 none any Details for json_coder-0.5.1-py3-none-any.whl

Total release size: 8.7 kB

Release files / json-coder-0.5.1.tar.gz

Download URL json-coder-0.5.1.tar.gz
Size 4.6 kB
Tags Source
SHA-256 checksum
How to use checksums
7fc981df33dd2d546bb30f68ee45fa283dc35f4e3b89127bba79b69a49d42d17
BLAKE2b-256 checksum
How to use checksums
a8d777cb8c5a7c3dfdda8884f0d8baa6ae93a36e4e4e56e22eb36ed760bdca11
Upload date
Uploaded using Trusted Publishing?
What is 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

Release files / json_coder-0.5.1-py3-none-any.whl

Download URL json_coder-0.5.1-py3-none-any.whl
Size 4.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
40fb54400ed6eb8496ec171ec86bfd4ccd873e75520ea8694d582dff6405be5d
BLAKE2b-256 checksum
How to use checksums
7f3d88c6a4ae27923e048785acd260a49a3644263749030fe35b5d575c8d8e55
Upload date
Uploaded using Trusted Publishing?
What is 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

Security for json-coder 0.5.1

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.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page