Why class Dict(dict) is json serializable (json.dumps(Dict) works), but class Dict(collections.UserDict)is not?
CodePudding user response:
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
