json_serializable.dart icon indicating copy to clipboard operation
json_serializable.dart copied to clipboard

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

Open bambinoua opened this issue 1 year ago • 1 comments

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,
};

bambinoua avatar Feb 24 '23 09:02 bambinoua

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

kevmoo avatar Feb 26 '23 23:02 kevmoo