json_dunder
Encdoe objects with a __json__ dunder method and decode objects with a __from_json__ dunder method.
import json_dunder
# Use tells json to use this class
coder = json_dunder.JsonDunderTypeCoder().use()
@coder.register
class A:
def __init__(self, x):
self.x = x
def __eq__(self, other):
if isinstance(other, self.__class__):
return self.x == other.x
return False
def __json__(self):
return {"x": self.x}
@classmethod
def __from_json__(cls, d: dict):
return cls(**d)
@coder.register
class B:
def __init__(self, a, y):
self.a = a
self.y = y
def __eq__(self, other):
if isinstance(other, self.__class__):
return self.a == other.a and self.y == other.y
return False
def __json__(self):
return {"a": self.a, "y": self.y}
@classmethod
def __from_json__(cls, d: dict):
return cls(**d)
a = A(1)
b = B(a, 2)
sa = json_dunder.dumps(a)
assert sa == '{"x": 1, "__json_type__": "__main__.A"}'
sb = json_dunder.dumps(b)
assert sb == '{"a": {"x": 1, "__json_type__": "__main__.A"}, "y": 2, "__json_type__": "__main__.B"}'
obj = json_dunder.loads(sa)
assert obj == a
obj = json_dunder.loads(sb)
assert obj.a == a
assert obj == b
Install
pip install json_dunder
Metadata
Release files for json-dunder 0.0.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 |
|---|---|---|---|---|
| json_dunder-0.0.0.tar.gz | 4.7 kB | Without provenance | Details for json_dunder-0.0.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| json_dunder-0.0.0-py3-none-any.whl | Python 3 | none | any | Details for json_dunder-0.0.0-py3-none-any.whl |
Total release size: 9.0 kB
Release files / json_dunder-0.0.0.tar.gz
| Download URL | json_dunder-0.0.0.tar.gz |
|---|---|
| Size | 4.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
7987cee400ff90497392b1bd5b0534f35248abf6ee4a33ef570abf80a40522c1
|
|
BLAKE2b-256 checksum How to use checksums |
f999d7329304881edb22a5f51344cd1d00c3c7c9fcb278e671e181ed08b3bd4a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.11.3
|
Release files / json_dunder-0.0.0-py3-none-any.whl
| Download URL | json_dunder-0.0.0-py3-none-any.whl |
|---|---|
| Size | 4.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
2171d4e64ace82f2ea67ed71c9dc48b6c064eac6b5e202365b055a142d9f0184
|
|
BLAKE2b-256 checksum How to use checksums |
5bf720e065e33a038b278deb5d5beb378dc47c8e02fdd77862f9486efb20d82a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.11.3
|
Security for json-dunder 0.0.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.