dtjson: json, but with datetime and timespan support
dtjson builds on regular Json, but adds support for timespan and datetime objects in Python. It does this by looking for "__type__": "datetime" or "__type__": "timedelta" in the keys, with isoformat' and seconds` respectively.
Example Usage::
>>> import dtjson
>>> import datetime
>>> serialized = dtjson.dumps(['foo', {'bar': ('baz', None, 1.0, 2), 'spam': datetime.datetime.now()}])
>>> print(serialized)
'["foo", {"bar": ["baz", null, 1.0, 2], "spam": {"__type__": "datetime", "isoformat": "2024-05-25T14:23:36.769090"}}]'
>>> unserialized = dtjson.loads(serialized)
>>> print(unserialized)
['foo', {'bar': ['baz', None, 1.0, 2], 'spam': datetime.datetime(2024, 5, 25, 14, 23, 36, 769090)}]
Timespans are also similarly support. The interface for using dtjson is nearly identical to the json module, and can be generally used as a replacement.
Installation
NOTE: PyPi rejected dtjson as a package name, so this is packaged as json-dt-serializer.
pip install json-dt-serializer
Usage details
Usage is nearly identical to the json module, with most arguments being passed directly to it.
dtjson.dump
def dump(
obj,
fp,
skipkeys=False,
ensure_ascii=True,
check_circular=True,
allow_nan=True,
cls=None,
indent=None,
separators=None,
default=None,
sort_keys=False,
**kw
)
Serializes obj as a JSON formatted stream to fp (a .write()-supporting file-like object).
Arguments:
- obj: The Python object to serialize.
- fp: File-like object supporting .write() where the JSON data will be written.
- skipkeys: If True, dict keys that are not basic types will be skipped.
- ensure_ascii: If True, non-ASCII characters are escaped in JSON strings.
- check_circular: If False, skips circular reference check.
- allow_nan: If False, out of range float values will raise a ValueError.
- cls: Custom JSONEncoder subclass.
- indent: If non-negative integer, pretty-prints with that indent level.
- separators: Tuple (item_separator, key_separator) to specify item and key separators.
- default: Function that returns a serializable version of obj or raises TypeError.
- sort_keys: If True, dictionary keys are sorted.
- **kw: Additional keyword arguments.
dtjson.dumps
def dumps(
obj,
skipkeys=False,
ensure_ascii=True,
check_circular=True,
allow_nan=True,
cls=None,
indent=None,
separators=None,
default=None,
sort_keys=False,
**kw
)
Serializes obj to a JSON formatted string.
Arguments:
- obj: The Python object to serialize.
- skipkeys: If True, dict keys that are not basic types will be skipped.
- ensure_ascii: If True, non-ASCII characters are escaped in JSON strings.
- check_circular: If False, skips circular reference check.
- allow_nan: If False, out of range float values will raise a ValueError.
- cls: Custom JSONEncoder subclass.
- indent: If non-negative integer, pretty-prints with that indent level.
- separators: Tuple (item_separator, key_separator) to specify item and key separators.
- default: Function that returns a serializable version of obj or raises TypeError.
- sort_keys: If True, dictionary keys are sorted.
- **kw: Additional keyword arguments.
dtjson.load
def load(
fp,
cls=None,
object_hook=None,
parse_float=None,
parse_int=None,
parse_constant=None,
object_pairs_hook=None,
**kw
)
Deserializes fp (a .read()-supporting file-like object containing a JSON document) to a Python object.
Arguments:
- fp: File-like object supporting .read() containing the JSON document.
- cls: Custom JSONDecoder subclass.
- object_hook: Function called with the result of any object literal decode.
- parse_float: Function to parse JSON float values.
- parse_int: Function to parse JSON int values.
- parse_constant: Function called with strings like -Infinity, Infinity, NaN.
- object_pairs_hook: Function called with an ordered list of pairs for object literal decode.
- **kw: Additional keyword arguments.
dtjson.loads
def loads(
s,
cls=None,
object_hook=None,
parse_float=None,
parse_int=None,
parse_constant=None,
object_pairs_hook=None,
**kw
)
Deserializes s (a str, bytes or bytearray instance containing a JSON document) to a Python object.
Arguments:
- s: A string, bytes, or bytearray instance containing the JSON document.
- cls: Custom JSONDecoder subclass.
- object_hook: Function called with the result of any object literal decode.
- parse_float: Function to parse JSON float values.
- parse_int: Function to parse JSON int values.
- parse_constant: Function called with strings like -Infinity, Infinity, NaN.
- object_pairs_hook: Function called with an ordered list of pairs for object literal decode.
- **kw: Additional keyword arguments.
Metadata
Release files for json-dt-serializer 1.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| json_dt_serializer-1.1.0.tar.gz | 3.9 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| json_dt_serializer-1.1.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 9.3 kB
Release files / json_dt_serializer-1.1.0.tar.gz
| Download URL | json_dt_serializer-1.1.0.tar.gz |
|---|---|
| Size | 3.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
46a1125981948ba08a8d63fab56ef19a77cb663f1be19bd14ee4c1979a53565f
|
|
BLAKE2b-256 checksum How to use checksums |
1d6b057e9898a7f9bb4e610889331166b73939706d02b728b22ce026b96f18c4
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/5.1.0 CPython/3.12.4
|
Release files / json_dt_serializer-1.1.0-py3-none-any.whl
| Download URL | json_dt_serializer-1.1.0-py3-none-any.whl |
|---|---|
| Size | 5.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
3c772f7d810d62a7a28242e22410c863e4ea6aa5a0348bd69d53ddc82cfb4ab1
|
|
BLAKE2b-256 checksum How to use checksums |
815fb454ed1009c6b79041ae8d1e005956abe3935e190c9fe6defd2513f02b40
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/5.1.0 CPython/3.12.4
|