Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement json converter and representation #139

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

fmigneault
Copy link

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 the Dict (or its derived class) to call JSON conversion.
    This is a bit similar to to_dict method, but specifically looks for json 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 for json.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.

@coveralls
Copy link

coveralls commented May 1, 2021

Coverage Status

Coverage remained the same at 100.0% when pulling 31f07b8 on fmigneault:json into 75284f9 on mewwts:master.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants