Skip to content
Ravi Teja Gudapati edited this page Jun 10, 2018 · 1 revision

Rename fields

Encode/Decode

Use the EnDecode to decode and encode using you own key

@GenSerializer(
  fields: const {
    'zipcode': const EnDecode('zip')
  }
)
class AddressSerializer extends Serializer<Address> with _$AddressSerializer {

Encode

Use the EncodeOnly to use you own key only during encoding

@GenSerializer(
  fields: const {
    'zipcode': const EncodeOnly('zip')
  }
)
class AddressSerializer extends Serializer<Address> with _$AddressSerializer {

Decode

Use the DecodeOnly to use you own key only during encoding

@GenSerializer(
  fields: const {
    'zipcode': const DecodeOnly('zip')
  }
)
class AddressSerializer extends Serializer<Address> with _$AddressSerializer {

Ignore

Use the ignore parameters list or Ignore class to ignore a field.

@GenSerializer(
  ignore: const [ 'ignore' ],
  fields: const {
    'zipcode': const Ignore()
  }
)
class AddressSerializer extends Serializer<Address> with _$AddressSerializer {

Field Processor

If you need to process a field to convert it manually, for example a DateTime to an iso8601 String, implement FieldProcessor

class DateTimeProcessorIso implements FieldProcessor<DateTime, String> {
  const DateTimeProcessorIso();

  DateTime deserialize(String input) {
    return DateTime.parse(input);
  }

  String serialize(DateTime value) {
    return value?.toIso8601String();
  }
}

Then use it on your Serializer declaration by providing the symbol of the field you want to process

@GenSerializer(
  fields: const {
    'birthday': const EnDecode(processor: const DateTimeProcessorIso())
  }
)
class UserSerializer extends Serializer<User> {
...

Inner Class

For example an User class that contain an Address

class User {
  String name;
  Address address;
}

Declare the UserSerializer class and provide the serializer to the Address class.

@GenSerializer(
  serializers: const [
    AddressSerializer
  ]
)
class UserSerializer extends Serializer<User> with _$UserSerializer {
}