Pretty printing::
>>> import json
- >>> print(json.dumps({'4': 5, '6': 7}, sort_keys=True, indent=4))
+ >>> print(json.dumps({'6': 7, '4': 5}, sort_keys=True, indent=4))
{
"4": 5,
"6": 7
}
+Specializing JSON object encoding::
+
+ >>> import json
+ >>> def custom_json(obj):
+ ... if isinstance(obj, complex):
+ ... return {'__complex__': True, 'real': obj.real, 'imag': obj.imag}
+ ... raise TypeError(f'Cannot serialize object of {type(obj)}')
+ ...
+ >>> json.dumps(1 + 2j, default=custom_json)
+ '{"__complex__": true, "real": 1.0, "imag": 2.0}'
+
Decoding JSON::
>>> import json