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.8

  • Fixed memory leak when auto-decoding bytestrings.

  • Fixed potential memory leak when using on_unknown handlers that return invalid objects.

A full change log is available in the source package.

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

Uploaded Source

Built Distributions

jsonlib-1.3.8.win32-py2.5.exe (90.7 kB view details)

Uploaded Source

jsonlib-1.3.8.win32-py2.4.exe (90.7 kB view details)

Uploaded Source

jsonlib-1.3.8-py2.6-macosx-10.3-i386.egg (33.2 kB view details)

Uploaded Egg

jsonlib-1.3.8-py2.6-linux-i686.egg (45.2 kB view details)

Uploaded Egg

jsonlib-1.3.8-py2.5-win32.egg (18.6 kB view details)

Uploaded Egg

jsonlib-1.3.8-py2.5-macosx-10.3-i386.egg (33.4 kB view details)

Uploaded Egg

jsonlib-1.3.8-py2.5-linux-i686.egg (42.2 kB view details)

Uploaded Egg

jsonlib-1.3.8-py2.4-win32.egg (18.5 kB view details)

Uploaded Egg

jsonlib-1.3.8-py2.4-macosx-10.3-fat.egg (33.2 kB view details)

Uploaded Egg

jsonlib-1.3.8-py2.4-linux-i686.egg (42.0 kB view details)

Uploaded Egg

File details

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

File metadata

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

File hashes

Hashes for jsonlib-1.3.8.tar.gz
Algorithm Hash digest
SHA256 a7a47ed93cbabe194be67ad8924cc3e30a639dadbb8a5f428e4c5b42fa339f9c
MD5 c052b14233b8a5d9bce048c07a89bc3f
BLAKE2b-256 0c22f3f22ac10fc7ffe467ca84a264566697ccdd858f2b7b70230c7c24877abe

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for jsonlib-1.3.8.win32-py2.5.exe
Algorithm Hash digest
SHA256 7eccdeb6debdb7922f2297011b0754e2d91d2e3876b0bea3d3ebc6a18f1388e7
MD5 ad922b72cca25fee513254cb139e7796
BLAKE2b-256 d45cd0d171b3cb24be7e4d195cc577fd5ba41e791e8f3ac8f6e10988888e6450

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for jsonlib-1.3.8.win32-py2.4.exe
Algorithm Hash digest
SHA256 990b1fb21ef5ad4de12c4f70b17889f46cf1542ba38fd5376d3e28ccd9f87f49
MD5 c039755be7e2f7aec0649546eb50f9ba
BLAKE2b-256 f5972884b5b29f98b6d492c4c58422715dac0b20b7fcfde079ebe1a41e8f9aa7

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for jsonlib-1.3.8-py2.6-macosx-10.3-i386.egg
Algorithm Hash digest
SHA256 d99e696b62d4fd68063f42a0dd1e5ebdee5a9bab28f194345bcb0568550aecb0
MD5 1c5a9e33304a1f0e71d7792e31cd8ec0
BLAKE2b-256 3f553414ef86c67d074ae070baa595dd348c98958961af31a833e6fa5eb14e2b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for jsonlib-1.3.8-py2.6-linux-i686.egg
Algorithm Hash digest
SHA256 445e61aa3d95e092b9733aa7599bcc33ae92c110ae5baa0bd743adfea60199f7
MD5 49b13eeb48429ba8b9cb5d7013d06566
BLAKE2b-256 00246218d43c8887929374903a659eb72e890acb2627f75bbcbca21753791dee

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for jsonlib-1.3.8-py2.5-win32.egg
Algorithm Hash digest
SHA256 a3e3bfa7b7a8768d31f60e099236acbd694e566a6460462faf10267376d6e58e
MD5 6aa09b4962d8fc0a6fee8fe2c65d3a75
BLAKE2b-256 4eeee26780ad170abee5b3f8bd172696d68b0dd196c2ee37fd58166f9b37e34c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for jsonlib-1.3.8-py2.5-macosx-10.3-i386.egg
Algorithm Hash digest
SHA256 105138637d645e6fcb4aec9899e9266afabda4902b6070dbe7fa0ccb6b817747
MD5 712c7e8cbc8282751a0915ba76a366d6
BLAKE2b-256 28da3cba9c570b0fa6c006c45b4215b2d2065ba37964c7c25e63e5c67a31b4da

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for jsonlib-1.3.8-py2.5-linux-i686.egg
Algorithm Hash digest
SHA256 60c453f15434ec7ab51e69b017d6d810a2c8d6fdb28071f2b774debb8390d483
MD5 edc6059046e145c2fad0946f95980aa4
BLAKE2b-256 3ab8b311bf16ccdd79cca37a1908ce18899707ad7844df7f2a231b608fe7e3c6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for jsonlib-1.3.8-py2.4-win32.egg
Algorithm Hash digest
SHA256 5c2caec57216b481f619fa3c6e7a300901b745596a45a1c031bc6b2b5d1fdac6
MD5 9c8be79bc2fcd3342c1cc1ba5a03823c
BLAKE2b-256 7702d3ec6bde7c84facf8ec1539c8babf5b4411ec4bac359f9e923183ce73525

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for jsonlib-1.3.8-py2.4-macosx-10.3-fat.egg
Algorithm Hash digest
SHA256 4e4bc7d844a70bd271fde869021d6223f2705269377bcbc15bda9859553ea1d9
MD5 1e2355fb10cd834b00a85767a0d4d614
BLAKE2b-256 b00b9d2cfdc0e3c5c75111c8b433a2b21d084224f6eb426b9c3bea720f65980e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for jsonlib-1.3.8-py2.4-linux-i686.egg
Algorithm Hash digest
SHA256 7b29b0c540b4782cafeb9891a1a8154a3f0519e3b846c02aa725685dd0206be1
MD5 c3ab267f48053f9e76ab58632d66660f
BLAKE2b-256 9d941f3b1c9a86d92a9130d8c2a4a5593c8834ccd63dfb6d3105a1013e3dd1d5

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page