jsonabler
Python package for making your classes easy encodable to JSON string and vice-versa.
Getting started
Requirements
- Python >= 3.8
Installation
pip install jsonabler
Usage
Making a Jsonable
Make your class extends the Jsonable interface and implements get_jsonable_data and from_jsonable_data methods with the encoding/decoding logic.
from jsonabler import Jsonable, jsonabled
@jsonabled
class Foo(Jsonable):
def __init__(self, bar: str):
self.__bar = bar
def get_jsonable_data(self) -> dict:
return {
'bar': self.__bar,
}
@classmethod
def from_jsonable_data(cls, data: dict) -> Jsonable:
return cls(data['bar'])
Registering a Jsonable
For decoding your Jsonable classes, you need to register it.
Decorating your classes with the @jsonabled decorator
from jsonabler import Jsonable, jsonabled
@jsonabled
class Foo(Jsonable):
...
Calling register_jsonables method passing class types
from jsonabler import Jsonable, register_jsonables
class Foo(Jsonable):
...
if __name__ == '__main__':
register_jsonables({Foo})
Encoding a Jsonable
Call dumps method passing a Jsonable object.
from jsonabler import dumps
def upload_foo(foo: Foo) -> None:
json_string = dumps(foo)
# transmit your JSON string
...
Encoded Foo
[
'Foo',
{
'bar': "abc",
}
]
Decoding a Jsonable
Call loads method passing the JSON string.
from jsonabler import loads, JsonableDecodeError, JsonableNotRegisteredError
from json import JSONDecodeError
def download_foo() -> Foo:
# receive JSON string encoded Foo object
...
try:
return loads(json_string)
# not a valid encoded JSON string
except JSONDecodeError:
...
# the Jsonable type of the encoded object was not registered
except JsonableNotRegisteredError:
...
# something went wrong while decoding the object
except JsonableDecodeError:
...
License
Distributed under the MIT License. See LICENSE file for more information.
Metadata
Release files for jsonabler 0.2.2
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 | |
|---|---|---|---|---|
| jsonabler-0.2.2.tar.gz | 4.3 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| jsonabler-0.2.2-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 9.0 kB
Release files / jsonabler-0.2.2.tar.gz
| Download URL | jsonabler-0.2.2.tar.gz |
|---|---|
| Size | 4.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
1bd92833c41a52e73ba4668bf43e8fcffebc3d59902d02921ea86a8c1be1156f
|
|
BLAKE2b-256 checksum How to use checksums |
acd70e1dc02a58f9732943030294b36ff28dd312f1de32bed8bb147d7e352262
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.0 CPython/3.9.12
|
Release files / jsonabler-0.2.2-py3-none-any.whl
| Download URL | jsonabler-0.2.2-py3-none-any.whl |
|---|---|
| Size | 4.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
df90b1c41b75996b843db8bddad34d79d21cf8717fa4288ceee0d55bccbf90d2
|
|
BLAKE2b-256 checksum How to use checksums |
9eff704569fbad617d10589dbdd4cd776da779f3805e2e514e833ad700b36c67
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.0 CPython/3.9.12
|
Security for jsonabler 0.2.2
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.