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 · 1 comment
Open

Document how to pretty print json? #273

pmorch opened this issue Dec 29, 2024 · 1 comment
Labels
enhancement New feature or request

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.

@Fatal1ty Fatal1ty added the enhancement New feature or request label Jan 21, 2025
@Fatal1ty
Copy link
Owner

Fatal1ty commented Jan 21, 2025

Hi @pmorch

Thank you for your comment! You are right, there is currently no documentation on the initialization capabilities of encoders and decoders. I thought this feature was only necessary for power users who would find the answer themselves. You did it well :) I will add more information in the documentation. Perhaps it would not be superfluous to add instructions on how to create your own mixins for other serialization formats or in order to cover the case with pretty formatting.

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

No branches or pull requests

2 participants