Json_serializable.dart: Add kebab- and snake-case options to entire enum via annotation

Created on 27 Aug 2018  路  5Comments  路  Source: google/json_serializable.dart

Now that the great feature of specifying a FieldRename strategy to a JsonSerializable class has been added, it would be great if the same functionality could be used on enums.

If I'm not mistaken, right now, the only way to do it, is like this:

enum RoutingCodeType {
  @JsonValue('sort_code')
  sortCode,
  @JsonValue('bsb_code')
  bsbCode
 }

Something like this would be great:

@JsonSerializableEnum(fieldRename: FieldRename.snake)
enum RoutingCodeType {
  sortCode,
  bsbCode
 }
P3 low help wanted enhancement

Most helpful comment

What if it inherited fieldRename from the parent serializable class?

@JsonSerializable(fieldRename: FieldRename.snake)
class MyClass {
  final MyEnum myEnum; // my_enum

  /// ...
}

enum MyEnum {firstField, secondField} // first_field, second_field

All 5 comments

Low-pri for me. If you want to implement it, I'm happy to review.

I can have a look. Would you introduce a new annotation class that can be added to enums, like JsonSerializableEnum?

What if it inherited fieldRename from the parent serializable class?

@JsonSerializable(fieldRename: FieldRename.snake)
class MyClass {
  final MyEnum myEnum; // my_enum

  /// ...
}

enum MyEnum {firstField, secondField} // first_field, second_field

What if it inherited fieldRename from the parent serializable class?

@JsonSerializable(fieldRename: FieldRename.snake)
class MyClass {
  final MyEnum myEnum; // my_enum

  /// ...
}

enum MyEnum {firstField, secondField} // first_field, second_field

The problem with that, @lukepighetti is that I generate one set of helpers for a given enum 鈥撀爏o I'd rather not have parent-class-specific configuration

In that case the previous suggestion seems like the remaining way to handle it.

Was this page helpful?
0 / 5 - 0 ratings