Fatal1ty / mashumaro

Fast and well tested serialization library
Apache License 2.0
751 stars 44 forks source link

Add an alternative way to assign a field alias with annotations #214

Closed Fatal1ty closed 4 months ago

Fatal1ty commented 4 months ago

Is your feature request related to a problem? Please describe. The current way to assign an alias to a field is to use field metadata:

@dataclass
class Foo:
    a: str = field(metadata={"alias": "alias_a"})
    b: str = field(metadata=field_options(alias="alias_b"))

This can be written in alternative shorter way.

Describe the solution you'd like Add a new Alias class and use it as an annotation:

@dataclass
class Foo:
    a: Annotated[str, Alias("alias_a")]
    b: Annotated[str, Alias("alias_b")]

Describe alternatives you've considered An alternative can be a la Meta class with an alias attribute. Nothing prevents us from adding such a class in the future and duplicating the functionality in it.