jsonb-api icon indicating copy to clipboard operation
jsonb-api copied to clipboard

Clarify what happens if an unknown Enum value gets deserialised

Open struberg opened this issue 7 years ago • 8 comments
trafficstars

Assume you have a Java class which has an Enum. What happens if a JSON input contains a value for that Enum which is not a valid Enum value?

The only useful sentence I could find in the spec is

Deserialization of a JSON value into an enum instance MUST be done 
by calling the enum’s valueOf(String) method.

And valueOf(String) throws an IllegalArgumentException. Is this what is intended? Means should we blow up in that case with a JsonbException?

The other option would be to set the field to null.

struberg avatar Aug 29 '18 16:08 struberg

JsonbException must be thrown in this case.

m0mus avatar Aug 31 '18 12:08 m0mus

Great, thanks @m0mus. Now the question remains whether you think we should further clarify this in the spec?

struberg avatar Aug 31 '18 13:08 struberg

We cannot change a spec document, but feel free to make changes in javadocs.

m0mus avatar Sep 06 '18 20:09 m0mus

Closing this as stale

aguibert avatar Jul 22 '19 20:07 aguibert

@aguibert I think it's too early to close. It makes sense updating the spec and clarify this behavior as Mark is suggesting.

m0mus avatar Jul 24 '19 09:07 m0mus

IMO the spec is pretty clear already:

Deserialization of a JSON value into an enum instance MUST be done 
by calling the enum’s valueOf(String) method.

If we pass an unknown enum value to enum's valueOf(String) method, then we do whatever valueOf(String) would do if an unknown value gets passed to it. No need to re-specify this in JSON-B.

aguibert avatar Jul 24 '19 15:07 aguibert

I suppose we could append the text: If deserializing the enum fails, an error is raised.

@m0mus do you have an estimate of where the spec asciidoc is? It looks like we just have skeleton information right now

aguibert avatar Jul 24 '19 16:07 aguibert