Skip to content
Ravi Teja Gudapati edited this page Jun 10, 2018 · 3 revisions

In this example, object Address is embedded in the object Person:

class Address {
  String street;
  String city;
}

class Person {
  String name;
  Address address;
}

Given the following serializers, Jaguar automatically detects that AddressSerializer serializer field address in Person.

@GenSerializer()
class AddressSerializer extends Serializer<Address> with _$AddressSerializer {
}

@GenSerializer()
class PersonSerializer extends Serializer<Person> with _$PersonSerializer {
}

Quite smart!

Note that AddressSerializer must visible in the current scope (either declared in the same library or imported).

But what if there are multiple seralizers that serialize Address. Lets say, AddressSerializer1 and AddressSerializer2. Use serializers configuration property of GenSerializer to explicitly specify which serializer to use.

@GenSerializer(
  'serializers': [AddressSerializer2],
)
class PersonSerializer extends Serializer<Person> with _$PersonSerializer {
}

Pretty neat!