Deserializing a json which contains @JsonFormat(shape=JsonFormat.Shape.ARRAY) and custom object using jackson

六眼飞鱼酱① 提交于 2021-02-05 08:18:48

问题


I have a custom object :

public class Response
{
    @JsonProperty("values")
    private List<Value> values;

    @JsonFormat(shape=JsonFormat.Shape.ARRAY)
    public static class Value {

        public Value(long timestamp, float val)
        {
            this.timestamp = timestamp;
            this.val = val;
        }
    }
}

But when this is being parsed, I get "Can not deserialize instance of Response$Value out of START_ARRAY token". The json is :

{
"values":[[1552215648,18]]
}

Any idea if I'm missing something here? Or should I have a custom deserializer to perform this?


回答1:


JsonFormat does the trick but you also need to declare constructor with JsonCreator annotation. Take a look on below example:

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.File;
import java.util.List;

public class JsonApp {

    public static void main(String[] args) throws Exception {
        File jsonFile = new File("./resource/test.json").getAbsoluteFile();

        ObjectMapper mapper = new ObjectMapper();
        Response myPojo = mapper.readValue(jsonFile, Response.class);
        System.out.println(myPojo);
    }
}

class Response {

    private List<Value> values;

    public List<Value> getValues() {
        return values;
    }

    public void setValues(List<Value> values) {
        this.values = values;
    }

    @Override
    public String toString() {
        return "Response{" +
                "values=" + values +
                '}';
    }
}

@JsonFormat(shape = JsonFormat.Shape.ARRAY)
class Value {

    private long timestamp;
    private float val;

    @JsonCreator
    public Value(@JsonProperty("timestamp") long timestamp, @JsonProperty("val") float val) {
        this.timestamp = timestamp;
        this.val = val;
    }

    public long getTimestamp() {
        return timestamp;
    }

    public void setTimestamp(long timestamp) {
        this.timestamp = timestamp;
    }

    public float getVal() {
        return val;
    }

    public void setVal(float val) {
        this.val = val;
    }

    @Override
    public String toString() {
        return "Value{" +
                "timestamp=" + timestamp +
                ", val=" + val +
                '}';
    }
}

Above code for below JSON payload:

{
  "values": [
    [
      1552215648,
      18
    ],
    [
      123,
      12.24
    ]
  ]
}

Prints:

Response{values=[Value{timestamp=1552215648, val=18.0}, Value{timestamp=123, val=12.24}]}



回答2:


You should JsonFormat over the variable values. Also since you are having the variable as of type List, you need not add JsonFormat.

public class Response
    {
        @JsonProperty("values")
        @JsonFormat(shape=JsonFormat.Shape.ARRAY)
        private List<Value> values;

        public static class Value {
            private long timestamp;
            private float val;

            // Getters and Setters

            public Value(long timestamp, float val)
            {
                this.timestamp = timestamp;
                this.val = val;
            }
        }
    }

Your JSON input format would be:

{
  values: [
     {
       "timestamp": 589988,
       "val": 56.0,
      }
   ]
}

Hope this helps !! I haven't tested the code, so please ignore syntax issues if any.



来源:https://stackoverflow.com/questions/55086995/deserializing-a-json-which-contains-jsonformatshape-jsonformat-shape-array-an

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!