google / json_serializable.dart

Generates utilities to aid in serializing to/from JSON.
https://pub.dev/packages/json_serializable
BSD 3-Clause "New" or "Revised" License
1.55k stars 395 forks source link

Add an option for `jsonSerializable` annotation to choose what `enum` property `name` or `index` to use for serialization. #1290

Open bambinoua opened 1 year ago

bambinoua commented 1 year ago

Now generated code contains such statement for ThemeMode enum:

const _$ThemeModeEnumMap = {
  ThemeMode.system: 'system',
  ThemeMode.light: 'light',
  ThemeMode.dark: 'dark',
};

So enum values are serialized as Strings but having a possibility to serialize enum values as integers would be a good option.

const _$ThemeModeEnumMap = {
  ThemeMode.system: 0,
  ThemeMode.light: 1,
  ThemeMode.dark: 2,
};
kevmoo commented 1 year ago

This is supported by Enums owned by the author. It'd be a new feature to add it for unowned enums.