Jackson ObjectMapper冲突的setter定义(Android.Graphics.Bitmap)

时间:2014-03-21 20:12:52

标签: java android json jackson

我有这个方法:

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

这是类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() {}
    }
}

以下是来自其他班级的电话:

Response rep = JsonOperation.deserializeResponse(jsonString);

我在使用android 2.3.6的设备上运行此代码,一切都很完美。 当我在我的Galaxy Note 3或S4上运行此代码时,我有几个错误。这是日志:

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

以下是来自服务器的Json输入:

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

我搜索并试图找到这个问题的答案,但我没有找到关于android.graphics.Bitmap中的setter的任何内容......我试图使用@JsonIgnore而没有任何结果。 我在尝试使用映射器时需要帮助来解决异常。 任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:9)

我刚刚解决了这个问题(对我而言),虽然这对于这些三星设备来说更像是一种解决方法。我意识到你提到你尝试了@JsonIgnore注释,但它设法解决了我的问题。

在我的模型中,我只是将JSON映射到我的基本模型的一小部分。但是,在这个基本模型中,我有一个包含Bitmap的另一个Object模型的List。通过将@JsonIgnore添加到模型列表中,问题得到解决(尽管我没有映射到它们,它们应该被忽略)。

我一直在使用ObjectMapper功能来忽略未使用的值,但它在三星设备上不起作用。

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

在下面的示例中,我的Profile对象包含一个Bitmap,我只使用JSON映射名称和年龄。

示例:

public class A {

private String name;

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

如果我不得不猜测,我会说上面的Performer对象包含一个Bitmap,如果你没有映射到那个,那么我会尝试忽略List。