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

Document how to pretty print json? #273

Open
pmorch opened this issue Dec 29, 2024 · 0 comments
Open

Document how to pretty print json? #273

pmorch opened this issue Dec 29, 2024 · 0 comments

Comments

@pmorch
Copy link

pmorch commented Dec 29, 2024

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.

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

No branches or pull requests

1 participant