Skip to main content
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.

Release files for jsonlib 1.3.9

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for jsonlib 1.3.9
File Size Uploaded
jsonlib-1.3.9.tar.gz 20.0 kB Details

Built distributions (wheels)

Table of built distributions (wheels) for jsonlib 1.3.9
File
jsonlib-1.3.9.win32-py2.5.exe Details
jsonlib-1.3.9.win32-py2.4.exe Details
jsonlib-1.3.9-py2.6-macosx-10.3-i386.egg Legacy Egg format - - Details
jsonlib-1.3.9-py2.6-linux-i686.egg Legacy Egg format - - Details
jsonlib-1.3.9-py2.5-win32.egg Legacy Egg format - - Details
jsonlib-1.3.9-py2.5-macosx-10.3-i386.egg Legacy Egg format - - Details
jsonlib-1.3.9-py2.5-linux-i686.egg Legacy Egg format - - Details
jsonlib-1.3.9-py2.4-win32.egg Legacy Egg format - - Details
jsonlib-1.3.9-py2.4-macosx-10.3-fat.egg Legacy Egg format - - Details
jsonlib-1.3.9-py2.4-linux-i686.egg Legacy Egg format - - Details

Total release size: 473.0 kB

Release files / jsonlib-1.3.9.tar.gz

Download URL jsonlib-1.3.9.tar.gz
Size 20.0 kB
Tags Source
SHA-256 checksum
How to use checksums
37eb2a0e5620343bc5a93378611ce3de49c3149c1c5da5faec6f8fed35f7bd6e
BLAKE2b-256 checksum
How to use checksums
a67a311cd44f20d652ddf3e8043986d1d655dd1318621848acb8b93615c65f62
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / jsonlib-1.3.9.win32-py2.5.exe

Download URL jsonlib-1.3.9.win32-py2.5.exe
Size 91.1 kB
Tags Source
SHA-256 checksum
How to use checksums
45b9affaaef0c371a829e8f7edd26e9c0631030fc517e9871741e79eb7873934
BLAKE2b-256 checksum
How to use checksums
a002c078971b1b770de481e1ca86f804dfe1bfa9d30bdd2bdbf0cc22c547737f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / jsonlib-1.3.9.win32-py2.4.exe

Download URL jsonlib-1.3.9.win32-py2.4.exe
Size 91.0 kB
Tags Source
SHA-256 checksum
How to use checksums
45c5fb42e067679c5694204e8dc9c5d16d2abc62f744f2b56a372d67f427c96c
BLAKE2b-256 checksum
How to use checksums
f8dfba94c84230038a5d74f83bbe00dc3b54746439c667c5473b002c262e456b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / jsonlib-1.3.9-py2.6-macosx-10.3-i386.egg

Download URL jsonlib-1.3.9-py2.6-macosx-10.3-i386.egg
Size 33.5 kB
Tags Egg
SHA-256 checksum
How to use checksums
45cac2cc8e69b4ab5d78434996efd7bec15a151ffa7484ecb58e10ed357cefce
BLAKE2b-256 checksum
How to use checksums
fac674cdc3125b7a9572de28314ba7b7d4a0d31e0ed266705c460f2d8b6c9dbd
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / jsonlib-1.3.9-py2.6-linux-i686.egg

Download URL jsonlib-1.3.9-py2.6-linux-i686.egg
Size 46.7 kB
Tags Egg
SHA-256 checksum
How to use checksums
b093ee432b3582cb025d0a7c04b1bfa0f9be3bf64a6be8538d48190a20c1eef8
BLAKE2b-256 checksum
How to use checksums
090604290202d289b1980487e1d75b88f26d57d3d8f55f51da6ab1940f7146c0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / jsonlib-1.3.9-py2.5-win32.egg

Download URL jsonlib-1.3.9-py2.5-win32.egg
Size 18.9 kB
Tags Egg
SHA-256 checksum
How to use checksums
f16374f8265acb9b00c784b0f9baa7b14e3128d4866aee06c9d0bf64ae4c602d
BLAKE2b-256 checksum
How to use checksums
2910a7dc55e7c6c185e7decc003686ea0e193128fc268d019e4ce63cdb93c7ea
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / jsonlib-1.3.9-py2.5-macosx-10.3-i386.egg

Download URL jsonlib-1.3.9-py2.5-macosx-10.3-i386.egg
Size 33.7 kB
Tags Egg
SHA-256 checksum
How to use checksums
59cf4cd5eb859ffc9f2d26fb6375378ccafab04832c1eb840b2fc8a88a40fc06
BLAKE2b-256 checksum
How to use checksums
020fec118e396b50cc00e0d5b315f6987258a2581209326939645df56bf0a5e0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / jsonlib-1.3.9-py2.5-linux-i686.egg

Download URL jsonlib-1.3.9-py2.5-linux-i686.egg
Size 42.8 kB
Tags Egg
SHA-256 checksum
How to use checksums
e9bb7da0e536eba27eaae923c9f6be6c049c54bd4b59476f9e197c2874078071
BLAKE2b-256 checksum
How to use checksums
ab3db6b07be7c1660cd3724eee7171a6ece8d614b40b9099b23835a5877fe1af
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / jsonlib-1.3.9-py2.4-win32.egg

Download URL jsonlib-1.3.9-py2.4-win32.egg
Size 18.7 kB
Tags Egg
SHA-256 checksum
How to use checksums
00f5ba44172f6b6307bc2a1675a6e468507ee2ce9f0a8f0a45353443bc25c1f9
BLAKE2b-256 checksum
How to use checksums
b5db9b8d3f02c672daea5b3447e721e49a5b41aa4fa8d5b0a592c6144593e5f9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / jsonlib-1.3.9-py2.4-macosx-10.3-fat.egg

Download URL jsonlib-1.3.9-py2.4-macosx-10.3-fat.egg
Size 33.8 kB
Tags Egg
SHA-256 checksum
How to use checksums
95d39ace278e7797ffbfbba09cb6aad89751b4ac915ddc76896cc36baa9b00d5
BLAKE2b-256 checksum
How to use checksums
9b70abe83be11b596f5e1f0fa044bfd26a17064a8c6108eb1687cac7865fc1b4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / jsonlib-1.3.9-py2.4-linux-i686.egg

Download URL jsonlib-1.3.9-py2.4-linux-i686.egg
Size 42.7 kB
Tags Egg
SHA-256 checksum
How to use checksums
f82731f0dfd7a18f37c9ac2edee9eec1ab245ea0a9be1f46246508fe0eef94e1
BLAKE2b-256 checksum
How to use checksums
cfb361cf56e7debf74a2aeb8c3ccb62d0e9f7044a99845e72cba18317c2640e3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

1.6.1

1 release file

1.6

This release

1.3.9 This release

11 release files

1.3.8

11 release files

1.3.6

9 release files

1.3.5

9 release files

1.3.4

9 release files

1.3.3

9 release files

1.3.2

5 release files

1.3.1

5 release files

1.3.0

5 release files

1.2.7

5 release files

1.2.6

5 release files

1.2.5

5 release files

1.2.3

4 release files

1.2.2

1 release file

1.2.1

1 release file

1.2.0

1 release file

1.1.0

1 release file

1.0.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page