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.