Pyjo.JSON - Minimalistic JSON

from Pyjo.JSON import decode_json, encode_json

bstring = encode_json({'foo': [1, 2], 'bar': 'hello!', 'baz': True})
dictionary = decode_json(bstring)

Pyjo.JSON is an implementation of RFC 7159 based on json module.

It supports normal Python data types like numbers, strings, booleans, lists, tuples and dicts and will try to call the to_json() method or dict() or list() function on objects, or stringify them if it doesn’t exist.

The two Unicode whitespace characters u2028 and u2029 will always be escaped to make JSONP easier, and the character / to prevent XSS attacks.

u"\u2028\u2029</script>" -> b'"\\u2028\\u2029<\\/script>"'

Other charaters will be encoded with utf-8 encoding without escaping.

{'i': u"♥ pyjo"} -> b'{"i":"\xe2\x99\xa5 pyjo"}'

Classes

Pyjo.JSON.decode_json(bstring)
value = decode_json(bstring)

Decode JSON to Python value and die if decoding fails.

Pyjo.JSON.encode_json(value)
bstring = encode_json({'i': u'♥ pyjo'})

Encode Python value to JSON.

Pyjo.JSON.from_json(string)
value = from_json(string)

Decode JSON text that is not utf-8 encoded to Python value and die if decoding fails.

Pyjo.JSON.j(obj)
bstring = j([1, 2, 3])
bstring = j({'i': u'♥ pyjo'})
value = j(bstring)

Encode Python data structure (which may only be dict or list or tuple) or decode JSON, an None return value indicates a bare null or that decoding failed.

Pyjo.JSON.to_json(value)
string = to_json({'i': u'♥ pyjo'}

Encode Python value to JSON text without utf-8 encoding it.