Gson ignore unknown enum value

755 views
Skip to first unread message

Sean Nguyen

unread,
Aug 4, 2011, 2:12:41 PM8/4/11
to google-gson
Hi,

Is there a way to configure gson to ignore an unknown enum value? For
example

in my client code there is an enum DeviceType has 2 enum value : PC,
MOBILE

But when a server is sent another type for example MAC as another
DeviceType. How can I configure my gson deserilalizer to set the
DeviceType = null instead of throwing an exception?

Thanks,

Sean

inde...@gmail.com

unread,
Aug 6, 2011, 2:01:56 AM8/6/11
to googl...@googlegroups.com
Have you considered registering a type adapter for your Enum class that implements the desired behavior? 

Inder

------

Todd Chapman

unread,
Sep 6, 2013, 10:34:05 AM9/6/13
to googl...@googlegroups.com
Sean,

If you can recall this far back, how did you configure Gson such that it threw an exception on an unknown enum value? I have the opposite problem in that unknown enum values are deserialized to nulls and no exception is thrown. Thanks!

-Todd

Martin Grajcar

unread,
Sep 6, 2013, 11:13:43 AM9/6/13
to googl...@googlegroups.com


--
You received this message because you are subscribed to the Google Groups "google-gson" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-gson...@googlegroups.com.
To post to this group, send email to googl...@googlegroups.com.
Visit this group at http://groups.google.com/group/google-gson.
For more options, visit https://groups.google.com/groups/opt_out.

Reply all
Reply to author
Forward
0 new messages