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.

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

  • If an unterminated object or array is encountered, report its start location properly.

  • Improved reporting of unknown escape codes.

  • Raise an exception when parsing a root value that is not an array or object.

  • Allow instances of UserString to be used as object keys.

  • Implemented the dump() function.

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

Uploaded Source

Built Distributions

jsonlib-1.3.6.win32-py2.5.exe (89.5 kB view details)

Uploaded Source

jsonlib-1.3.6.win32-py2.4.exe (89.5 kB view details)

Uploaded Source

jsonlib-1.3.6-py2.5-win32.egg (18.1 kB view details)

Uploaded Egg

jsonlib-1.3.6-py2.5-macosx-10.3-i386.egg (19.2 kB view details)

Uploaded Egg

jsonlib-1.3.6-py2.5-linux-i686.egg (41.5 kB view details)

Uploaded Egg

jsonlib-1.3.6-py2.4-win32.egg (18.3 kB view details)

Uploaded Egg

jsonlib-1.3.6-py2.4-macosx-10.3-fat.egg (32.2 kB view details)

Uploaded Egg

jsonlib-1.3.6-py2.4-linux-i686.egg (41.5 kB view details)

Uploaded Egg

File details

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

File metadata

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

File hashes

Hashes for jsonlib-1.3.6.tar.gz
Algorithm Hash digest
SHA256 62372d4068a6116fad8485623b276ff692c91e38eecf263eb5723f84f8575cc8
MD5 834f95a9797ee01b7630aacf72d611fc
BLAKE2b-256 8dca4052a0ca9224f005cfc2c9ca8e8edf95a8ffff5cda962577344d1aadef50

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for jsonlib-1.3.6.win32-py2.5.exe
Algorithm Hash digest
SHA256 75f926d344ab3154d0ae51f042265452c0f2b58cbbe46e9c2f82fe65ce70f7ac
MD5 c72ba1e1324e9958ef5a297fc732e556
BLAKE2b-256 fa86373fd1e04192eb70eecfc702df2e032979b8781d63f2905118760c641ea3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for jsonlib-1.3.6.win32-py2.4.exe
Algorithm Hash digest
SHA256 7048cc4abdbb9aa57a030bace291581b0a3677572b2414f45bc745b7b5130112
MD5 77ec4ae3411abc7017288c1b2358539e
BLAKE2b-256 ff1d50c24ee7d03f9c9effddd5471a494643f2fb172a57aff19cbc742ea512fb

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for jsonlib-1.3.6-py2.5-win32.egg
Algorithm Hash digest
SHA256 ce3a665bc7e6c975edd6dc30f8eaddf83dd7abf1498f791ab4bb50e19edc3015
MD5 f13b592bcddc94415db159cad804c614
BLAKE2b-256 e37e121d9de3f361f9ebdc2d144beffb96a772ef537e2651e3e029456953060a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for jsonlib-1.3.6-py2.5-macosx-10.3-i386.egg
Algorithm Hash digest
SHA256 6c2aa728c493ac1708834c0c9d1d142e710b604ed326e8267b23d43ae0363dc9
MD5 6947b03ec7148b681948febb97966019
BLAKE2b-256 de22140395f2bfc4360491329c42d0f6b4d6f5efc88eac0513e6a51cfe7a0ce7

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for jsonlib-1.3.6-py2.5-linux-i686.egg
Algorithm Hash digest
SHA256 8d7c68e9a3c4e3083776cc2aec2239da6e377e4b296df92c671c9f7f00629af0
MD5 c00efbb58f6891efbe192c77b1801b1c
BLAKE2b-256 c62a6658db900913f8cdf80943eeb95ff3448c88926ccd645b262d684ff7ea45

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for jsonlib-1.3.6-py2.4-win32.egg
Algorithm Hash digest
SHA256 bc9ee4e65afd35aab0fc4fddc010c3df2a4f635028540bba1c12552bf55b8d99
MD5 777b177553f35b5b861f298be44b28ee
BLAKE2b-256 351d09a95ac2ea51c919f1efe977df934b30f6aadeeceaccd26e1fa6e0d59318

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for jsonlib-1.3.6-py2.4-macosx-10.3-fat.egg
Algorithm Hash digest
SHA256 da4f88eef8ea1d32414e8b886a67a696a709dd24b5f5c04d3c0db2decfaeca6a
MD5 d21abf320fc230bc06b262db09fa4dbe
BLAKE2b-256 4167c70dc4e0f343c8ecddd38de46bedbc7ddf57b690025280f060085cfe39f9

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for jsonlib-1.3.6-py2.4-linux-i686.egg
Algorithm Hash digest
SHA256 96eb70a21204f6bc66c8882824d24410c8743ffb824a8ef05d031fdeca067a9a
MD5 7dae7b8d2576f198acb10615829686f6
BLAKE2b-256 48d739bac69b016de1716e3690a7a6f210d84bb79b3506817b7207cf428b78e2

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