Package Products :: Package ZenUtils :: Module jsonutils |
[hide private]
[frames] | no frames]

# Module jsonutils

source code

Classes | [hide private]
StringifyingDecoder Casts all unicode objects as strings.
JavaScript A simple class that represents a JavaScript literal that should not be JSON encoded.
JavaScriptRegex A simple class that represents a JavaScript Regex literal that should not be JSON encoded.
ObjectEncoder
JavaScriptEncoder A JavaScript encoder based on JSON.

Functions | [hide private]
_recursiveCaster(ob) | source code
_sanitize_value(value, errors='replace') JSONEncoder doesn't allow overriding the encoding of built-in types (in particular strings), and allows specifying an encoding but not a policy for errors when decoding strings to UTF-8. | source code
str, func |
json(value, **kw) Serialize `value` into a JSON string. | source code
javascript(value) A JavaScript encoder based on JSON. | source code
unjson(value, **kw) Create the Python object represented by the JSON string `value`. | source code

Function Details | [hide private]

### _sanitize_value(value, errors='replace') | source code
JSONEncoder doesn't allow overriding the encoding of built-in types (in particular strings), and allows specifying an encoding but not a policy for errors when decoding strings to UTF-8. This function replaces all strings in a nested collection with unicode strings with 'replace' as the error policy.

### json(value, **kw) | source code
Serialize `value` into a JSON string. If `value` is callable, a decorated version of `value` that serializes its return value will be returned.
    >>> value = (dict(a=1L), u"123", 123)
    >>> print json(value)
    [{"a": 1}, "123", 123]
    >>> @json
    ... def f():
    ...     return value
    ...
    >>> print f()
    [{"a": 1}, "123", 123]
    >>> from array import array
    >>> a1 = array('i', list(range(10)))
    >>> a2 = array('c', 'XYZZY')
    >>> a3 = (array('u',[unichr(i) for i in range(250,260)]))
    >>> [json(s) for s in (a1, a2, a3)]
    ['[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]', '"XYZZY"', '"\\u00fa\\u00fb\\u00fc\\u00fd\\u00fe\\u00ff\\u0100\\u0101\\u0102\\u0103"']
    >>> json([a1, a2, a3])
    '[[0, 1, 2, 3, 4, 5, 6, 7, 8, 9], "XYZZY", "\\u00fa\\u00fb\\u00fc\\u00fd\\u00fe\\u00ff\\u0100\\u0101\\u0102\\u0103"]'
    >>> json({'properties' : [{ 'key' : 'a1', 'value' : a1 },{ 'key' : 'a2', 'value' : a2 },{ 'key' : 'a3', 'value' : a3 },] })
    '{"properties": [{"value": [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], "key": "a1"}, {"value": "XYZZY", "key": "a2"}, {"value": "\\u00fa\\u00fb\\u00fc\\u00fd\\u00fe\\u00ff\\u0100\\u0101\\u0102\\u0103", "key": "a3"}]}'
Parameters: `value` (dict, list, tuple, str, etc. or callable) - An object to be serialized Returns: str, func The JSON representation of `value` or a decorated function

### javascript(value) | source code
A JavaScript encoder based on JSON. It encodes like normal JSON except it passes JavaScript objects un-encoded.

### unjson(value, **kw) | source code
Create the Python object represented by the JSON string `value`.
    >>> jsonstr = '[{"a": 1}, "123", 123]'
    >>> print unjson(jsonstr)
    [{'a': 1}, '123', 123]
Parameters: `value` (str) - A JSON string Returns: The object represented by `value`