Make any type JSON-serializable
Project description
Make any type JSON-serializable.
A Python module which makes json.dumps work with several builtin and stdlib types. A hook for registering any custom type is also provided.
Installing
Simply install alljson with pip or add it to your project dependencies:
pip install alljson
Supported types
After installing, the following types are JSON-serializable:
- generators
- set and frozenset
- dict item/key/value iterators and views
- datetime.date and datetime.datetime (as strings in ISO format)
- uuid.UUID
- decimal.Decimal (serialized as string in order to preserve precision)
- reversed results
In addition to these, the following Python 3 types are supported:
- map, filter, range iterators
- enum.Enum
- pathlib.Path
- types.MappingProxyType
- classes implementing Sequence and Mapping abc interfaces
Registering custom types
In order to register a new type, use alljson.register_encoder(type, encoder_function). encoder_function should take object of given type as the only parameter and return a simple JSON-serializable Python value (such as dict or str).
For example:
import arrow import alljson alljson.register_encoder(arrow.Arrow, arrow.Arrow.isoformat)
Acknowledgements
- pth trick was stolen from delightful future-fstrings project
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for alljson-0.1-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1634ce0a3eb8a196f9840c8fc073a87890df68b31a43567cd0fcb3840924707f |
|
MD5 | 3a1f924ff396f2693cc467b9fb729111 |
|
BLAKE2-256 | c657c8b059c4a9acc3c5bee1b50ede00ea6155ec39940883031d1ca959782a99 |