java jackson json 处理器 - 在 RestTemplate 中使用 - EnumSet 的处理
我有一个连接到 JSON WebService 的 Android 应用程序。其中一种方法返回“标志类型”值的逗号分隔字符串列表,换句话说,是位掩码。例如,它返回“FileAppend,FileOverwrite”。对于这种类型,我定义了一个 java 枚举
enum FileMode { FileAppend, FileOverwrite, ... }
,并希望 Jackson 反序列化器自动将 JSON 负载中返回的字符串列表转换为枚举。我尝试了原始 Enum FileMode 和 EnumSet,但在反序列化时这两种情况下都出现异常。有没有办法以某种方式进行注释,以便反序列化器知道如何反序列化它?
@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 是一个位字段,这意味着它可以有多个字段集(位掩码)。 当我使用 JSON 反序列化器反序列化此类时,出现以下异常 嵌套异常是 org.codehaus.jackson.map.JsonMappingException:无法反序列化 VALUE_STRING 令牌之外的 java.util.EnumSet 实例
可能是什么原因?
问候
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果值(如错误消息所示)只是一个 JSON 字符串,而不是预期的字符串数组,则您需要编写一个自定义反序列化器。但为什么这些不序列化为 JSON 数组,并将枚举值作为单独的字符串呢? Jackson 会在没有任何注释的情况下自动处理这个问题(不需要您添加的任何注释,我假设添加它们是为了尝试使事情正常工作?)。
您可以直接在字段 (@JsonDeserialize(using=MyDeserializer.class)) 上注册反序列化器,也可以通过注册该类型的反序列化器。
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.
在 json 中,将值作为字符串数组传递。例如 - 如果您有 DAY 的枚举集,其中 DAY 是一个具有 MONDAY、TUESDAY 等值的枚举,则将值传递为 -
"days" :["MONDAY","SUNDAY"]
默认反序列化会导致创建枚举集。
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.