Java: json array deserialization with zeros

I have JSON that looks like this:

{
  "values": [
    [
      123456,
      789.0
    ],
    [
      123457,
      null
    ]
  ]
}

"Scheme": each value is an array of exactly two things, the first of which is long, and the second is double (or zero). I would like to parse this into a Java object (just a POJO).

I tried Jackson, but he has a known bug that prevents zeros from working in arrays: https://github.com/FasterXML/jackson-databind/issues/403

I also tried Gson, but apparently it can't handle the idea of โ€‹โ€‹converting arrays to Java objects (not Java arrays):

com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 3 column 6

Here's the full test class, demonstrating the inoperability of both Jackson and Gson for this simple task:

import java.util.List;

import org.codehaus.jackson.annotate.JsonCreator;
import org.codehaus.jackson.map.ObjectMapper;

import com.google.gson.Gson;

public class JsonTest {

    private static final String TEST_JSON = 
                    "{\n" +
                    "  \"values\": [\n" +
                    "    [\n" +
                    "      123456,\n" +
                    "      789.0\n" +
                    "    ],\n" +
                    "    [\n" +
                    "      123457,\n" +
                    "      null\n" +
                    "    ]\n" +
                    "  ]\n" +
                    "}\n";

    public static class MyPojo1 {
        public List<TimestampAndValue> values;

        public static class TimestampAndValue {
            public long timestamp;
            public Double value;
            @JsonCreator
            public TimestampAndValue(List<Number> nums) {
                if(nums == null || nums.size() < 2) {
                    throw new IllegalArgumentException(String.format("Expected at least two numbers (timestamp & value), instead got: %s", nums));
                }
                this.timestamp = nums.get(0).longValue();
                this.value = nums.get(1).doubleValue();
            }
        }
    }

    public static class MyPojo2 {
        public List<TimestampAndValue> values;

        public static class TimestampAndValue {
            public long timestamp;
            public Double value;
        }
    }

    public static void main(String[] args) {
        try {
            System.out.println(new ObjectMapper().readValue(TEST_JSON, MyPojo1.class));
        } catch(Throwable t) {
            t.printStackTrace();
        }
        try {
            System.out.println(new Gson().fromJson(TEST_JSON, MyPojo2.class));
        } catch(Throwable t) {
            t.printStackTrace();
        }
    }

}

: JSON Java? , org.json , .

+4
3

, , (, , ) .

JSON , , : "" - , , 2, , - ( ). , . , - - x!= 2 , () , ; POJO.

, JSON Java library. , , . POJO public final, . Jackson Gson, JSON (. ), , , , .

- , , , , .

0

, :

// my pojo
public class Main {
    // my property - it a 2-dim array (might work with a List of Lists as well)
    @JsonProperty(value = "values")
    Double[][] values;
    public static void main(String[] args) throws Exception {
        ObjectMapper om = new ObjectMapper();
        String json = "{\"values\":[[123456,789.0],[123457,null]]}";
        Main m = om.readValue(json, Main.class);
        // prints the de-serialized json
        System.out.println(Arrays.deepToString(m.values));
    }
}

[[123456.0, 789.0], [123457.0, null]]

  • Doubles , NULL, ObjectMapper.
  • , , List<Number>, , JSON array ( List of List s).
  • LATEST ( Maven LATEST) Jackson libs. - 2.3.2 , Maven 2.4.1.3 - . Alexey.
  • . Jackson.
+4

GSON, :

MyPojo2:

public class MyPojo2 
{
    public List<List<Double>> values;

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

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

JSON List, values.

public static void main (String[] args)
    {
        try 
        {
            String json = "{\"values\":[[123456,789],[123457,null]]}";
            MyPojo2 obj = null;

            Gson gson = new Gson();
            obj = gson.fromJson(json, MyPojo2.class);
            System.out.println(obj);
            System.out.println(gson.toJson(obj));
        } 
        catch (Exception e) 
        {
            e.printStackTrace();
        }
    }

:

MyPojo2@b09697
{"values":[[123456.0,789.0],[123457.0,null]]}
0
source

All Articles