JsonConverter<FieldType, JsonType> class abstract

Interface that custom converters can use to register a convertation between 2 types

This can be used both for external classes and Enum convertions

// enum example
enum Category {
  sports,
  news,
  celebrity;
}

class EnumCategoryConverter implements JsonConverter<Category, String> {
  const EnumCategoryConverter();

  @override
  String toJson(Category value, Map<dynamic, dynamic> json, String keyName) => value.name;

  @override
  Category fromJson(String value) {
    return Category.values.firstWhere((c) => c.name == value);
  }
}

void main() {
  // register converter
  jsonConverterRegistrant
    .register(const EnumCategoryConverter());
}

Constructors

JsonConverter()

Properties

hashCode int
The hash code for this object.
no setterinherited
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

fromJson(JsonType value, Map json, String keyName) → FieldType
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toJson(FieldType value) → JsonType
toString() String
A string representation of this object.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited