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.

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!']

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.

Change Log

1.3.9

  • Fixed a crash on some platforms when passing a non-string object for indentation.

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.9.tar.gz (20.0 kB view details)

Uploaded Source

Built Distributions

jsonlib-1.3.9.win32-py2.5.exe (91.1 kB view details)

Uploaded Source

jsonlib-1.3.9.win32-py2.4.exe (91.0 kB view details)

Uploaded Source

jsonlib-1.3.9-py2.6-macosx-10.3-i386.egg (33.5 kB view details)

Uploaded Egg

jsonlib-1.3.9-py2.6-linux-i686.egg (46.7 kB view details)

Uploaded Egg

jsonlib-1.3.9-py2.5-win32.egg (18.9 kB view details)

Uploaded Egg

jsonlib-1.3.9-py2.5-macosx-10.3-i386.egg (33.7 kB view details)

Uploaded Egg

jsonlib-1.3.9-py2.5-linux-i686.egg (42.8 kB view details)

Uploaded Egg

jsonlib-1.3.9-py2.4-win32.egg (18.7 kB view details)

Uploaded Egg

jsonlib-1.3.9-py2.4-macosx-10.3-fat.egg (33.8 kB view details)

Uploaded Egg

jsonlib-1.3.9-py2.4-linux-i686.egg (42.7 kB view details)

Uploaded Egg

File details

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

File metadata

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

File hashes

Hashes for jsonlib-1.3.9.tar.gz
Algorithm Hash digest
SHA256 37eb2a0e5620343bc5a93378611ce3de49c3149c1c5da5faec6f8fed35f7bd6e
MD5 905a7b624ec2ad39caaa289856782b7b
BLAKE2b-256 a67a311cd44f20d652ddf3e8043986d1d655dd1318621848acb8b93615c65f62

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for jsonlib-1.3.9.win32-py2.5.exe
Algorithm Hash digest
SHA256 45b9affaaef0c371a829e8f7edd26e9c0631030fc517e9871741e79eb7873934
MD5 314a324c52da5213b85e34cf6256cecc
BLAKE2b-256 a002c078971b1b770de481e1ca86f804dfe1bfa9d30bdd2bdbf0cc22c547737f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for jsonlib-1.3.9.win32-py2.4.exe
Algorithm Hash digest
SHA256 45c5fb42e067679c5694204e8dc9c5d16d2abc62f744f2b56a372d67f427c96c
MD5 513fd5ce6d651286641946bc54cf234c
BLAKE2b-256 f8dfba94c84230038a5d74f83bbe00dc3b54746439c667c5473b002c262e456b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for jsonlib-1.3.9-py2.6-macosx-10.3-i386.egg
Algorithm Hash digest
SHA256 45cac2cc8e69b4ab5d78434996efd7bec15a151ffa7484ecb58e10ed357cefce
MD5 e5a9c3ebaafd6e756cb5c2d3d7f427bd
BLAKE2b-256 fac674cdc3125b7a9572de28314ba7b7d4a0d31e0ed266705c460f2d8b6c9dbd

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for jsonlib-1.3.9-py2.6-linux-i686.egg
Algorithm Hash digest
SHA256 b093ee432b3582cb025d0a7c04b1bfa0f9be3bf64a6be8538d48190a20c1eef8
MD5 56236dbf310cd106a4d15b807ab98e76
BLAKE2b-256 090604290202d289b1980487e1d75b88f26d57d3d8f55f51da6ab1940f7146c0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for jsonlib-1.3.9-py2.5-win32.egg
Algorithm Hash digest
SHA256 f16374f8265acb9b00c784b0f9baa7b14e3128d4866aee06c9d0bf64ae4c602d
MD5 d903411af2c288126a693e7c8858b2b8
BLAKE2b-256 2910a7dc55e7c6c185e7decc003686ea0e193128fc268d019e4ce63cdb93c7ea

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for jsonlib-1.3.9-py2.5-macosx-10.3-i386.egg
Algorithm Hash digest
SHA256 59cf4cd5eb859ffc9f2d26fb6375378ccafab04832c1eb840b2fc8a88a40fc06
MD5 3c69f1ebfeddbc733ae96828198e4935
BLAKE2b-256 020fec118e396b50cc00e0d5b315f6987258a2581209326939645df56bf0a5e0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for jsonlib-1.3.9-py2.5-linux-i686.egg
Algorithm Hash digest
SHA256 e9bb7da0e536eba27eaae923c9f6be6c049c54bd4b59476f9e197c2874078071
MD5 c93eeb9d87baa8bcaf627bbc2cfb89cc
BLAKE2b-256 ab3db6b07be7c1660cd3724eee7171a6ece8d614b40b9099b23835a5877fe1af

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for jsonlib-1.3.9-py2.4-win32.egg
Algorithm Hash digest
SHA256 00f5ba44172f6b6307bc2a1675a6e468507ee2ce9f0a8f0a45353443bc25c1f9
MD5 67a084f2c1e58db349b065473ae5e02e
BLAKE2b-256 b5db9b8d3f02c672daea5b3447e721e49a5b41aa4fa8d5b0a592c6144593e5f9

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for jsonlib-1.3.9-py2.4-macosx-10.3-fat.egg
Algorithm Hash digest
SHA256 95d39ace278e7797ffbfbba09cb6aad89751b4ac915ddc76896cc36baa9b00d5
MD5 9c2bed5ef0166304c6232d644cf66f75
BLAKE2b-256 9b70abe83be11b596f5e1f0fa044bfd26a17064a8c6108eb1687cac7865fc1b4

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for jsonlib-1.3.9-py2.4-linux-i686.egg
Algorithm Hash digest
SHA256 f82731f0dfd7a18f37c9ac2edee9eec1ab245ea0a9be1f46246508fe0eef94e1
MD5 de2e4f113730855c743aba569985bf92
BLAKE2b-256 cfb361cf56e7debf74a2aeb8c3ccb62d0e9f7044a99845e72cba18317c2640e3

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