0

Consider the following dart class using freezed:

@freezed
class Example with _$Example {
  const factory Example(
          {String? fieldA, int? fieldB}) = _Example;

  factory Example.fromJson(Map<String, dynamic> json) => _$ExampleFromJson(json);
}

If you were to deserialize the following json:

  {
    "fieldA": "test",
    "fieldB": 9,
    "fieldC": 100
  }

and then serialize it, it would end up like this:

  {
    "fieldA": "test",
    "fieldB": 9
  }

How can I preserve fieldC or any unknown field upon deserialization? In C# there is an annotation called ExtensionData (either for system.text or for newtonsoft) that held all the unserialized data. Is there an equivalent in freezed/json_serializable? If not, is there some way to workaround this?

Ozaner Hansha
  • 101
  • 1
  • 2

0 Answers0