Skip to content
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.

Alternative -> need version 2.1.2 or above

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 {
}