I have a Map<Id, int> whose key is a custom class. That class is is annotated with @JsonConverter for converting it to a String. When I run json_serializable over my code, I get the error
[project]: [SEVERE] json_serializable on lib/src/cart.dart:
[project]:
[project]: Could not generate `toJson` code for `items` because of type `Id`.
[project]: Map keys must be one of: Object, dynamic, enum, String, BigInt, DateTime, int, Uri.
Now Id is not one of those types, but by default it will serialise to a String. So should this use can be supported? More info/example below:
❯ dart --version
Dart SDK version: 3.2.5 (stable) (Tue Jan 16 15:02:13 2024 +0000) on "macos_arm64"
@JsonSerializable()
final class Cart {
final Map<Id, int> items;
}
@JsonSerializable()
@IdJsonConverter()
final class Id implements Comparable<Id> {
...
}
final class IdJsonConverter extends JsonConverter<Id, String> {
const IdJsonConverter();
@override
Id fromJson(String json) {
return Id.fromString(json);
}
@override
String toJson(Id object) {
return object.toString();
}
}
I have a
Map<Id, int>
whose key is a custom class. That class is is annotated with@JsonConverter
for converting it to a String. When I run json_serializable over my code, I get the errorNow
Id
is not one of those types, but by default it will serialise to a String. So should this use can be supported? More info/example below:Map keys must be one of: