For serializing Python objects to JSON (dicts) and back
pip install jsons- Turn Python objects into dicts or (json)strings and back
- No changes required to your objects
- Easily customizable and extendable
- Works with dataclasses, attrs and POPOs
💗 this lib? Leave a ★ and tell your colleagues!
Example of a model to serialize:
>>> @dataclass
... class Person:
... name: str
... birthday: datetime
...
>>> p = Person('Guido van Rossum', birthday_guido)
Example of using jsons to serialize:
>>> out = jsons.dump(p)
>>> out
{'birthday': '1956-01-31T12:00:00Z', 'name': 'Guido van Rossum'}
Example of using jsons to deserialize:
>>> p2 = jsons.load(out, Person)
>>> p2
Person(name='Guido van Rossum', birthday=datetime.datetime(1956, 1, 31, 12, 0, tzinfo=datetime.timezone.utc))
Installation
pip install jsons
Usage
import jsons
some_instance = jsons.load(some_dict, SomeClass) # Deserialization
some_dict = jsons.dump(some_instance) # Serialization
In some cases, you have instances that contain other instances that need (de)serialization, for instance with lists or dicts. You can use the
typing classes for this as is demonstrated below.
