Jsons is a Python library designed for seamlessly serializing complex Python objects (including dataclasses, attrs, and POPOs) to JSON (dicts or strings) and deserializing them back. It aims for minimal effort, requiring no modifications to your objects, and is highly customizable and extendable. The current version is 1.6.3, and it maintains an active release cadence with several minor updates per year addressing features and bug fixes.
pip install jsonsVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to define a dataclass and use `jsons.dump()` to serialize an instance into a dictionary and `jsons.load()` to deserialize it back. It also includes an example with nested objects and lists, showcasing `jsons`'s ability to handle complex type hints.
Use `import jsons` for object-oriented serialization and `import json` for raw JSON string/file manipulation. `jsons` handles conversion of `datetime` objects by default.
If you require microseconds to be stripped, you may need to configure a custom serializer for `datetime` objects or preprocess your data before serialization.
Upgrade to `jsons` v1.6.3 or newer. If upgrading is not possible, ensure that string fields are explicitly typed or validate deserialized objects to catch unexpected `datetime` instances.
Upgrade to `jsons` v1.6.1 or newer to ensure correct `IntEnum` serialization with `use_enum_name=True` and proper type hint resolution for named tuples.
Register a custom deserializer for the problematic type using `jsons.set_deserializer`. For example, for a custom `MyClass`, you would define `jsons.set_deserializer(my_deserializer_function, MyClass)`.
While `jsons` attempts to handle this gracefully, for explicit control, ensure `Optional[Type]` (e.g., `Optional[int]`) from the `typing` module is used for optional fields. You can also suppress this warning using `jsons.suppress_warnings(True)` or prevent `None` attributes from being dumped with `jsons.dump(obj, strip_nones=True)`.
Always use `jsons.dump()` for serializing custom Python objects with the `jsons` library. If a particular custom class still causes this error even with `jsons.dump()`, register a custom serializer for that type using `jsons.set_serializer(my_serializer_function, CustomClass)`.
No dependency data recorded yet.