Skip to content

Support a CustomMap #1481

Open
Open
@JoanSchi

Description

@JoanSchi

Support for the serialization of a custom map, with the same key Type support as map is missing.

At the moment we use the implementation from the tuple_example to convert a custom Map to json and back (IMap in fast_immutable_collections) In the tuple example we use toJsonT for the key Type conversion, however for example an integer is returned as a integer and not as a String, which makes sense for a value in a tuple, but not for a key in a json map.

A similar implementation of the tuple_example but with support for a key conversion toJsonK with the same Type support as Map, would be great.

class CustomMap<K, V> {
  Map<K, V> dummy;
  CustomMap(
    this.dummy,
  );

  factory CustomMap.fromJson(
    Map<String, dynamic> json,
    K Function(String) fromJsonK,
    V Function(Object?) fromJsonV,
  ) =>
      CustomMap(json.map<K, V>(
          (key, value) => MapEntry(fromJsonK(key), fromJsonV(value))));

  Map<String, dynamic> toJson(
          String Function(K) toJsonK, Object? Function(V) toJsonV) =>
      dummy.map((key, value) => MapEntry(toJsonK(key), toJsonV(value)));
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions