Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jackson ObjectMapper Conflicting setter definitions (Android.Graphics.Bitmap)

I have this method :

 public static Response deserializeResponse(String jsonResponse) throws JsonParseException, JsonMappingException, IOException {

    ObjectMapper mapper = new ObjectMapper();

    try{
        return mapper.readValue(jsonResponse, Response.class);
    }catch(JsonParseException e){
        e.printStackTrace();
    }catch(JsonMappingException e){
        e.printStackTrace();
    }catch(IOException e){
        e.printStackTrace();
    }
    return null;
}

Here is the class Response :

public static class Response {

    public String RequestType;

    public Data data;

    public void SetRequestType(String requestType) { this.RequestType = requestType; }

    @JsonIgnore
    @JsonProperty("data")
    public void SetData(Data data) { this.data = data; }

    public Response(){
        data = new Data();
    }

    public static class Data {

        public String Status;
        public String ErrorCode;
        public int versionCode;
        public String Expiry;
        public String ProductSKU;
        public String Email;
        public String Birthday;
        public String PictureUrl;
        public String Pin;
        public List<PhoneNumber> Numbers = new ArrayList<PhoneNumber>();
        public int IdTransaction;
        public int SubProductId;

        public List<Performer> Performers = new ArrayList<Performer>();
        public Client Client;

        public Data() {}
    }
}

Here is the call from an other class :

Response rep = JsonOperation.deserializeResponse(jsonString);

I run this code on a device that uses android 2.3.6 and everything works perfect. When i run this code on my Galaxy Note 3 or S4, I have severals errors. Here is the log :

W/System.err﹕ com.fasterxml.jackson.databind.JsonMappingException: Conflicting setter     definitions for property "imagePath": android.graphics.Bitmap#setImagePath(1 params) vs android.graphics.Bitmap#setImagePath(1 params)
03-21 15:51:54.373  15204-15218/com.francovoyance W/System.err﹕ at com.fasterxml.jackson.databind.deser.DeserializerCache._createAndCache2(DeserializerCache.java:269)
03-21 15:51:54.373  15204-15218/com.francovoyance W/System.err﹕ at com.fasterxml.jackson.databind.deser.DeserializerCache._createAndCacheValueDeserializer(DeserializerCache.java:244)
03-21 15:51:54.373  15204-15218/com.francovoyance W/System.err﹕ at com.fasterxml.jackson.databind.deser.DeserializerCache.findValueDeserializer(DeserializerCache.java:143)
03-21 15:51:54.373  15204-15218/com.francovoyance W/System.err﹕ at com.fasterxml.jackson.databind.DeserializationContext.findContextualValueDeserializer(DeserializationContext.java:325)
03-21 15:51:54.373  15204-15218/com.francovoyance W/System.err﹕ at com.fasterxml.jackson.databind.deser.std.StdDeserializer.findDeserializer(StdDeserializer.java:633)
03-21 15:51:54.373  15204-15218/com.francovoyance W/System.err﹕ at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.resolve(BeanDeserializerBase.java:427)
03-21 15:51:54.373  15204-15218/com.francovoyance W/System.err﹕ at com.fasterxml.jackson.databind.deser.DeserializerCache._createAndCache2(DeserializerCache.java:295)
03-21 15:51:54.373  15204-15218/com.francovoyance W/System.err﹕ at com.fasterxml.jackson.databind.deser.DeserializerCache._createAndCacheValueDeserializer(DeserializerCache.java:244)
03-21 15:51:54.373  15204-15218/com.francovoyance W/System.err﹕ at com.fasterxml.jackson.databind.deser.DeserializerCache.findValueDeserializer(DeserializerCache.java:143)
03-21 15:51:54.373  15204-15218/com.francovoyance W/System.err﹕ at com.fasterxml.jackson.databind.DeserializationContext.findContextualValueDeserializer(DeserializationContext.java:325)
03-21 15:51:54.373  15204-15218/com.francovoyance W/System.err﹕ at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.createContextual(CollectionDeserializer.java:147)
03-21 15:51:54.373  15204-15218/com.francovoyance W/System.err﹕ at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.createContextual(CollectionDeserializer.java:23)
03-21 15:51:54.373  15204-15218/com.francovoyance W/System.err﹕ at com.fasterxml.jackson.databind.DeserializationContext.findContextualValueDeserializer(DeserializationContext.java:329)
03-21 15:51:54.373  15204-15218/com.francovoyance W/System.err﹕ at com.fasterxml.jackson.databind.deser.std.StdDeserializer.findDeserializer(StdDeserializer.java:633)
03-21 15:51:54.383  15204-15218/com.francovoyance W/System.err﹕ at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.resolve(BeanDeserializerBase.java:427)
03-21 15:51:54.383  15204-15218/com.francovoyance W/System.err﹕ at com.fasterxml.jackson.databind.deser.DeserializerCache._createAndCache2(DeserializerCache.java:295)
03-21 15:51:54.383  15204-15218/com.francovoyance W/System.err﹕ at com.fasterxml.jackson.databind.deser.DeserializerCache._createAndCacheValueDeserializer(DeserializerCache.java:244)
03-21 15:51:54.383  15204-15218/com.francovoyance W/System.err﹕ at com.fasterxml.jackson.databind.deser.DeserializerCache.findValueDeserializer(DeserializerCache.java:143)
03-21 15:51:54.383  15204-15218/com.francovoyance W/System.err﹕ at com.fasterxml.jackson.databind.DeserializationContext.findContextualValueDeserializer(DeserializationContext.java:325)
03-21 15:51:54.383  15204-15218/com.francovoyance W/System.err﹕ at com.fasterxml.jackson.databind.deser.std.StdDeserializer.findDeserializer(StdDeserializer.java:633)
03-21 15:51:54.383  15204-15218/com.francovoyance W/System.err﹕ at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.resolve(BeanDeserializerBase.java:427)
03-21 15:51:54.383  15204-15218/com.francovoyance W/System.err﹕ at com.fasterxml.jackson.databind.deser.DeserializerCache._createAndCache2(DeserializerCache.java:295)
03-21 15:51:54.383  15204-15218/com.francovoyance W/System.err﹕ at com.fasterxml.jackson.databind.deser.DeserializerCache._createAndCacheValueDeserializer(DeserializerCache.java:244)
03-21 15:51:54.383  15204-15218/com.francovoyance W/System.err﹕ at com.fasterxml.jackson.databind.deser.DeserializerCache.findValueDeserializer(DeserializerCache.java:143)
03-21 15:51:54.383  15204-15218/com.francovoyance W/System.err﹕ at com.fasterxml.jackson.databind.DeserializationContext.findRootValueDeserializer(DeserializationContext.java:342)
03-21 15:51:54.383  15204-15218/com.francovoyance W/System.err﹕ at com.fasterxml.jackson.databind.ObjectMapper._findRootDeserializer(ObjectMapper.java:2895)
03-21 15:51:54.383  15204-15218/com.francovoyance W/System.err﹕ at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:2789)
03-21 15:51:54.383  15204-15218/com.francovoyance W/System.err﹕ at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:1943)
03-21 15:51:54.383  15204-15218/com.francovoyance W/System.err﹕ at com.francovoyance.JsonOperation.deserializeResponse(JsonOperation.java:321)
03-21 15:51:54.383  15204-15218/com.francovoyance W/System.err﹕ at com.francovoyance.HttpOperation$HttpPostRequest.doInBackground(HttpOperation.java:125)
03-21 15:51:54.383  15204-15218/com.francovoyance W/System.err﹕ at com.francovoyance.HttpOperation$HttpPostRequest.doInBackground(HttpOperation.java:69)
03-21 15:51:54.383  15204-15218/com.francovoyance W/System.err﹕ at android.os.AsyncTask$2.call(AsyncTask.java:287)
03-21 15:51:54.383  15204-15218/com.francovoyance W/System.err﹕ at java.util.concurrent.FutureTask.run(FutureTask.java:234)
03-21 15:51:54.383  15204-15218/com.francovoyance W/System.err﹕ at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
03-21 15:51:54.383  15204-15218/com.francovoyance W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
03-21 15:51:54.383  15204-15218/com.francovoyance W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
03-21 15:51:54.383  15204-15218/com.francovoyance W/System.err﹕ at java.lang.Thread.run(Thread.java:841)
03-21 15:51:54.383  15204-15218/com.francovoyance W/System.err﹕ Caused by: java.lang.IllegalArgumentException: Conflicting setter definitions for property "imagePath": android.graphics.Bitmap#setImagePath(1 params) vs android.graphics.Bitmap#setImagePath(1 params)
03-21 15:51:54.383  15204-15218/com.francovoyance W/System.err﹕ at com.fasterxml.jackson.databind.introspect.POJOPropertyBuilder.getSetter(POJOPropertyBuilder.java:196)
03-21 15:51:54.383  15204-15218/com.francovoyance W/System.err﹕ at com.fasterxml.jackson.databind.deser.BeanDeserializerFactory.filterBeanProps(BeanDeserializerFactory.java:614)
03-21 15:51:54.383  15204-15218/com.francovoyance W/System.err﹕ at com.fasterxml.jackson.databind.deser.BeanDeserializerFactory.addBeanProps(BeanDeserializerFactory.java:522)
03-21 15:51:54.383  15204-15218/com.francovoyance W/System.err﹕ at com.fasterxml.jackson.databind.deser.BeanDeserializerFactory.buildBeanDeserializer(BeanDeserializerFactory.java:267)
03-21 15:51:54.383  15204-15218/com.francovoyance W/System.err﹕ at com.fasterxml.jackson.databind.deser.BeanDeserializerFactory.createBeanDeserializer(BeanDeserializerFactory.java:171)
03-21 15:51:54.383  15204-15218/com.francovoyance W/System.err﹕ at com.fasterxml.jackson.databind.deser.DeserializerCache._createDeserializer(DeserializerCache.java:388)
03-21 15:51:54.383  15204-15218/com.francovoyance W/System.err﹕ at com.fasterxml.jackson.databind.deser.DeserializerCache._createAndCache2(DeserializerCache.java:264)
03-21 15:51:54.383  15204-15218/com.francovoyance W/System.err﹕ ... 36 more

Here is the Json input from the server :

private string CheckVersion(JObject jsonObject)
{
    StringBuilder sb = new StringBuilder();
    StringWriter sw = new StringWriter(sb);
    using (JsonWriter writer = new JsonTextWriter(sw))
    {
        writer.Formatting = Formatting.Indented;
        writer.WriteStartObject();

        writer.WritePropertyName("RequestType");
        writer.WriteValue("CheckVersion");

        writer.WritePropertyName("data");
        writer.WriteStartObject();

        writer.WritePropertyName("Status");

        string inVersion = (string)jsonObject["data"]["versionCode"];
        string actualVersion = WebTV.Common.AppSettings.GetString("APP_VERSION", "1");
        actualVersion = Convert.ToString((int)Convert.ToDouble(actualVersion));

        if (inVersion.Equals(actualVersion))
        {
            writer.WriteValue("Ok");
        }
        else
        {
            writer.WriteValue("Error");

            writer.WritePropertyName("versionCode");
            writer.WriteRawValue(actualVersion);
        }

        writer.WriteEndObject();

    }
    return sb.ToString();
}

I searched and tried to find answers to this problem, but i didn't find anything about the setters in android.graphics.Bitmap... I tried to use @JsonIgnore without any results. I need help to resolve the exception when I'm trying to use the mapper. Any help would be appreciated.

like image 731
francis lanthier Avatar asked Mar 21 '14 20:03

francis lanthier


1 Answers

I just solved this issue (for me), though it feels like more of a workaround for these Samsung devices. I realize you mentioned you tried the @JsonIgnore annotation, but it managed to solve my issue.

In my model, I was only mapping the JSON to a small subset of my base model. In this base model however, I had a List of another Object model that contained a Bitmap. By adding @JsonIgnore to the List of models, the issue was resolved (although I wasn't mapping to them and they should have been ignored).

I had been using an ObjectMapper feature to ignore unused values, but it did not work on the Samsung devices.

ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);

In the example below, my Profile object contains a Bitmap, and I'm only mapping the name and age with the JSON.

Example:

public class A {

private String name;

private int age;
@JsonIgnore
private List<Profile> profiles;
}

If I had to make a wild guess, I'd say your Performer object above contains a Bitmap, and if you're not mapping to that, then I wouldd try and ignore the List.

like image 72
Sam Avatar answered Oct 17 '22 18:10

Sam