Skip to content

Document how to pretty print json? #273

Open
@pmorch

Description

@pmorch

Is your feature request related to a problem? Please describe.
There are cases where it makes sense to want to indent JSON.

Describe the solution you'd like
I'd like to request that this be documented.

Example using the type and instance from README.md's Usage example

standard = json_codec.encode(portfolio, Portfolio)
print(f"standard:\n{standard}")

pretty = json_codec.encode(
    portfolio,
    Portfolio,
    post_encoder_func=lambda o: json.dumps(o, sort_keys=True, indent=4)
)
print(f'pretty:\n{pretty}')

Outputs:

standard:
{"currencies": [{"currency": "USD", "balance": 238.67}, {"currency": "EUR", "balance": 361.84}], "stocks": [{"ticker": "AAPL", "name": "Apple", "balance": 10}, {"ticker": "AMZN", "name": "Amazon", "balance": 10}]}

pretty:
{
    "currencies": [
        {
            "balance": 238.67,
            "currency": "USD"
        },
        {
            "balance": 361.84,
            "currency": "EUR"
        }
    ],
    "stocks": [
        {
            "balance": 10,
            "name": "Apple",
            "ticker": "AAPL"
        },
        {
            "balance": 10,
            "name": "Amazon",
            "ticker": "AMZN"
        }
    ]
}

And perhaps just having this issue is documentation enough. It would have allowed me to find it I think. I'll let you decide. Just closing it would be fine with me.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions