Skip to main content

JSON module with added datetime support.

Project description

JSON with datetime support.

This module is a light shim around the existing standard library JSON moduile.

It encodes and decodes Python datetime objects as human-readable ISO8601 dates.

https://raw.githubusercontent.com/zeth/jsondt/master/iso8601.jpg

It is a drop-in replacement for standard library JSON, so you can just do:

>>> import jsondt as json

Then carry on with the standard library JSON documentation.

Currently, this module supports only Python 3.7 and above.

Backporting to older versions is possible if there is demand for it.

Simple example

Here we go:

>>> import jsondt as json
>>> from datetime import datetime
>>> myobject = {'ctime': datetime.now()}
>>> encoded = json.dumps(myobject)
>>> print(encoded)
'{"ctime": "2019-08-19T18:18:25.609815"}'
>>> decoded = json.loads(encoded)
>>> print(decoded)
{'ctime': datetime.datetime(2019, 8, 19, 18, 18, 25, 609815)}
>>> myobject == decoded
True

JavaScript compatible

One of the main use cases is to interoperate with JSON produced by JavaScript in the browser.

Spin up your Javascript console:

>>> const record = {'ctime': new Date()}
>>> const encoded = JSON.stringify(record);
>>> console.log(encoded)
{"ctime":"2019-08-19T17:25:03.547Z"}

Now get that JSON string to Python through your API call or post request (here’s one that we made earlier):

>>> encoded = '{"ctime":"2019-08-19T17:25:03.547Z"}'
>>> import jsondt as json
>>> json.loads(encoded)
{'ctime': datetime.datetime(2019, 8, 19, 17, 25, 3, 547000,
tzinfo=datetime.timezone.utc)}

Say Hello

I am interested in more common use cases for ISO 8601 dates.

If you have an ISO 8601 date in JSON produced by a well known application or library (in any language) and jsondt does not recognise it, please make an issue on GitHub and I will see if I can add support.

Control Mode

You probably don’t need to care about this bit.

There is a second mode that is semi-automatic instead of fully-automatic.

Imagine we have this strange object:

>>> strange = {'a_date': datetime.datetime(2019, 8, 19, 21, 32, 59, 169730),
...            'b_date': '2018-05-01T07:03:44.560600'}

If we dump it and load it back, it will load both dates as datetime objects.

>>> import jsondt as json
>>> encoded = json.dumps(strange)
>>> decoded = json.loads(encoded)
>>> decoded == strange
False

If however, you prefer the original object back, you can use control mode.

>>> import jsondt as json
>>> encoded = json.dumps(strange, control=True)
>>> decoded = json.loads(encoded, control=True)
>>> decoded == strange
True

The way it works, is that with control=True, it puts a control code (backslash D) at the start of the encoded date. Like so:

>>> print(encoded)
{"a_date": "\\D2019-08-19T21:32:59.169730",
 "b_date": "2018-05-01T07:03:44.560600"}

Thanks for reading

If you want more examples, read the supplied tests.

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

jsondt-1.0.0.tar.gz (10.8 kB view details)

Uploaded Source

Built Distributions

jsondt-1.0.0-py3.7.egg (5.5 kB view details)

Uploaded Source

jsondt-1.0.0-py3-none-any.whl (9.7 kB view details)

Uploaded Python 3

File details

Details for the file jsondt-1.0.0.tar.gz.

File metadata

  • Download URL: jsondt-1.0.0.tar.gz
  • Upload date:
  • Size: 10.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.1.0 requests-toolbelt/0.9.1 tqdm/4.34.0 CPython/3.7.3

File hashes

Hashes for jsondt-1.0.0.tar.gz
Algorithm Hash digest
SHA256 866b1fff9f7b2cfd89979790c0b2cc92d9be74861b2d9738430186bce9ad96cc
MD5 a549f3e89a03fc3fa6c4682f89132878
BLAKE2b-256 113c027ee3781623962b7cfdc8f6da814312b5443897ae49b9ff047f708e2b8a

See more details on using hashes here.

File details

Details for the file jsondt-1.0.0-py3.7.egg.

File metadata

  • Download URL: jsondt-1.0.0-py3.7.egg
  • Upload date:
  • Size: 5.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.1.0 requests-toolbelt/0.9.1 tqdm/4.34.0 CPython/3.7.3

File hashes

Hashes for jsondt-1.0.0-py3.7.egg
Algorithm Hash digest
SHA256 4712727467b8d3b292f6e129de013169574e650fe949c5d62c77a5afd9659a20
MD5 774e9d29fd69915ad8e20bb90d25ffb6
BLAKE2b-256 43c7d954b90b58e9f78b1544e388fab407f53621711befd1c3831cd1a120f316

See more details on using hashes here.

File details

Details for the file jsondt-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: jsondt-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 9.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.1.0 requests-toolbelt/0.9.1 tqdm/4.34.0 CPython/3.7.3

File hashes

Hashes for jsondt-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 71ac895897c4d2da9471c1c900de31063b4034181f98e19c523e11b9eaa4d932
MD5 55323929e731ac17a3a80b9943872420
BLAKE2b-256 3911b03dfd8f813a3e07d397fd9fcd615d667002eeac28a22a50f36a57ccf5ae

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