google / google/json_serializable.dart

json_serializable _$*EnumMap generation

Open
#1,377 4 comments 2 reactions 0 assignees View on GitHub
Dominant language
Dart
Stars
1.6k
Forks
461
Avg merge
45m
Merged PRs (30d)
1

Description

When `json_serializable` is generating code to serialize an `enum` values it creates a private const map named `_$EnumMap`.

However this map is being generated not for the dart source file where this enum is declared at, but in the sources this enum is referenced in. Basically the same map is generated for every source file that is referencing that same `enum` type.

This adds some code duplication even when this enum is serialized the same way in all places.

Additionally, if the model where this enum is declared at is coming from a different module (e.g. a common data model or some common API) we have to regenerate all generated code in all modules that depend on the module where enum is coming from.

To address those two issues it would be great if `json_serializable` could generate a public map or a common public method that would be used from the generated code that serializes enum value in all other places.

Using:
```
Dart SDK version: 3.2.0
json_annotation: 4.8.1
json_serializable: 6.7.1
```

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.