ZnJSON
Package to Encode/Decode some common file formats to json
Available via pip install znjson
In comparison to pickle this allows having readable json files combined with some
serialized data.
Example
import numpy as np
import json
import znjson
data = json.dumps(
obj={"data_np": np.arange(2), "data": [x for x in range(10)]},
cls=znjson.ZnEncoder,
indent=4
)
_ = json.loads(data, cls=znjson.ZnDecoder)
The resulting *.json file is partially readable and looks like this:
{
"data_np": {
"_type": "np.ndarray_small",
"value": [
0,
1
]
},
"data": [
0,
1,
2,
3,
4
]
}
Custom Converter
ZnJSON allows you to easily add custom converters.
Let's write a serializer for datetime.datetime.
from znjson import ConverterBase
from datetime import datetime
class DatetimeConverter(ConverterBase):
"""Encode/Decode datetime objects
Attributes
----------
level: int
Priority of this converter over others.
A higher level will be used first, if there
are multiple converters available
representation: str
An unique identifier for this converter.
instance:
Used to select the correct converter.
This should fulfill isinstance(other, self.instance)
or __eq__ should be overwritten.
"""
level = 100
representation = "datetime"
instance = datetime
def encode(self, obj: datetime) -> str:
"""Convert the datetime object to str / isoformat"""
return obj.isoformat()
def decode(self, value: str) -> datetime:
"""Create datetime object from str / isoformat"""
return datetime.fromisoformat(value)
This allows us to use this new serializer:
znjson.config.register(DatetimeConverter) # we need to register the new converter first
json_string = json.dumps(dt, cls=znjson.ZnEncoder, indent=4)
json.loads(json_string, cls=znjson.ZnDecoder)
and will result in
{
"_type": "datetime",
"value": "2022-03-11T09:47:35.280331"
}
If you don't want to register your converter to be used everywhere, simply use:
json_string = json.dumps(dt, cls=znjson.ZnEncoder.from_converters(DatetimeConverter))
Metadata
Release files for znjson 0.2.6
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 |
|---|---|---|---|---|
| znjson-0.2.6.tar.gz | 12.5 kB | Without provenance | Details for znjson-0.2.6.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| znjson-0.2.6-py3-none-any.whl | Python 3 | none | any | Details for znjson-0.2.6-py3-none-any.whl |
Total release size: 27.8 kB
Release files / znjson-0.2.6.tar.gz
| Download URL | znjson-0.2.6.tar.gz |
|---|---|
| Size | 12.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
b9a5360bc598018ea82972c595b953edc6cc0b3f860fbaff46034266461372ea
|
|
BLAKE2b-256 checksum How to use checksums |
e2d66041b375b2e5e789bf99ff4b2f4f179f1328fe8221e209c87376461b8676
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.8.3 CPython/3.12.7 Linux/6.5.0-1025-azure
|
Release files / znjson-0.2.6-py3-none-any.whl
| Download URL | znjson-0.2.6-py3-none-any.whl |
|---|---|
| Size | 15.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
144cc842161cabf6bc97ec8908dd99b2ef37faa4ecc4083d1ad816654a5527e0
|
|
BLAKE2b-256 checksum How to use checksums |
35ed4874212e62e00dac96e84e68fe8465a3e50e512e8e2984c0a874298522da
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.8.3 CPython/3.12.7 Linux/6.5.0-1025-azure
|
Security for znjson 0.2.6
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.