Skip to main content

A simple way of 'extending' json to support additional types like datetime, Decimal and binary data.

Project description

  • Free software: Simplified BSD license

Documentation

Jsonte is just a simple and well defined way of ‘extending’ json to support some additional types.

The primary goal is to add support for data types that are commonly used in databases. In particular:

  • dates, times, and timestamps (both with and without timezones)

  • arbitrary precision numeric entries

  • binary data

A secondary goal was to do this in such a way that the resulting json was easily human readable, and also programming language agnostic.

The format is simple, and is always itself valid json.

* A date:      { "#date": "2015-05-28" }
* A time:      { "#time": "22:12:42.381000" }
* A timestamp: { "#tstamp": "2015-05-28T22:13:42.381000" }

These are just the ISO formats for each of the above items.

* A numeric entry:  { "#num": "1234.0000" }
* Some binary data: { "#bin": "SGVsbG8gV29ybGQhAA==" }

The numeric entry is encoded as a string so the degree of precision is not lost. The binary data is just base64 encoded.

Key Escaping

Keys in objects that start with either a hash (#) or a tidle (~) are escaped by a tidle (~) being prefixed to the key. This is to avoid any accidental collisions with the ‘special’ object keys used.

So an object that would normally be encoded as { “#bin”: 1234 } would become { “~#bin”: 1234 } when encoded by jsonte, and { “~foo”: “bar” } would become { “~~foo”: “bar” }

Python Implementation

The python implementation is designed to be a drop-in replacement for the standard json library.

import datetime
import decimal
import dateutil.tz
import jsonte

timezone = dateutil.tz.gettz('Australia/Victoria')
data = { 'now': datetime.datetime.now(),
         'now_with_tz': datetime.datetime.now(timezone),
         'today': datetime.date.today(),
         'the_time': datetime.datetime.now().time(),
         'cost': decimal.Decimal('12.50'),
         'binary': bytearray('Hello World!\x00\x01\x02'),
         '%foo': 'a',
         '#num': 1,
         '~baz': 2.0 }

serialiser = jsonte.JsonteSerialiser(indent=4, sort_keys=True)
s = serialiser.dumps(data)
data2 = serialiser.loads(s)

At this point we have

>>> data is data2
False
>>> data == data2
True
>>> print s
{
    "%foo": "a",
    "binary": {
        "#bin": "SGVsbG8gV29ybGQhAAEC"
    },
    "cost": {
        "#num": "12.50"
    },
    "now": {
        "#tstamp": "2015-05-28T23:43:40.454000"
    },
    "now_with_tz": {
        "#tstamp": "2015-05-28T23:43:40.454000+10:00"
    },
    "the_time": {
        "#time": "23:43:40.454000"
    },
    "today": {
        "#date": "2015-05-28"
    },
    "~#num": 1,
    "~~baz": 2.0
}

History

0.8.5 (2015-08-09)

  • Bugfix: fix an issue when _one_shot was used in iterencode in non-2.6 versions of Python

0.8.4 (2015-08-06)

  • Major reworking
    • Make jsonte a module rather than a package, since the scope is small enough to do so.

    • Remove top level functions and put the core functionality into a class that is instantiated.

    • Allow a custom objecthook to be used.

    • Allow the ability to enforce json ‘websafety’ through raising an exception or prefixing the string.

    • Allow additional type serialisers to be added in any order (subclasses no longer need to go first).

    • Support for Python 2.6, 3.3 and 3.4 (as initially just 2.7)

0.8.3 (2015-06-08)

  • Base SerialisationDict on a separate a mixin class to provide more flexability.

0.8.2 (2015-06-04)

  • Rename JsonteDict to SerialisationDict to add clarity.

  • Give access to the registered type classes.

0.8.0 (2015-05-22)

  • Initial version

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

jsonte-0.8.5a.zip (14.6 kB view hashes)

Uploaded Source

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