2

I love how easy it is to map JSON data to a Java object with Jsonb, but I seem to have stumbled upon a not well-documented use-case...

Given this json data:

{
  "id": "test",
  "points": [
    [
      -24.787439346313477,
      5.5551919937133789
    ],
    [
      -23.788913726806641,
      6.7245755195617676
    ],
    [
      -22.257251739501953,
      7.2461895942687988
    ]
  ]
}

What can be used as the object type to store the points-values?

import jakarta.json.bind.annotation.JsonbProperty;

public class Temp {

    @JsonbProperty("id")
    private String id;
        
    @JsonbProperty("points")
    private ??? points;

    // Getters-Setters
}

So I can create the Temp-object with:

import jakarta.json.bind.Jsonb;
import jakarta.json.bind.JsonbBuilder;
    
Jsonb jsonb = JsonbBuilder.create();
Temp temp = jsonb.fromJson(jsonString, Temp.class);

So far I've tried the following:

  • List<Point> --> "Can't deserialize JSON array into: class java.awt.Point"
  • List<Point2D> --> "Can't deserialize JSON array into: class java.awt.Point2D"
Frank
  • 5,741
  • 4
  • 28
  • 49

3 Answers3

2

Let's try it:

@Data
public class Temp {
    @JsonbProperty("id")
    private String id;
    
    @JsonbProperty("points")
    private List<List<BigDecimal>> points;
    
    public static void main(String[] args) {
        String jsonString = "{\n" +
                            "  \"id\": \"test\",\n" +
                            "  \"points\": [\n" +
                            "    [\n" +
                            "      -24.787439346313477,\n" +
                            "      5.5551919937133789\n" +
                            "    ],\n" +
                            "    [\n" +
                            "      -23.788913726806641,\n" +
                            "      6.7245755195617676\n" +
                            "    ],\n" +
                            "    [\n" +
                            "      -22.257251739501953,\n" +
                            "      7.2461895942687988\n" +
                            "    ]\n" +
                            "  ]\n" +
                            "}";
        Jsonb jsonb = JsonbBuilder.create();
        Temp temp = jsonb.fromJson(jsonString, Temp.class);
        System.out.println(temp);
    }
}
logbasex
  • 1,688
  • 1
  • 16
  • 22
1

To figure out the default mapping, use a non-generic field and observe it with the debugger:

public class Test {
  public static void main(String[] args) {
    String json = "{\"id\":\"test\",\"points\":[[-24.787439346313477,5.555191993713379],[-23.78891372680664,6.724575519561768],[-22.257251739501953,7.246189594268799]]}";
    Temp temp = JsonbBuilder.create().fromJson(json, Temp.class);
    System.out.println(temp.points);
  }

  public static class Temp {
    public String id     = null;
    public List   points = null;

    public Temp() {
    }
  }
}

enter image description here

Reto Höhener
  • 5,419
  • 4
  • 39
  • 79
  • Indeed, BigDecimals seem the way to go. Both your and @logbasex answers helped a lot, thanks! – Frank Jan 06 '21 at 08:26
  • If you control the json format, maybe change the points list to `[ { "x" : 1, "y": 2}, ...]` and map it to your own custom Point class. – Reto Höhener Jan 06 '21 at 08:29
  • indeed, that was my plan B, but need to check if that will be possible – Frank Jan 06 '21 at 08:32
1

Since I've already done it: Changing the json format would allow this:

public class Test {
  public static void main(String[] args) {
    String json = "{\"id\":\"test\",\"points\":[ {\"x\" : 1.0, \"y\" : 2.0 }, {\"x\" : 3.0, \"y\" : 4.0 } ] }";
    Temp temp = JsonbBuilder.create().fromJson(json, Temp.class);
    System.out.println(temp.points);
  }

  public static class Temp {
    public String      id     = null;
    public List<Point> points = null;
    public Temp() { }
  }

  public static class Point {
    public double x;
    public double y;
    public Point() { }
  }
}
Reto Höhener
  • 5,419
  • 4
  • 39
  • 79