pyserde
pyserde copied to clipboard
Support for aliasing on dataclass fields
Similarly to field attributes in the serde.rs crate it would be very useful to be able to describe multiple aliases for the same field, that way you're not stuck if multiple names exist while de-serializing.
Something like this would be useful
@dataclass
@serde
class Foo:
bar_field: int
name_field: str = field(alias = ["first_alias", "second_alias", "third_alias" ]
The syntax is definitely up for debate, since in the serde library it's `#[serde(alias = "one", alias = "two", alias = "three"] which I don't think is allowed in python.
This change would allow the deserialization of something like
[
{
"bar_field": 1,
"first_alias": "Would work"
},
{
"bar_field": 2,
"second_alias": "Also works"
},
{
"bar_field": 3,
"third_alias": "And these all deserialize correctly"
}
]
to deserialize all into the same Foo objects.
It also makes the library really flexible when you want to make different names more than once, such as using a more descriptive name on swagger docs.
Hi, @Exiled1 Thanks for being interested in pyserde!
These aliases would be used only during deserialization? 🤔 Do you know what field name will be used during serialization? 🤔
Why hello there @yukinarit! I definitely took an interest after seeing how much nicer to work with pyserde is than serde (other py library)
And yes, these aliases should only be used during serialization into a struct/dataclass.
And the field name used during serialization should still be the name defined in the original field, or whatever's been specified by the field(rename=...) method.
When serialized the result of the above json would be
(Pardon my lack of code blocks, I'm on mobile)
Foo(bar_field: 1, name_field: "works")
Foo(bar_field: 2, name_field: "works")
Foo(bar_field: 3, name_field: "works")
And this would be overridden by the rename flag during serialization. So if you have something like
foo_field: str = field (rename="bar_field", alias=["foobar_field", "barfoo_field", "fizz_field"])
You could have a json use any of the names within alias as well as rename, however the deserialized version would look like
{ "bar_field": "stuff" }
With obviously no change to the program representation of the struct 😄
Essentially this change would just allow you to be more flexible with your json specifications so someone doesn't run into the issues of the developer wanting to support different ways of formatting their json files.
It would make it so the developer could have name_field be written like nameField, NameField, Name_Field, etc. If they deem an alias to be reasonable enough to support.
Essentially the field rename option should be used to rename the deserialized output, while the field alias option would be used to allow multiple formats during serialization.
@Exiled1 alias was implemented in pyserde 0.9.5 🙂
Wow! That was really fast! Thank you for letting me know!!! I'll definitely have to look at your implementation to see how to potentially contribute :)