简体   繁体   中英

How to parse array of numeric values in a JSON to Java object with Jsonb

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"

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

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

在此处输入图像描述

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM