简体   繁体   English

java jackson json 处理器 - 在 RestTemplate 中使用 - 处理 EnumSet

[英]java jackson json processor - using in RestTemplate - handling of EnumSet

I have an Android app that connects to a JSON WebService.我有一个连接到 JSON WebService 的 Android 应用程序。 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".例如,它返回“FileAppend,FileOverwrite”。 For this type I have a java enum defined对于这种类型,我定义了一个 java 枚举

enum FileMode { FileAppend, FileOverwrite,   ... } 

and want Jackson deserializer to automatically convert the returned String list in JSON payload into the enum.并希望 Jackson 解串器自动将 JSON 有效负载中返回的字符串列表转换为枚举。 I tried both raw Enum FileMode and EnumSet but I get exceptions in both cases while deserialization.我尝试了原始 Enum FileMode 和 EnumSet ,但是在反序列化时我都遇到了异常。 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). AccessMask 是一个位字段,意味着它可以有多个字段集(位掩码)。 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 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问候

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.如果 value 是 - 如错误消息所示 - 只是一个 JSON 字符串,而不是预期的字符串数组,则需要编写自定义反序列化程序。 But why are these not serialized as JSON arrays with enum values as individual Strings?但是为什么这些不序列化为 JSON arrays 枚举值作为单独的字符串? 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?). Jackson 会在没有任何注释的情况下自动处理这个问题(您添加的任何注释都不需要,我假设它们是为了让事情正常工作而添加的?)。

You can register deserializer either directly on field (@JsonDeserialize(using=MyDeserializer.class)) or by registering deserializer for that type.您可以直接在字段 (@JsonDeserialize(using=MyDeserializer.class)) 上注册反序列化器,也可以通过为该类型注册反序列化器。

In json, pass values as array of string.在 json 中,将值作为字符串数组传递。 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"]例如 - 如果您有 DAY 的枚举集,其中 DAY 是一个枚举,其值为 MONDAY、TUESDAY 等,则将值传递为 - "days":["MONDAY","SUNDAY"]

default deserialization results in creating enumset.默认反序列化会导致创建枚举集。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM