couchdb.json

Package couchdb :: Module json |

# Module json

Thin abstraction layer over the different available modules for decoding and encoding JSON data.

This module currently supports the following JSON modules:
  - `simplejson`: http://code.google.com/p/simplejson/
  - `cjson`: http://pypi.python.org/pypi/python-cjson
  - `json`: This is the version of `simplejson` that is bundled with the Python standard library since version 2.6 (see http://docs.python.org/library/json.html)

The default behavior is to use `simplejson` if installed, and otherwise fallback to the standard library module. To explicitly tell CouchDB-Python which module to use, invoke the use() function with the module name:

    from couchdb import json
    json.use('cjson')

In addition to choosing one of the above modules, you can also configure CouchDB-Python to use custom decoding and encoding functions:

    from couchdb import json
    json.use(decode=my_decode, encode=my_encode)

Functions
object |
decode(string) Decode the given JSON string. |
basestring |
encode(obj) Encode the given object as a JSON string. |
use(module=None, decode=None, encode=None) Set the JSON library that should be used, either by specifying a known module name, or by providing a decode and encode function. |

Function Details

### decode(string) |
Decode the given JSON string. Parameters: `string` (basestring) - the JSON string to decode Returns: object the corresponding Python data structure

### encode(obj) |
Encode the given object as a JSON string. Parameters: `obj` (object) - the Python data structure to encode Returns: basestring the corresponding JSON string

### use(module=None, decode=None, encode=None) |
Set the JSON library that should be used, either by specifying a known module name, or by providing a decode and encode function. The modules "simplejson", "cjson", and "json" are currently supported for the `module` parameter. If provided, the `decode` parameter must be a callable that accepts a JSON string and returns a corresponding Python data structure. The `encode` callable must accept a Python data structure and return the corresponding JSON string. Exceptions raised by decoding and encoding should be propagated up unaltered. Parameters: `module` (str or module) - the name of the JSON library module to use, or the module object itself `decode` (callable) - a function for decoding JSON strings `encode` (callable) - a function for encoding objects as JSON strings