Json_serializable.dart: Allow non-String map keys, if there exists a compatible converter

Created on 26 Feb 2019  路  3Comments  路  Source: google/json_serializable.dart

Map<Version, int> should be doable...

P2 medium enhancement

Most helpful comment

@bgetsug 鈥撀爎ight now, no. I've wired through support for known types.

Getting this generic for an arbitrary key type will be...interesting. 馃槃 I think I can pull it off, though.

All 3 comments

Or Map<int, int> for that matter.

I'd really like to be able to use value objects in place of Strings to make my Maps a bit more clear. When this feature is complete will I be able to do something like the following?

@JsonSerializable()
class ContainerThing {
  Map<ThingId, int> stuff;
}

class ThingId extends StringValue {
  ThingId(String value) : super(value);
}

class StringValue extends Value<String> {
  StringValue(String value) : super(value);
}

class StringValueConverter implements JsonConverter<StringValue, String> {
  @override
  StringValue fromJson(String json) => StringValue(json);

  @override
  String toJson(StringValue object) => object.value;
}

@immutable
abstract class Value<T> {
  final T value;

  Value(this.value);

  @override
  bool operator ==(Object other) =>
      identical(this, other) ||
      other is Value &&
          runtimeType == other.runtimeType &&
          value == other.value;

  @override
  int get hashCode => value.hashCode;
}

@bgetsug 鈥撀爎ight now, no. I've wired through support for known types.

Getting this generic for an arbitrary key type will be...interesting. 馃槃 I think I can pull it off, though.

Was this page helpful?
0 / 5 - 0 ratings