Skip to main content

JSON serializer/deserializer for Python

Project description

Author:

John Millikin

Overview

JSON is a lightweight data-interchange format. It is often used for exchanging data between a web server and user agent.

This module aims to produce a library for serializing and deserializing JSON that conforms strictly to RFC 4627.

For the Python 3 version of jsonlib, see jsonlib-python3.

Other JSON implementations of interest include simplejson (available in the standard library as of Python 2.6) and demjson.

Usage

jsonlib has two functions of interest, read and write. It also defines some exception: ReadError, WriteError, and UnknownSerializerError.

For compatibility with the standard library, read is aliased to loads and write is aliased to dumps. They do not have the same set of advanced parameters, but may be used interchangeably for simple invocations.

Deserialization

To deserialize a JSON expression, call the jsonlib.read function with an instance of str or unicode.

>>> import jsonlib
>>> jsonlib.read ('["Hello world!"]')
[u'Hello world!']

Floating-point values

By default, jsonlib will parse values such as “1.1” into an instance of decimal.Decimal. To use the built-in value type float instead, set the use_float parameter to True. Please note that this may cause a loss of precision when parsing some values.

>>> jsonlib.read ('[1.5]', use_float = True)
[1.5]
>>> jsonlib.read ('[1.1]', use_float = True)
[1.1000000000000001]
>>> jsonlib.read ('[3.14159265358979323846]', use_float = True)
[3.1415926535897931]

Serialization

Serialization has more options, but they are set to reasonable defaults. The simplest use is to call jsonlib.write with a Python value.

>>> import jsonlib
>>> jsonlib.write (['Hello world!'])
'["Hello world!"]'

Pretty-Printing

To “pretty-print” the output, pass a value for the indent parameter.

>>> print jsonlib.write (['Hello world!'], indent = '    ')
[
    "Hello world!"
]
>>>

Mapping Key Sorting

By default, mapping keys are serialized in whatever order they are stored by Python. To force a consistent ordering (for example, in doctests) use the sort_keys parameter.

>>> jsonlib.write ({'e': 'Hello', 'm': 'World!'})
'{"m":"World!","e":"Hello"}'
>>> jsonlib.write ({'e': 'Hello', 'm': 'World!'}, sort_keys = True)
'{"e":"Hello","m":"World!"}'

Encoding and Unicode

By default, the output is encoded in UTF-8. If you require a different encoding, pass the name of a Python codec as the encoding parameter.

>>> jsonlib.write (['Hello world!'], encoding = 'utf-16-be')
'\x00[\x00"\x00H\x00e\x00l\x00l\x00o\x00 \x00w\x00o\x00r\x00l\x00d\x00!\x00"\x00]'

To retrieve an unencoded unicode instance, pass None for the encoding.

>>> jsonlib.write (['Hello world!'], encoding = None)
u'["Hello world!"]'

By default, non-ASCII codepoints are forbidden in the output. To include higher codepoints in the output, set ascii_only to False.

>>> jsonlib.write ([u'Hello \u266a'], encoding = None)
u'["Hello \\u266a"]'
>>> jsonlib.write ([u'Hello \u266a'], encoding = None, ascii_only = False)
u'["Hello \u266a"]'

Mapping Key Coercion

Because JSON objects must have string keys, an exception will be raised when non-string keys are encountered in a mapping. It can be useful to coerce mapping keys to strings, so the coerce_keys parameter is available.

>>> jsonlib.write ({True: 1})
Traceback (most recent call last):
WriteError: Only strings may be used as object keys.
>>> jsonlib.write ({True: 1}, coerce_keys = True)
'{"true":1}'

Serializing Other Types

If the object implements the iterator or mapping protocol, it will be handled automatically. If the object is intended for use as a basic value, it should subclass one of the supported basic values.

String-like objects that do not inherit from str, unicode, or UserString.UserString will likely be serialized as a list. This will not be changed. If iterating them returns an instance of the same type, the serializer might crash. This (hopefully) will be changed.

To serialize a type not known to jsonlib, use the on_unknown parameter to write:

>>> from datetime import date
>>> def unknown_handler (value):
...     if isinstance (value, date): return str (value)
...     raise jsonlib.UnknownSerializerError
>>> jsonlib.write ([date (2000, 1, 1)], on_unknown = unknown_handler)
'["2000-01-01"]'

Streaming Serializer

When serializing large objects, the use of an in-memory buffer may cause too much memory to be used. For these situations, use the dump function to write objects to a file-like object:

>>> import sys
>>> jsonlib.dump (["Written to stdout"], sys.stdout)
["Written to stdout"]
>>>

Exceptions

ReadError

Raised by read if an error was encountered parsing the expression. Will contain the line, column, and character position of the error.

Note that this will report the character, not the byte, of the character that caused the error.

WriteError

Raised by write or dump if an error was encountered serializing the passed value.

UnknownSerializerError

A subclass of WriteError that is raised when a value cannot be serialized. See the on_unknown parameter to write.

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

jsonlib-1.3.10.tar.gz (20.5 kB view details)

Uploaded Source

Built Distributions

jsonlib-1.3.10.win32-py2.5.exe (92.1 kB view details)

Uploaded Source

jsonlib-1.3.10.win32-py2.4.exe (92.0 kB view details)

Uploaded Source

jsonlib-1.3.10-py2.6-macosx-10.3-i386.egg (34.1 kB view details)

Uploaded Egg

jsonlib-1.3.10-py2.6-linux-i686.egg (47.2 kB view details)

Uploaded Egg

jsonlib-1.3.10-py2.5-win32.egg (19.3 kB view details)

Uploaded Egg

jsonlib-1.3.10-py2.5-macosx-10.3-i386.egg (34.4 kB view details)

Uploaded Egg

jsonlib-1.3.10-py2.5-linux-i686.egg (43.3 kB view details)

Uploaded Egg

jsonlib-1.3.10-py2.4-win32.egg (19.1 kB view details)

Uploaded Egg

jsonlib-1.3.10-py2.4-macosx-10.3-fat.egg (34.2 kB view details)

Uploaded Egg

jsonlib-1.3.10-py2.4-linux-i686.egg (43.2 kB view details)

Uploaded Egg

File details

Details for the file jsonlib-1.3.10.tar.gz.

File metadata

  • Download URL: jsonlib-1.3.10.tar.gz
  • Upload date:
  • Size: 20.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for jsonlib-1.3.10.tar.gz
Algorithm Hash digest
SHA256 baa11eb9ba5307cbb5224340a974a95353b0627e6e2dd5243e8bdbff86429912
MD5 5b44c39732a9110fdc6f0c17ec4d986c
BLAKE2b-256 772529d2902329b9d157339901c3e8445522669fd60d36e683d5a11c8cb9abdb

See more details on using hashes here.

File details

Details for the file jsonlib-1.3.10.win32-py2.5.exe.

File metadata

File hashes

Hashes for jsonlib-1.3.10.win32-py2.5.exe
Algorithm Hash digest
SHA256 2e6d5f46d1f5e62e9ea733897fccfd33c0c10cb42de4c5508efa24fccb5888bd
MD5 9c5775140739a7fe721150b2ec9c7737
BLAKE2b-256 5bda65a33bb80a0bdd5a716f4e080e8670d427d0657ef06fb61ff94c528e18ab

See more details on using hashes here.

File details

Details for the file jsonlib-1.3.10.win32-py2.4.exe.

File metadata

File hashes

Hashes for jsonlib-1.3.10.win32-py2.4.exe
Algorithm Hash digest
SHA256 4966ce5db9520ad133ab07ccf97f634f53e9811958ca5b6a01acf63401bdde3c
MD5 187e9ef1ca32e3e02d14aff494ab460a
BLAKE2b-256 2dffb9bf1da53a77c72c86295c21218f7e39a034c3166fa4493675f7e62b285e

See more details on using hashes here.

File details

Details for the file jsonlib-1.3.10-py2.6-macosx-10.3-i386.egg.

File metadata

File hashes

Hashes for jsonlib-1.3.10-py2.6-macosx-10.3-i386.egg
Algorithm Hash digest
SHA256 d266130efd073a31f5a02f21d5ddbd902ab0acf46b1094e632c8c3a60c5e9536
MD5 e1e01c0358e2e5e83f93615259f65be9
BLAKE2b-256 e8563422aafc167470e1c8b3b12f76002ee10c51da686ed51fe373d2bac483ff

See more details on using hashes here.

File details

Details for the file jsonlib-1.3.10-py2.6-linux-i686.egg.

File metadata

File hashes

Hashes for jsonlib-1.3.10-py2.6-linux-i686.egg
Algorithm Hash digest
SHA256 2c842caac47280b966a9793f69c9a02e69ed77b5582cb7ea6778f277637425aa
MD5 048f1be239865e7cff2e09e742d48dad
BLAKE2b-256 58185a4c1a41cd94eee7503e8b63a37af53f3b5426aa2f590ccb64a4ddeb806b

See more details on using hashes here.

File details

Details for the file jsonlib-1.3.10-py2.5-win32.egg.

File metadata

File hashes

Hashes for jsonlib-1.3.10-py2.5-win32.egg
Algorithm Hash digest
SHA256 7ed3cc8637f6f76b0e83ea4eada76b9f240e2216691d316f4a76810d0fd975d6
MD5 d64d3b9021fcbe329211a62b3e36e39d
BLAKE2b-256 d0847beaccfc9e401c4a66712e642696327cefc2264dd83d4a5f5d6563db7008

See more details on using hashes here.

File details

Details for the file jsonlib-1.3.10-py2.5-macosx-10.3-i386.egg.

File metadata

File hashes

Hashes for jsonlib-1.3.10-py2.5-macosx-10.3-i386.egg
Algorithm Hash digest
SHA256 205d526887b6f7bd107f144bd94974b070a6afdc2f6fe10c286bdeb60504e114
MD5 2fa16fc637ec2fde9779ad80a485f811
BLAKE2b-256 c71c2a7b3998a25c225a4ad3b1998db6d4c6acfb171cf87f2f4e492c82c61018

See more details on using hashes here.

File details

Details for the file jsonlib-1.3.10-py2.5-linux-i686.egg.

File metadata

File hashes

Hashes for jsonlib-1.3.10-py2.5-linux-i686.egg
Algorithm Hash digest
SHA256 9c9f633421aedc981413687348b63ce815304655cece33e3ac9cfc43d9cda988
MD5 22b3f3c7169f8d78a835d564ac0772e7
BLAKE2b-256 b7bbd3d5b590091de5c174cc4093727a6998f0bbc48c2693c53113f62807d9de

See more details on using hashes here.

File details

Details for the file jsonlib-1.3.10-py2.4-win32.egg.

File metadata

File hashes

Hashes for jsonlib-1.3.10-py2.4-win32.egg
Algorithm Hash digest
SHA256 e206d917947d5de3c6add152157c3ada2fe6520b9047c17e28773061e23e0c90
MD5 7c1cd6b23a5bdd461e6b39f9e1b5263d
BLAKE2b-256 dc6f9ade47973b495d24674f3fa08b4de7d8009555f0f14014397cabae076b52

See more details on using hashes here.

File details

Details for the file jsonlib-1.3.10-py2.4-macosx-10.3-fat.egg.

File metadata

File hashes

Hashes for jsonlib-1.3.10-py2.4-macosx-10.3-fat.egg
Algorithm Hash digest
SHA256 3706f9a4e4629535b89526bfd3b4c319a7da744b48a96021e443610562d249a1
MD5 1fd0b3960d069bef1a8fd0cb87fa53b4
BLAKE2b-256 34aa15132f94a8f631d0c294def6f250f37037f34da1eb7646ea76292d9a8845

See more details on using hashes here.

File details

Details for the file jsonlib-1.3.10-py2.4-linux-i686.egg.

File metadata

File hashes

Hashes for jsonlib-1.3.10-py2.4-linux-i686.egg
Algorithm Hash digest
SHA256 f4465dd0f979424591df895daee019e2cc855157c008006fc9ebe00531ba4949
MD5 812ce9fbb313c3da6f743e7ab7f987fc
BLAKE2b-256 0e93dbc78d35573a039bbfadc9b211feec0a7c1c0a3997ad26e64421bc1c920d

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 Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page