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

A full change log is available in README.txt. Changes for the most recent version:

1.3.7

  • Fixed error reporting positions of syntax errors that occur immediately after a newline.

  • Add loads() and dumps() as aliases to read() and write(), respectively, for compatibility with the new json standard library module.

  • Small fixes to the test suite to clear spurious errors caused by differences between the behavior of repr() on instances of decimal.Decimal and UnicodeDecodeError.

Release files for jsonlib 1.3.7

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.7
File Size Uploaded
jsonlib-1.3.7.tar.gz 19.6 kB Details

Built distributions (wheels)

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

Total release size: 467.6 kB

Release files / jsonlib-1.3.7.tar.gz

Download URL jsonlib-1.3.7.tar.gz
Size 19.6 kB
Tags Source
SHA-256 checksum
How to use checksums
0a6dc46ebdb4f7c7418f863b50ae8d14536cff7b00e32e1162cd01be132ad9f9
BLAKE2b-256 checksum
How to use checksums
81e68f524d10c873501187d32bdfd47a883ca929f6a06f4379f08bd34623c7b8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

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

Download URL jsonlib-1.3.7.win32-py2.5.exe
Size 90.5 kB
Tags Source
SHA-256 checksum
How to use checksums
46b6c8fcf4a55999047446ac07cab22c42391b158b4ab0b2d3cfe464bba94609
BLAKE2b-256 checksum
How to use checksums
0f7040e34aaf686cda8734ccfd95dff4c27bcac12acc8060f8abe67736eadab9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

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

Download URL jsonlib-1.3.7.win32-py2.4.exe
Size 90.5 kB
Tags Source
SHA-256 checksum
How to use checksums
ecfc1d6e7b8d2d8de5eadf2374e00c65859aa894502e540ea3f22ae11615ad19
BLAKE2b-256 checksum
How to use checksums
7b0fd635adce8835e145ea4cb5185d5658a078df582080f6f65da090aa0ee885
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

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

Download URL jsonlib-1.3.7-py2.6-macosx-10.3-i386.egg
Size 33.1 kB
Tags Egg
SHA-256 checksum
How to use checksums
37b2640dbd0c00ac1c3ed074b88ad581978cbe897f881967a9750d306bd8ca1f
BLAKE2b-256 checksum
How to use checksums
6521ee112d8082581ed784c910ad784b5bd8a1f9c3c26ff25ed09e224497d71d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

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

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

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

Download URL jsonlib-1.3.7-py2.5-win32.egg
Size 18.6 kB
Tags Egg
SHA-256 checksum
How to use checksums
5df471b46f2180b89a24098d632a07ffb8cc7db3b219d7072c6f86e7d87d5300
BLAKE2b-256 checksum
How to use checksums
044dc8f417c1cabf8545ec2d644f671356df3cf94aec51b6aea01bd356abf0cb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

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

Download URL jsonlib-1.3.7-py2.5-macosx-10.3-i386.egg
Size 33.3 kB
Tags Egg
SHA-256 checksum
How to use checksums
511550602951cc54ea0e302c0bf325ed335e8263cd02df9806d1990074e70dbe
BLAKE2b-256 checksum
How to use checksums
9d53dc2de82644effbe059fb7d65211c93c5fe1dd6955df530285ef8834e1798
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

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

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

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

Download URL jsonlib-1.3.7-py2.4-win32.egg
Size 18.4 kB
Tags Egg
SHA-256 checksum
How to use checksums
bdb77d08b9104a565d6f06c3597d34dd4fd4e45b10db9795bcda30bcc104840d
BLAKE2b-256 checksum
How to use checksums
5f9dec958b0e248cf4ec5e8ea44c11d4b4afa414538e9e18de9937866cf14159
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

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

Download URL jsonlib-1.3.7-py2.4-macosx-10.3-fat.egg
Size 33.1 kB
Tags Egg
SHA-256 checksum
How to use checksums
8256d409fad016bb90c90e77dc574f414e67febb5f0dc294c9fbac5d9a7a7cb4
BLAKE2b-256 checksum
How to use checksums
51bb9ac0b130d9945d807123349774ce41a6a5f3b2aaebdd5dd8d7d0febb3b71
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

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

Download URL jsonlib-1.3.7-py2.4-linux-i686.egg
Size 41.9 kB
Tags Egg
SHA-256 checksum
How to use checksums
479481a863a363d28b677671a27f5441ef656d10edd322b31c3662f25b4234f3
BLAKE2b-256 checksum
How to use checksums
e655702e1b77c11e0f06b0e4f088b2ef2c9c768654dc008708a3d59196f1ebb4
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

1.3.9

11 release files

1.3.8

11 release files

This release

1.3.7 This release

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