4
我有一個類,它看起來像這樣:如何與傑克遜序列基於java.util.Map類
@JsonFormat(shape=JsonFormat.Shape.OBJECT)
public class MyMap implements Map<String, String>
{
protected Map<String, String> myMap = new HashMap<String, String>();
protected String myProperty = "my property";
public String getMyProperty()
{
return myProperty;
}
public void setMyProperty(String myProperty)
{
this.myProperty = myProperty;
}
//
// java.util.Map mathods implementations
// ...
}
而與此代碼主要方法:
MyMap map = new MyMap();
map.put("str1", "str2");
ObjectMapper mapper = new ObjectMapper();
mapper.getDeserializationConfig().withAnnotationIntrospector(new JacksonAnnotationIntrospector());
mapper.getSerializationConfig().withAnnotationIntrospector(new JacksonAnnotationIntrospector());
System.out.println(mapper.writeValueAsString(map));
當執行此代碼我得到以下輸出:{「str1」:「str2」}
我的問題是爲什麼內部屬性「myProperty」沒有與地圖序列化? 應該做些什麼來序列化內部屬性?
由於阿列克謝。我選擇嘗試構圖模式,並完成了這項工作。只需要注意一點,我不需要爲我的地圖獲取器使用@JsonAnyGetter註釋。 – odavid
@odavid好!請注意,如果您不使用JsonAnyGetter,那麼您的json看起來像:{「field」:「value1」,「map」:{「key1」:「value2」}} –