Skip to main content

JSON (with dates)

Project description


Build Status Pypi Version Python Versions

An extension of json: but one that (naively) handles dates.


You can use pip. (Note that it uses setuptools)

pip install jsond

To also install development tools use:

pip install jsond[dev]
# or
pip install -e .[dev]

Simple Usage

I have a date.

>>> import datetime
>>> my_date = datetime.datetime(2011, 3, 15, 0, 0, 0)

Standard json doesn't deal with dates :(

>>> import json
>>> json.dumps(my_date)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/", line 243, in dumps
    return _default_encoder.encode(obj)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/", line 207, in encode
    chunks = self.iterencode(o, _one_shot=True)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/", line 270, in iterencode
    return _iterencode(o, 0)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/", line 184, in default
    raise TypeError(repr(o) + " is not JSON serializable")
TypeError: datetime.datetime(2011, 3, 15, 0, 0) is not JSON serializable

But jsond will handle it

>>> import jsond
>>> jsond.dumps(my_date)

And we can pull the date back as well.

>>> jsond.loads('"datetime:2011-03-15T00:00:00"')
datetime.datetime(2011, 3, 15, 0, 0)
>>> date2 = jsond.loads('"datetime:2011-03-15T00:00:00"')
>>> type(date2)
<type 'datetime.datetime'>
>>> date2 == my_date
>>> date2
datetime.datetime(2011, 3, 15, 0, 0)

Also, jsond will produce output that won't break json. (Though of course, you'll get a string rather than a datetime object.)

>>> enc = jsond.dumps(my_date)
>>> json.loads(enc)

Other usage

Often message-processing systems will use json internally to serialise messages.

But that means that dates can't (easily) be used.

To help with this, we provide two functions: from_json_serialisable_object and to_json_serialisable_object.

For those who prefer spelling serialise with a 'z', there are two 'alias' functions for convenience.

  • from_json_serializable_object -> from_json_serialisable_object
  • to_json_serializable_object -> to_json_serialisable_object
def handle_message(original_message):
    # At this stage original_message will be a dict, list etc, but it won't
    # have any datetime objects as it was json-serialisable.
    # We might have prepared it earlier with jsond.dumps, but that means that
    # we have 'datetime:...' string instead of datetime objects.
    message = jsond.from_json_serialisable_object(original_message)

    # message will now have datetime objects
    # We can do whatever processing we want.

    # Now we have to put message back on the queue. We have to output an
    # object that the broker will put back onto the queue etc.
    # So we can't have datetime objects. But we want to output an object
    # (not an encoded string).
    new_message = jsond.to_json_serialisable_object(message)

    # Now we can output/emit/etc new_message.
    return new_message

Project details

Download files

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

Files for jsond, version 1.5.0
Filename, size File type Python version Upload date Hashes
Filename, size jsond-1.5.0.tar.gz (5.3 kB) File type Source Python version None Upload date Hashes View

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Huawei Huawei PSF Sponsor Microsoft Microsoft PSF Sponsor NVIDIA NVIDIA PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page