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

Parse JSON without modifying the dataclass #515

Open
mnieber opened this issue Feb 2, 2024 · 1 comment
Open

Parse JSON without modifying the dataclass #515

mnieber opened this issue Feb 2, 2024 · 1 comment
Labels
enhancement New feature or request

Comments

@mnieber
Copy link

mnieber commented Feb 2, 2024

Description

I'm always frustrated when a solution could use a function, but instead relies on creating or modifying classes.

Possible solution

Allow a functional approach to parsing a json, e.g.

@dataclass
class Person:
    name: str

person = parse(
    data=person_json, 
    cls=Person,
    encoders=dict(
        name=encode_name
    ),
    validators=dict(
        name=validate_name
    )
)

An additional benefit is that I can flexibly choose the encoders and validators (they are not baked into my Person data-structure)

person = parse(
    data=person_json_in_format_a, 
    cls=Person,
    encoders=format_a_encoders,
    validators=format_a_validators
)

Alternatives

I've looked into various solutions based on Pydantic. I haven't found any nice ones (dataclasses-json looks like it might be the nicest one so far).

Context

No response

@mnieber mnieber added the enhancement New feature or request label Feb 2, 2024
@USSX-Hares
Copy link
Collaborator

Duplicate of #442

@USSX-Hares USSX-Hares marked this as a duplicate of #442 Feb 11, 2024
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