Pyjo.JSON - Minimalistic JSON¶
from Pyjo.JSON import decode_json, encode_json
data = encode_json({'foo': [1, 2], 'bar': 'hello!', 'baz': True})
value = decode_json(data)
Pyjo.JSON is a minimalistic pure-Python implementation of RFC 8259.
It supports normal Python data types like dict, list,
tuple, str, numbers, booleans and None, and will try to
call the __json__ method on other objects, or stringify them if their class
has its own __str__. Anything else becomes null. Strings and numbers are
told apart by their types.
[1, -2, 3] -> [1, -2, 3]
{"foo": "bar"} -> {'foo': 'bar'}
The keys of an object keep the order of the dict, or are sorted with
sort_keys.
{'b': 1, 'a': 2} -> {"b":1,"a":2}
{'b': 1, 'a': 2} -> {"a":2,"b":1} # sort_keys=True
Literal names will be translated to and from the native Python values, which
the constants true and false hold too.
true -> True
false -> False
null -> None
The characters / and < will always be escaped to prevent XSS attacks,
so that JSON can be put into an element script of HTML as it is.
"</script>" -> "\u003C\/script>"
"<!--<script>" -> "\u003C!--\u003Cscript>"
For better performance the json module of the standard library will be
used, set up like the pure-Python implementation, with error messages of its
own. This can also be disabled with the PYJO_NO_JSON_STDLIB environment
variable.
See also¶
Pyjoyment, https://mojolicious.org.
Ported from Mojo::JSON of Mojolicious 9.49.
Functions¶
- Pyjo.JSON.decode_json(data: bytes | bytearray | str) Any[source]¶
value = decode_json(data)
Decode JSON to Python value and raise
ValueErrorif decoding fails. Bytes areUTF-8, orUTF-16orUTF-32, with a byte order mark or without one, asjson.loads()detects them, where a byte order mark is not a part of the JSON.# {'a': 1} decode_json(b'\xff\xfe{\x00"\x00a\x00"\x00:\x001\x00}\x00')
- Pyjo.JSON.default(value: object) Any[source]¶
data = json.dumps(value, default=Pyjo.JSON.default)
Turn an object that is not a
dict,list,tuple,str, number, boolean orNoneinto one that is, asPyjo.JSONdoes: with its__json__method, as a string if its class has its own__str__, or asNone. Bytes become a string of the characters 0-255. This is thedefaultfunction for the encoder of thejsonmodule of the standard library.# '{"url": "http://example.com"}' json.dumps({'url': Pyjo.URL.new('http://example.com')}, default=Pyjo.JSON.default)
- Pyjo.JSON.encode_json(value: object, *, sort_keys: bool = False) bytes[source]¶
data = encode_json({'i': '♥ pyjoyment'}) data = encode_json({'i': '♥ pyjoyment'}, sort_keys=True)
Encode Python value to JSON, with the keys of an object in the order of the
dict, or sorted withsort_keys.
- Pyjo.JSON.false = False¶
from Pyjo.JSON import false
False value, the same as
False.
- Pyjo.JSON.from_json(chars: str) Any[source]¶
value = from_json(chars)
Decode JSON text that is not
UTF-8encoded to Python value and raiseValueErrorif decoding fails.
- Pyjo.JSON.j(value: list[Any] | tuple[Any, ...] | dict[Any, Any], *, sort_keys: bool = False) bytes[source]¶
- Pyjo.JSON.j(value: bytes | bytearray | str, *, sort_keys: bool = False) Any
data = j([1, 2, 3]) data = j({'i': '♥ pyjoyment'}) data = j({'i': '♥ pyjoyment'}, sort_keys=True) value = j(data)
Encode Python data structure (which may only be a
list, atupleor adict) or decode JSON, aNonereturn value indicates a barenullor that decoding failed.sort_keyssorts the keys of the objects of the JSON that it encodes.
- Pyjo.JSON.to_json(value: object, *, sort_keys: bool = False) str[source]¶
chars = to_json({'i': '♥ pyjoyment'}) chars = to_json({'i': '♥ pyjoyment'}, sort_keys=True)
Encode Python value to JSON text without
UTF-8encoding it, with the keys of an object in the order of thedict, or sorted withsort_keys.
- Pyjo.JSON.true = True¶
from Pyjo.JSON import true
True value, the same as
True.