简体   繁体   English

杰克逊如何使用自定义获取器/设置器序列化地图?

[英]How does jackson serialize a map with custom getters/setters ?

I have a bean in jackson which uses the @JSonAnySetter method to store all unknown parameters in a map. 我在jackson中有一个bean,它使用@JSonAnySetter方法将所有未知参数存储在地图中。

@JSonAnySetter
handleUnkowns(String k, Object v) 
{
   myMap.put(k,v); 
}

I use this as the "Base bean" for all my data types, so that if data is missing, the unknown parameters are populated and data is not lost.... rather than jackson crashing. 我将其用作所有数据类型的“基础Bean”,这样,如果丢失了数据,则会填充未知参数,并且不会丢失数据....而不是让杰克逊崩溃。

However, I want the serialized form of these unknowns to NOT be nested - that is - I want serialized parameters to be at the top level of the object, when the object is serialized. 但是,我希望这些未知数的序列化形式不被嵌套-也就是说-当对象被序列化时,我希望序列化的参数位于对象的顶层。 Additionally, I want the custom fields to also be serialized : 另外,我希望自定义字段也可以序列化:

//I want this map to be serialized/deserialized : {"collarWidth":10 "name":"fido"}
class Dog extens JSonBean 
{
     int collarWidth=0;
     getCollarWidth(){return collarWidth;}
     setCollarWidth(int x){collarWidth=x;}
}

Note that in the above case - since I extend from a Map, Jackson's custom Map serialization will take place, and the unknownParameters will be a "field" in my json. 请注意,在上述情况下-由于我是从Map扩展而来的,因此将进行Jackson的自定义Map序列化操作,并且unknownParameters将是json中的“字段”。

Thus the expected JSON serialization would be 因此,预期的JSON序列化将是
{"collarWidth":10 "unknownParameters":{"name":"fido"}} {“ collarWidth”:10“ unknownParameters”:{“ name”:“ fido”}}

rather than 而不是

{"collarWidth":10 "name":"fido"} {“ collarWidth”:10“名称”:“ fido”}

So - what is the simplest way to "merge" the unknown parameters with the known ones, so that the java bean serializer retains the same nesting as the input string ? 那么-将未知参数与已知参数“合并”的最简单方法是什么,以便Java Bean序列化程序保留与输入字符串相同的嵌套?

The obvious solution is to merge the parameters from the "myMap" object with the serialized map , but that seems like overkill, and i assume that this problem might have a more elegant solution. 显而易见的解决方案是将“ myMap”对象中的参数与序列化的地图合并,但这似乎有点过头了,我认为这个问题可能有一个更优雅的解决方案。

Have you checked out @JsonAnyGetter annotation? 您是否已签出@JsonAnyGetter批注? Map that method returns will be unwrapped, to make it work with @JsonAnySetter . 该方法返回的映射将被解包,以使其与@JsonAnySetter一起@JsonAnySetter This blog entry explains usage. 此博客条目解释了用法。

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

相关问题 Hibernate如何将设置器/获取器映射到属性? - How does Hibernate map setters/getters to properties? 使用Jackson序列化自定义地图 - Serialize a custom map with Jackson 杰克逊反序列化:如何将特定属性映射到getter和setter以及将所有属性加载到同一个POJO的映射中? - Jackson Deserialization: How to map specific properties to getters and setters as well as loading all the properties into map of a same POJO? IDE不会显示Lombok为Jackson注释类生成的getter和setter - IDE does not show getters and setters generated by Lombok for a Jackson annotated class Eclipse如何生成setter和getter? - How does Eclipse generate setters and getters? 如何序列化地图 <Integer, Custom object> 与杰克逊流API? - How to serialize a map<Integer, Custom object> with Jackson streaming API? 序列化/反序列化自定义地图 <Key, Object> 在杰克逊 - Serialize/Deserialize custom Map<Key, Object> in Jackson 杰克逊(Jackson):与Builder一起反序列化标准设置器/获取器? - Jackson: deserialize with Builder along with standard setters/getters? 具有 map 的 class 的获取器/设置器 - Getters/setters of a class having a map 杰克逊将枚举和非传统的二传手/字母序列化 - Jackson Serializing Enums And Non Traditional Setters/Getters
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM