Question

I'm trying to create a Java REST endpoint which returns a JSON data array to be consumed by JQuery FLOT charting plugin.

At a minimum, the JSON data for FLOT needs to be an array of numbers i.e.

[ [x1, y1], [x2, y2], ... ]

Given I have a List of Point objects in Java i.e.

List<Point> data = new ArrayList<>();

where Point is defined as

public class Point {

    private final Integer x;
    private final Integer y;

    public Point(Integer x, Integer y) {
        this.x = x;
        this.y = y;
    }

    ...
 }

What methods or Jackson2 annotations, if any, do I need to put on the Java objects to get the correct JSON format. Currently I am getting the output in this format:

[{x:x1, y:y1}, {x:x2, y:y2} ...]

When I need this format:

[[x1,y1], [x2,y2] ...]
Was it helpful?

Solution 2

You can write a custom Point serializer

import java.io.IOException;

import org.codehaus.jackson.JsonGenerator;
import org.codehaus.jackson.JsonProcessingException;
import org.codehaus.jackson.map.JsonSerializer;
import org.codehaus.jackson.map.SerializerProvider;

public class CustomPointSerializer extends JsonSerializer<Point> {

    @Override
    public void serialize(Point point, JsonGenerator gen, SerializerProvider provider) throws IOException, JsonProcessingException {
        gen.writeStartArray();
        gen.writeNumber(point.getX());
        gen.writeNumber(point.getY());
        gen.writeEndArray();
    }
}

then you can set your custom serializer class to your Point class

import org.codehaus.jackson.map.annotate.JsonSerialize;

@JsonSerialize(using = CustomPointSerializer.class)
public class Point {

    private Integer x;
    private Integer y;

    public Point(Integer x, Integer y) {
        this.x = x;
        this.y = y;
    }

    public Integer getX() {
        return x;
    }

    public void setX(Integer x) {
        this.x = x;
    }

    public Integer getY() {
        return y;
    }

    public void setY(Integer y) {
        this.y = y;
    }
}

and try it

ObjectMapper mapper = new ObjectMapper();
List<Point> points = new ArrayList<Point>();
points.add(new Point(1,2));
points.add(new Point(2,3));
System.out.println(mapper.writeValueAsString(points));

the code produces the following result

[[1,2],[2,3]]

hope this helps.

OTHER TIPS

You can use the @JsonView annotation on a special getter method which returns an array of intergers. Here is an example:

public class JacksonObjectAsArray {
    static class Point {

        private final Integer x;
        private final Integer y;

        public Point(Integer x, Integer y) {
            this.x = x;
            this.y = y;
        }

        @JsonValue
        public int[] getXY() {
            return new int[] {x, y};
        }
    }

    public static void main(String[] args) throws JsonProcessingException {
        ObjectMapper mapper = new ObjectMapper();
        System.out.println(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(new Point(12, 45)));
    }

}

Output:

[ 12, 45 ]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top