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 396 forks source link

Dynamic field and enum #1248

Closed SuricateTrading closed 1 year ago

SuricateTrading commented 1 year ago

When serializing a dynamic field containing an enum, the error "Converting object to an encodable object failed" occurs.

Example:

@JsonSerializable() class MyClass{ dynamic field; }

@JsonEnum() enum MyEnum{ @JsonValue(0) top, @JsonValue(1) middle, }

void main() { MyClass() ..field = MyEnum.top ..myClass.toJson(); }

kevmoo commented 1 year ago

By design. We NEED the type info to do any type of conversion. dynamic (or Object, etc) only works with values that are already JSON-compatible.