Code example for JsonArray

Methods: add

0
 */ 
public class CenterAdapter implements JsonSerializer<Center>, JsonDeserializer<Center>{
 
    @Override 
    public JsonElement serialize(Center src, Type typeOfSrc, JsonSerializationContext context) {
        JsonArray centerArray = new JsonArray();
        JsonPrimitive x = new JsonPrimitive(src.getX());
        JsonPrimitive y = new JsonPrimitive(src.getY());
        centerArray.add(x);
        centerArray.add(y);
        return centerArray;
    } 
 
    @Override 
    public Center deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
        Center center = new Center();
        if (json instanceof JsonArray) {
            JsonArray array = (JsonArray)json;
            if (array.size() != 2) {
                throw new JsonParseException("Center must be of format [x,y]");
Experience pair programming with AI  Get Codota for Java