Skip to content

Json encode from __repr__, __str__ or __serialize__ when available #114285

@pmithrandir

Description

@pmithrandir

Hello,

When I need to json encode a new object or an Enum , I need to either extend str, or build a json decoder to make it compatible with the default json encoder.

When using third parties lib, it might create some difficulties as implementing string is not an option, and the dumps method is not reachable.

If I wanted to do that in java, I would implement in my class a serialize method that would be called by json serializer.

Would it be possible, in the encoder, to allow the usage of either a serialize(self) method, or to directly use repr or str when the object after all other option failed.

if isinstance(o, str):

That will give the final developer a reliable way to explain the json encoder how to interpret the object and serialize it.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions