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

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.

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

Uploaded Source

Built Distributions

jsonlib-1.3.7.win32-py2.5.exe (90.5 kB view details)

Uploaded Source

jsonlib-1.3.7.win32-py2.4.exe (90.5 kB view details)

Uploaded Source

jsonlib-1.3.7-py2.6-macosx-10.3-i386.egg (33.1 kB view details)

Uploaded Egg

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

Uploaded Egg

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

Uploaded Egg

jsonlib-1.3.7-py2.5-macosx-10.3-i386.egg (33.3 kB view details)

Uploaded Egg

jsonlib-1.3.7-py2.5-linux-i686.egg (41.9 kB view details)

Uploaded Egg

jsonlib-1.3.7-py2.4-win32.egg (18.4 kB view details)

Uploaded Egg

jsonlib-1.3.7-py2.4-macosx-10.3-fat.egg (33.1 kB view details)

Uploaded Egg

jsonlib-1.3.7-py2.4-linux-i686.egg (41.9 kB view details)

Uploaded Egg

File details

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

File metadata

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

File hashes

Hashes for jsonlib-1.3.7.tar.gz
Algorithm Hash digest
SHA256 0a6dc46ebdb4f7c7418f863b50ae8d14536cff7b00e32e1162cd01be132ad9f9
MD5 343b14d027a72eba212502825fd87c8b
BLAKE2b-256 81e68f524d10c873501187d32bdfd47a883ca929f6a06f4379f08bd34623c7b8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for jsonlib-1.3.7.win32-py2.5.exe
Algorithm Hash digest
SHA256 46b6c8fcf4a55999047446ac07cab22c42391b158b4ab0b2d3cfe464bba94609
MD5 f08a017e2e6d7d39001b27bf84eea406
BLAKE2b-256 0f7040e34aaf686cda8734ccfd95dff4c27bcac12acc8060f8abe67736eadab9

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for jsonlib-1.3.7.win32-py2.4.exe
Algorithm Hash digest
SHA256 ecfc1d6e7b8d2d8de5eadf2374e00c65859aa894502e540ea3f22ae11615ad19
MD5 fcbdaf9c5d0e6cd9f1fc34ce95f6038d
BLAKE2b-256 7b0fd635adce8835e145ea4cb5185d5658a078df582080f6f65da090aa0ee885

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for jsonlib-1.3.7-py2.6-macosx-10.3-i386.egg
Algorithm Hash digest
SHA256 37b2640dbd0c00ac1c3ed074b88ad581978cbe897f881967a9750d306bd8ca1f
MD5 65b268e7debf7c25132befa3d461d226
BLAKE2b-256 6521ee112d8082581ed784c910ad784b5bd8a1f9c3c26ff25ed09e224497d71d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for jsonlib-1.3.7-py2.6-linux-i686.egg
Algorithm Hash digest
SHA256 3e232441dee6ebd689f3e7ed19d5777b9dfcca619e73a5fc94f0d0b0c9246b79
MD5 f36c221012a7a7311a272e454c3d2ad8
BLAKE2b-256 a7d8b088115a727df79c6a41fa66412796931358e42f3e3f39c2695b2a9f934c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for jsonlib-1.3.7-py2.5-win32.egg
Algorithm Hash digest
SHA256 5df471b46f2180b89a24098d632a07ffb8cc7db3b219d7072c6f86e7d87d5300
MD5 29dca16bbe3f3820d0176565f7414f25
BLAKE2b-256 044dc8f417c1cabf8545ec2d644f671356df3cf94aec51b6aea01bd356abf0cb

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for jsonlib-1.3.7-py2.5-macosx-10.3-i386.egg
Algorithm Hash digest
SHA256 511550602951cc54ea0e302c0bf325ed335e8263cd02df9806d1990074e70dbe
MD5 5ee7a2411b40e508d9291e90ab92310d
BLAKE2b-256 9d53dc2de82644effbe059fb7d65211c93c5fe1dd6955df530285ef8834e1798

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for jsonlib-1.3.7-py2.5-linux-i686.egg
Algorithm Hash digest
SHA256 e1cb066627225471aa9a6be5c9665b3a2f89e5c4718573a4f26057eeed4b04b7
MD5 ee5e8a45ec064f9c90d159fc7358cee5
BLAKE2b-256 e34ba23cec96aa0572d894516c07a7d222fd22552c867f902e1f473e0adefc7a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for jsonlib-1.3.7-py2.4-win32.egg
Algorithm Hash digest
SHA256 bdb77d08b9104a565d6f06c3597d34dd4fd4e45b10db9795bcda30bcc104840d
MD5 009665f443f730a6a08c1503fce77ecb
BLAKE2b-256 5f9dec958b0e248cf4ec5e8ea44c11d4b4afa414538e9e18de9937866cf14159

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for jsonlib-1.3.7-py2.4-macosx-10.3-fat.egg
Algorithm Hash digest
SHA256 8256d409fad016bb90c90e77dc574f414e67febb5f0dc294c9fbac5d9a7a7cb4
MD5 cc5160912af61de0adce47012b09a8e1
BLAKE2b-256 51bb9ac0b130d9945d807123349774ce41a6a5f3b2aaebdd5dd8d7d0febb3b71

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for jsonlib-1.3.7-py2.4-linux-i686.egg
Algorithm Hash digest
SHA256 479481a863a363d28b677671a27f5441ef656d10edd322b31c3662f25b4234f3
MD5 2b9bb675e2af9fe2fa5e0f5494ea0eea
BLAKE2b-256 e655702e1b77c11e0f06b0e4f088b2ef2c9c768654dc008708a3d59196f1ebb4

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