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
|