Why class Dict(dict)
is json serializable (json.dumps(Dict)
works), but class Dict(collections.UserDict)
is not?
You can look at the source code for JSONEncoder and specifically this line. It checks for dict
and collections.UserDict
is not instance of dict
.
The Encodersupports the following objects and types by default:
+-------------------+---------------+
| Python | JSON |
+===================+===============+
| dict | object |
+-------------------+---------------+
| list, tuple | array |
+-------------------+---------------+
| str | string |
+-------------------+---------------+
| int, float | number |
+-------------------+---------------+
| True | true |
+-------------------+---------------+
| False | false |
+-------------------+---------------+
| None | null |
+-------------------+---------------+
You can extend it to handle collections.UserDict
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With