使用 Jackson 将 JSON 反序列化为具有重载方法的对象 [英] Deserializing JSON into object with overloaded methods using Jackson
问题描述
我正在尝试使用 Jackson 反序列化存储在 CouchDb 中的 JSON 对象.此对象需要反序列化为包含重载方法的 pojo.当我尝试从沙发上检索对象并进行反序列化时,出现以下异常:
I am attempting to deserialize a JSON object stored in CouchDb using Jackson. This object needs to deserialize into a pojo that contains overloaded methods. When I attempt to retrieve the object from couch and do the deserialization I get the following exception:
org.ektorp.DbAccessException:org.codehaus.jackson.map.JsonMappingException:冲突的 setter 定义属性乘数":com.db.commodities.framework.sdos.model.security.EqOpt#setMultiplier(1参数) vscom.db.commodities.framework.sdos.model.security.EqOpt#setMultiplier(1参数)
org.ektorp.DbAccessException: org.codehaus.jackson.map.JsonMappingException: Conflicting setter definitions for property "multiplier": com.db.commodities.framework.sdos.model.security.EqOpt#setMultiplier(1 params) vs com.db.commodities.framework.sdos.model.security.EqOpt#setMultiplier(1 params)
我试图注释我希望 Jackson 使用的 setter,但这似乎不起作用.
I tried to annotate the setter I would like Jackson to use, but that appears to not have worked.
@JsonProperty("multiplier")
public void setMultiplier(SDOSAttribute multiplier) {
this.multiplier = multiplier;
}
public void setMultiplier(double multiplier) {
this.multiplier.setValue(String.valueOf(multiplier));
}
如何配置 Jackson 以使用特定方法正确反序列化?还是我以错误的方式处理这个问题?
How do I configure Jackson to properly deserialize using a specific method? Or am I approaching this problem the wrong way?
我进行了以下更改.这似乎有效,但有点难看.如果有人有更好的方法来做到这一点,请随时分享,我很乐意接受.
I have made the following changes. This seems to work, but is a little uglier. If anyone has a better way to do this please feel free to share and I will gladly accept.
@JsonProperty("multiplier")
protected void setMultiplierAttribute(SDOSAttribute multiplier) {
this.multiplier = multiplier;
}
@JsonIgnore
public void setMultiplier(double multiplier) {
this.multiplier.setValue(String.valueOf(multiplier));
}
推荐答案
没有必要更改 setter 方法的名称以避免歧义.否则,您使用 @JsonIgnore
走在正确的轨道上.使用 @JsonIgnore
在所有要忽略的同名方法上,使用的方法不需要 @JsonProperty
注释.
It's not necessary to change the name of the setter method to avoid ambiguity. You're otherwise on the right track with @JsonIgnore
. With @JsonIgnore
on all of the same-named methods to be ignored, the one to use does not need the @JsonProperty
annotation.
这是一个简单的例子来证明这一点.
Here's a simple example to demonstrate this point.
input.json: {"value":"四十二"}
Foo.java:
import java.io.File;
import org.codehaus.jackson.annotate.JsonIgnore;
import org.codehaus.jackson.map.ObjectMapper;
public class Foo
{
String value;
public String getValue() {return value;}
public void setValue(String value) {this.value = value;}
@JsonIgnore
public void setValue(int value) {this.value = String.valueOf(value);}
public static void main(String[] args) throws Exception
{
ObjectMapper mapper = new ObjectMapper();
Foo foo = mapper.readValue(new File("input.json"), Foo.class);
System.out.println(mapper.writeValueAsString(foo));
}
}
如果您不想使用 Jackson 注释更改原始 POJO defs,那么您可以使用 MixIn
.
If you don't want to alter the pristine POJO defs with a Jackson annotation, then you can use a MixIn
.
import java.io.File;
import org.codehaus.jackson.annotate.JsonIgnore;
import org.codehaus.jackson.map.ObjectMapper;
public class Foo
{
String value;
public String getValue() {return value;}
public void setValue(String value) {this.value = value;}
public void setValue(int value) {this.value = String.valueOf(value);}
public static void main(String[] args) throws Exception
{
ObjectMapper mapper = new ObjectMapper();
mapper.getDeserializationConfig().addMixInAnnotations(Foo.class, IgnoreFooSetValueIntMixIn.class);
Foo foo = mapper.readValue(new File("input.json"), Foo.class);
System.out.println(mapper.writeValueAsString(foo));
}
}
abstract class IgnoreFooSetValueIntMixIn
{
@JsonIgnore public abstract void setValue(int value);
}
这篇关于使用 Jackson 将 JSON 反序列化为具有重载方法的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!