Json_serializable.dart: Support deserialization to default enum value for unsupported values

Created on 26 Jun 2019  ·  12Comments  ·  Source: google/json_serializable.dart

I would prefer to have enum field set to provided default value instead of "is not one of the supported values" ArgumentError. I don't want my app crashes on any Back End change.

P2 medium enhancement

Most helpful comment

@CoderSpinoza @Lisenish – PR in flight https://github.com/dart-lang/json_serializable/pull/516

Please take a look

All 12 comments

Reasonable...

I would prefer to have enum field set to provided default value instead of "is not one of the supported values" ArgumentError. I don't want my app crashes on any Back End change.

You could annotate an enum value as unknown. Gotcha.

@kevmoo How can this be achieved? I see that #360 hasn't been merged.

I didn't like the approach of #360 – although we could do something similar on second thought.

I would prefer to have enum field set to provided default value instead of "is not one of the supported values" ArgumentError. I don't want my app crashes on any Back End change.

You could annotate an enum value as unknown. Gotcha.

By above, you mean something like below?

enum ApiErrorCause {
  @JsonValue(-1)
  INTERNAL_ERROR,
  @JsonValue(-2)
  ILLEGAL_PARAMS,
  @JsonUnknown
  UNKNOWN
}

@kevmoo @Lisenish @beastoin

Could we discuss on how to support this? I believe this is pretty much necessary for any API response field that we want to express as enum. It won't be very clean to use custom JsonConverter for every enum field.

@CoderSpinoza I think while this issue is open unfortunately there is no other approach right now except JsonConverter.

This turns out to be a bit more gnarly than I feared. Digging in...

@CoderSpinoza @Lisenish – PR in flight https://github.com/dart-lang/json_serializable/pull/516

Please take a look

@kevmoo Thanks a lot for such a fast merge :) I will wait for next release!

@kevmoo Awesome work, thank you! 👍

Good to hear that unknown enum has been supported. Thank @CoderSpinoza @Lisenish and @kevmoo for your hard working. Happy coding 👨‍💻

Was this page helpful?
0 / 5 - 0 ratings

Related issues

fzyzcjy picture fzyzcjy  ·  5Comments

Nico04 picture Nico04  ·  5Comments

hatjs880328s picture hatjs880328s  ·  3Comments

FaisalAbid picture FaisalAbid  ·  5Comments

matanlurey picture matanlurey  ·  5Comments