google / google/json_serializable.dart

Support a CustomMap

Open
#1,481 1 comment 1 reaction 0 assignees View on GitHub
Type: enhancement
Dominant language
Dart
Stars
1.6k
Forks
461
Avg merge
45m
Merged PRs (30d)
1

Description

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](https://github.com/google/json_serializable.dart/blob/master/example/lib/tuple_example.dart) to convert a custom Map to json and back (IMap in [fast_immutable_collections](https://github.com/marcglasberg/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 {
Map dummy;
CustomMap(
this.dummy,
);

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

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

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.