Thanks to visit codestin.com Credit goes to github.com
We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
json.dumps(..., default=)
1 parent 891236f commit ac31f71Copy full SHA for ac31f71
Doc/library/json.rst
@@ -54,12 +54,23 @@ Compact encoding::
54
Pretty printing::
55
56
>>> import json
57
- >>> print(json.dumps({'4': 5, '6': 7}, sort_keys=True, indent=4))
+ >>> print(json.dumps({'6': 7, '4': 5}, sort_keys=True, indent=4))
58
{
59
"4": 5,
60
"6": 7
61
}
62
63
+Specializing JSON object encoding::
64
+
65
+ >>> import json
66
+ >>> def custom_json(obj):
67
+ ... if isinstance(obj, complex):
68
+ ... return {'__complex__': True, 'real': obj.real, 'imag': obj.imag}
69
+ ... raise TypeError(f'Cannot serialize object of {type(obj)}')
70
+ ...
71
+ >>> json.dumps(1 + 2j, default=custom_json)
72
+ '{"__complex__": true, "real": 1.0, "imag": 2.0}'
73
74
Decoding JSON::
75
76
0 commit comments