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. 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

我有一個連接到JSON WebService的Android應用程序。其中一個方法為“flag-type”值返回逗號分隔的字符串列表,換句話說是位掩碼。例如,它返回“FileAppend,FileOverwrite”。對於這種類型,我定義了一個java枚舉

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?

並希望Jackson反序列化器自動將JSON有效負載中返回的String列表轉換為枚舉。我嘗試了原始的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 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

AccessMask是一個位字段,意味着它可以有多個字段集(位掩碼)。當我使用JSON反序列化器反序列化此類時,我得到以下異常嵌套異常是org.codehaus.jackson.map.JsonMappingException:無法從VALUE_STRING標記反序列化java.util.EnumSet的實例

What may be the reason?

可能是什么原因?

Regards

2 个解决方案

#1


1  

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?).

如果值是 - 如錯誤消息所示 - 只是一個JSON字符串,而不是預期的字符串數組,則需要編寫自定義反序列化程序。但是為什么這些沒有序列化為帶有枚舉值的JSON數組作為單獨的字符串?傑克遜會在沒有任何注釋的情況下自動處理這個問題(你不需要添加任何注釋,我認為它們是為了讓事情變得有效而加入的)。

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

您可以直接在字段(@JsonDeserialize(using = MyDeserializer.class))上注冊反序列化器,也可以注冊該類型的反序列化器。

#2


0  

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"]

在json中,將值作為字符串數組傳遞。例如 - 如果您有DAY的枚舉,其中DAY是值為MONDAY,TUESDAY等的枚舉,則將值傳遞為 - “days”:[“MONDAY”,“SUNDAY”]

default deserialization results in creating enumset.

默認反序列化導致創建枚舉。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2011/08/10/730271dbc4fd01c747c879dc51feec71.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com