Skip to main content

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.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

json_dt_serializer-1.1.0.tar.gz (3.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

json_dt_serializer-1.1.0-py3-none-any.whl (5.4 kB view details)

Uploaded Python 3

File details

Details for the file json_dt_serializer-1.1.0.tar.gz.

File metadata

  • Download URL: json_dt_serializer-1.1.0.tar.gz
  • Upload date:
  • Size: 3.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.0 CPython/3.12.4

File hashes

Hashes for json_dt_serializer-1.1.0.tar.gz
Algorithm Hash digest
SHA256 46a1125981948ba08a8d63fab56ef19a77cb663f1be19bd14ee4c1979a53565f
MD5 26280b8ff4f03456239a0236d7ff7c30
BLAKE2b-256 1d6b057e9898a7f9bb4e610889331166b73939706d02b728b22ce026b96f18c4

See more details on using hashes here.

File details

Details for the file json_dt_serializer-1.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for json_dt_serializer-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3c772f7d810d62a7a28242e22410c863e4ea6aa5a0348bd69d53ddc82cfb4ab1
MD5 dc5c933ac808f255f5b3ddbd14a9dfe1
BLAKE2b-256 815fb454ed1009c6b79041ae8d1e005956abe3935e190c9fe6defd2513f02b40

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

1.1.0 This release

2 files

1.0.1

2 files

1.0.0

2 files

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page