-
Notifications
You must be signed in to change notification settings - Fork 34
Alias
Kevin Segaud edited this page Jun 13, 2018
·
6 revisions
Field
configuration property of GenSerializer
allows aliasing serialization and deserialization of keys separately using encodeTo
and decodeFrom
proeprties. Alternatively, Alias
syntactic sugar can be used to alias both serialization and deserialization keys to same value.
Lets dig into an example. Suppose this is our model:
class User {
String name;
int age;
}
We would like to alias name
as Name
:
@GenSerializer(
fields: {
'name': Alias('Name'),
}
)
class UserSerializer extends Serializer<User> with _$UserSerializer {
}
Not: Alias
is just a syntactic sugar of Field
to make aliasing easier.
One can directly declare Alias
on the model's field:
class User {
@Alias('Name')
String name;
int age;
}
@GenSerializer()
class UserSerializer extends Serializer<User> with _$UserSerializer {
}