Let's say I'm building an app that receives a json as a response, for example:
{
"a": 5,
"b": [1, 2, 3]
}
Let's say I want to parse this json into the following class:
class Example {
String a;
List<String> b;
}
Using Dart's json_serializable package I can do the following
String intToString(int value) => value.toString();
List<String> intToStringList(List<int> value) => value.map(intToString).toList();
@JsonSerializable()
class Example {
@JsonKey(fromJson: intToString)
String a;
@JsonKey(fromJson: intToStringList)
List<String> b;
Example(this.a, this.b);
factory Example.fromJson(Map<String, dynamic> json) => _$ExampleFromJson(json);
Map<String, dynamic> toJson() => _$ExampleToJson(this);
}
My question is if there is any built in, more elegant way to do this without custom method implementations.