0
votes

I have an Android app that connects to a JSON WebService. One of the methods returns comma separated string list for "flag-type" value, in other words a bit mask. For instance, it returns "FileAppend, FileOverwrite". For this type I have a java enum defined

enum FileMode { FileAppend, FileOverwrite,   ... } 

and want Jackson deserializer to automatically convert the returned String list in JSON payload into the enum. I tried both raw Enum FileMode and EnumSet but I get exceptions in both cases while deserialization. Is there a way to annotate somehow so that the deserializer know how to deserialize it?

@JsonIgnoreProperties(ignoreUnknown=true)
@JsonTypeName("AccessMask")
@JsonAutoDetect
public enum AccessMask {
None,

HideDateCreated,
HideDateModified,
HideDateTaken,
HideMetaData,
HideUserStats,
HideVisits,

NoCollections,
NoPrivateSearch,
NoPublicSearch,
NoRecentList,

ProtectExif,
ProtectXXLarge,             // new in version 1.3
ProtectExtraLarge,
ProtectLarge,
ProtectMedium,
ProtectOriginals,

ProtectGuestbook,           // new in version 1.1
NoPublicGuestbookPosts,     // new in version 1.1
NoPrivateGuestbookPosts,    // new in version 1.1
NoAnonymousGuestbookPosts,  // new in version 1.1

ProtectComments,            // new in version 1.1
NoPublicComments,           // new in version 1.1
NoPrivateComments,          // new in version 1.1
NoAnonymousComments,        // new in version 1.1

PasswordProtectOriginals,   // new in version 1.2

ProtectAll }

// and below is a property of a class defined below. 
class Picture {
  @JsonProperty("AccessMask")
  EnumSet<AccessMask> accessMask;
 }

AccessMask is a bit field meaning it can have multiple field set (bit mask). When I deserialize this class using JSON deserializer, I got the following exception nested exception is org.codehaus.jackson.map.JsonMappingException: Can not deserialize instance of java.util.EnumSet out of VALUE_STRING token

What may be the reason?

Regards

2
What kind of exceptions? Basic JSON String list like '["enum1","enum3"]' would work just fine, assuming values match with enum values. - StaxMan
I provided detailed info about exception and types being deserialized. - cubesoft

2 Answers

1
votes

If value is -- as error message suggests -- just a JSON String, and not as would expected, an array of Strings, you need to write a custom deserializer. But why are these not serialized as JSON arrays with enum values as individual Strings? Jackson would handle this automatically without any annotations (none of annotations you added are needed, I assume they were added to try to make things work?).

You can register deserializer either directly on field (@JsonDeserialize(using=MyDeserializer.class)) or by registering deserializer for that type.

0
votes

In json, pass values as array of string. For example - if you have enumset of DAY, where DAY is an enum with values MONDAY, TUESDAY, etc then pass values as - "days" :["MONDAY","SUNDAY"]

default deserialization results in creating enumset.