implement json converter and representation #139
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 to
to_dict
method, but specifically looks forjson
method or properly in nested items.This method name is often employed, for example, in
requests
library items,pyramid
,flask
and many others.It also enforces
str
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 implement
json
and will automatically benefit from the resolution.method
__repr__
combined with__json__
option.When activated, the representation of the
Dict
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 a
Dict
or derived class.I'm also open to further improvements if needed. Feedback welcomed.