Skip to main content

Custom Datetime JSON Serializer

Project description

CDJS (Custom Datetime JSON Serializer)

cdjs is an extension for orjson to serialize datetime (and other types) in a fast custom way.

By default orjson serializes datetime according to RFC 3339 format which sometimes may not suit. orjson provides a mean to process datetime using custom serializer (via OPT_PASSTHROUGH_DATETIME flag and via default=custom_datetime_serializer). Serializers implemented in Python are usually not fast enough, that's the reason behind implementation of the custom datetime serializer written in Rust to gain optimal speed.

Aside from datetimes, cdjs is an attempt to port bson.json_util.default() and to use with orjson as a default handler.

At the moment serialization of the following types are supported:

  • datetime
  • bson.ObjectId

Example

import datetime
import hashlib

from bson import ObjectId
from cdjs import serialize
import orjson

mydata = {
    '_id': ObjectId(hashlib.md5(b'test').hexdigest()[:24]),
    'date': datetime.datetime(2021, 1, 1, hour=0, minute=4, second=36, microsecond=123000)
}
orjson.dumps(mydata, option=orjson.OPT_PASSTHROUGH_DATETIME, default=serialize)

# b'{"_id":{"$oid":"098f6bcd4621d373cade4e83"},"date":{"$date":"2021-01-01T00:04:36.123Z"}}'

Benchmarks

To run benchmark:

pip install -r bench-requirements.txt
python tests/benchmark.py --help
python tests/benchmark.py --scenario utc_dates

UTC Dates Only Benchmark

Installation

pip install cdjs

Building

To make a develop build

NOTE: Develop build doesn't enable optimizations, hence the result may work slow.

python ./setup.py develop

To make a release build

Pre-requisites

# switch to nightly channel
RUSTUP_USE_CURL=1 rustup default nightly-2021-01-31
pip install maturin

To compile, package and publish to PyPI

maturin build --no-sdist --release --strip --manylinux off
maturin publish

Testing

To run tests

python -m unittest -v tests.test_serialization

Python/OS Version Support

  • Python 3.6 (tested)
  • Python 3.7+ (not tested)
  • Linux (with GLib 2.17+)

Project details


Download files

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

Source Distribution

cdjs-0.1.5.tar.gz (41.7 kB view details)

Uploaded Source

Built Distributions

cdjs-0.1.5-cp38-cp38-manylinux2010_x86_64.whl (146.2 kB view details)

Uploaded CPython 3.8 manylinux: glibc 2.12+ x86-64

cdjs-0.1.5-cp36-cp36m-manylinux2010_x86_64.whl (146.1 kB view details)

Uploaded CPython 3.6m manylinux: glibc 2.12+ x86-64

File details

Details for the file cdjs-0.1.5.tar.gz.

File metadata

  • Download URL: cdjs-0.1.5.tar.gz
  • Upload date:
  • Size: 41.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: maturin/0.9.4

File hashes

Hashes for cdjs-0.1.5.tar.gz
Algorithm Hash digest
SHA256 13804f563120aac2eb2dd05eae6f6eeff72b481142cd55f210dc3796e8211eba
MD5 0bb3c168a82800b904a353e6a262fe7d
BLAKE2b-256 01089be7c897d0773d208736c7e8f0811216d885d8025358d22241d47e2eccfe

See more details on using hashes here.

File details

Details for the file cdjs-0.1.5-cp38-cp38-manylinux2010_x86_64.whl.

File metadata

File hashes

Hashes for cdjs-0.1.5-cp38-cp38-manylinux2010_x86_64.whl
Algorithm Hash digest
SHA256 d0d39ab3350b090628a69a3161cdcdef0a3fe6a17d59e4b196eb90f5527441ed
MD5 fbc926c43d4a1a1d611682c3a0495f6a
BLAKE2b-256 9994952c73ff8018825e596a37aa8532936f5fc6cb58c6989275b4d716215a4d

See more details on using hashes here.

File details

Details for the file cdjs-0.1.5-cp36-cp36m-manylinux2010_x86_64.whl.

File metadata

File hashes

Hashes for cdjs-0.1.5-cp36-cp36m-manylinux2010_x86_64.whl
Algorithm Hash digest
SHA256 4830b07e258d21a0b0473b500b8c08dd01b23194614bfe2b9584a0ff71523bd9
MD5 dd1167007825a399535e3e6c26a202a9
BLAKE2b-256 cf8d44485c9073343aa1f3de9c414b5c1f005f9dfbb853f94ec5e9add463d470

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page