addict
addict copied to clipboard
implement json converter and representation
Hi, First, I would like to say thanks for this amazing library! Very useful.
While developing on some of my projects, I came across a few times into situations where the convenience offered by this library was taken away because it was tedious to ensure JSON serializable items deeply nested in the definitions.
To resolve this, I came across the approach presented in this PR. I would like to contribute it. There are 2 additions:
-
method
json
that specifically ask theDict
(or its derived class) to call JSON conversion. This is a bit similar toto_dict
method, but specifically looks forjson
method or properly in nested items. This method name is often employed, for example, inrequests
library items,pyramid
,flask
and many others. It also enforcesstr
conversion of un-serializable items, such that the generated content is valid forjson.dumps
use. Any sub-class not directly JSON serializable only needs to implementjson
and will automatically benefit from the resolution. -
method
__repr__
combined with__json__
option. When activated, the representation of theDict
will be a "pretty print" representation (as JSON) of the contents. The module and name of the class is also displayed before the JSON representation to indicate it was aDict
or derived class.
I'm also open to further improvements if needed. Feedback welcomed.