Killing JSON into an object with overloaded methods using Jackson

I am trying to deserialize a JSON object stored in CouchDb using Jackson. This object must be deserialized in pojo, which contains overloaded methods. When I try to retrieve an object from the couch and do deserialization, I get the following exception:

org.ektorp.DbAccessException: org.codehaus.jackson.map.JsonMappingException: Conflicting setter definitions for the multiplier property: com.db.commodities.framework.sdos.model.security.EqOpt # setMultiplier (1 params) vs com.db .commodities.framework.sdos.model.security.EqOpt # setMultiplier (1 PARAMS)

I tried to annotate the installer that Jackson would like to use, but it looks like it didn't work.

@JsonProperty("multiplier") public void setMultiplier(SDOSAttribute multiplier) { this.multiplier = multiplier; } public void setMultiplier(double multiplier) { this.multiplier.setValue(String.valueOf(multiplier)); } 

How to configure Jackson to properly deserialize using a specific method? Or am I approaching this problem incorrectly?

EDIT:

I made the following changes. It seems to work, but a little uglier. If anyone has a better way to do this, feel free to share, and I will happily agree.

 @JsonProperty("multiplier") protected void setMultiplierAttribute(SDOSAttribute multiplier) { this.multiplier = multiplier; } @JsonIgnore public void setMultiplier(double multiplier) { this.multiplier.setValue(String.valueOf(multiplier)); } 
+42
java json jackson
Jun 14 2018-11-11T00:
source share
3 answers

No need to change the name of the setter method to avoid ambiguity. Otherwise, you are on the right track with @JsonIgnore . When using @JsonIgnore for all the same named methods that need to be ignored, the @JsonProperty annotation is not required for use.

Here is a simple example to demonstrate this point.

input.json: {"value":"forty-two"}

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)); } } 

If you don't want to modify the original POJO defs using Jackson annotation, you can use 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); } 
+39
Jun 14 '11 at 19:48
source share
— -

In Jackson> 2.4 use directly:

 mapper.addMixIn(Foo.class, IgnoreFooSetValueIntMixIn.class); 
+3
Jun 02 '16 at 11:34
source share

In cases where @JsonIgnore does not help (I currently have such a case, but I'm still not sure about the reason), you can also use @XmlTransient instead.

+2
Sep 26 '12 at 10:57
source share



All Articles