Example usage for com.google.gson JsonArray add

List of usage examples for com.google.gson JsonArray add

Introduction

In this page you can find the example usage for com.google.gson JsonArray add.

Prototype

public void add(JsonElement element) 

Source Link

Document

Adds the specified element to self.

Usage

From source file:com.exorath.simpleapi.impl.hub.discovery.serverlist.HubServerImpl.java

License:Apache License

public JsonObject serializeToJson() {
    JsonObject object = new JsonObject();

    object.addProperty("bungee", getBungeeName());
    object.addProperty("joinable", isJoinable());
    object.addProperty("maxplayers", getPlayerAmount());

    JsonArray playersArray = new JsonArray();
    players.forEach(p -> playersArray.add(new JsonParser().parse(p.serialize())));
    return object;
}

From source file:com.exorath.simpleapi.impl.serverlist.GameServerImpl.java

License:Apache License

public JsonObject serializeToJson() {
    JsonObject object = new JsonObject();

    object.addProperty("bungee", getBungeeName());
    object.addProperty("joinable", isJoinable());
    object.addProperty("playable", isPlayable());
    object.addProperty("maxplayers", getPlayerAmount());

    JsonArray playersArray = new JsonArray();
    players.forEach(p -> playersArray.add(new JsonParser().parse(p.serialize())));
    JsonArray extraLoreArray = new JsonArray();
    extraLore.forEach(line -> extraLoreArray.add(new JsonPrimitive(line)));
    return object;
}

From source file:com.exsoloscript.challonge.gson.ParticipantQueryListAdapter.java

License:Apache License

@Override
public JsonElement serialize(List<ParticipantQuery> participantQueryList, Type type,
        JsonSerializationContext jsonSerializationContext) {
    JsonObject base = new JsonObject();
    JsonArray participantArray = new JsonArray();

    for (ParticipantQuery query : participantQueryList) {
        participantArray.add(jsonSerializationContext.serialize(query));
    }//from w  w  w  .j av a 2 s.co  m

    base.add("participants", participantArray);
    return base;
}

From source file:com.facebook.ads.sdk.BatchRequest.java

License:Open Source License

public String executeInternal() throws APIException, IOException {

    Map<String, Object> params = new LinkedHashMap<String, Object>();
    Map<String, File> files = new HashMap<String, File>();
    JsonArray batch = new JsonArray();
    params.put("access_token", context.getAccessToken());
    if (context.hasAppSecret()) {
        params.put("appsecret_proof", context.getAppSecretProof());
    }/*from   ww  w  .j a va2s. c  o m*/
    params.put("include_headers", "false");
    for (Pair requestEntry : requests) {
        JsonObject batchElement = new JsonObject();
        BatchModeRequestInfo info = requestEntry.request.getBatchModeRequestInfo();

        batchElement.addProperty("method", info.method);
        batchElement.addProperty("relative_url", info.relativeUrl);
        batchElement.addProperty("name", requestEntry.name);

        if (info.body != null) {
            batchElement.addProperty("body", info.body);
        }

        if (info.files != null) {
            JsonObject attachedFiles = new JsonObject();
            for (Map.Entry entry : info.files.entrySet()) {
                File file = (File) entry.getValue();
                attachedFiles.addProperty("File" + files.size(), (String) entry.getKey());
                files.put("File" + files.size(), file);
            }
            batchElement.add("attached_files", attachedFiles);
        }

        batch.add(batchElement);
    }
    params.put("batch", batch.toString());
    params.putAll(files);
    return APIRequest.getExecutor().sendPost(context.getEndpointBase() + "/", params, context);
}

From source file:com.facebook.buck.rules.BuildInfoRecorder.java

License:Apache License

private String toJson(Iterable<String> values) {
    JsonArray out = new JsonArray();
    for (String str : values) {
        out.add(new JsonPrimitive(str));
    }/*from w  w  w.  j av a  2s  .com*/
    return out.toString();
}

From source file:com.facebook.buck.rules.BuildInfoRecorder.java

License:Apache License

private String toJson(Multimap<String, String> multimap) {
    JsonObject out = new JsonObject();
    for (Map.Entry<String, Collection<String>> entry : multimap.asMap().entrySet()) {
        JsonArray values = new JsonArray();
        for (String value : entry.getValue()) {
            values.add(new JsonPrimitive(value));
        }/*from w  w  w.  jav a  2 s.  c o m*/
        out.add(entry.getKey(), values);
    }
    return out.toString();
}

From source file:com.flipkart.android.proteus.toolbox.Utils.java

License:Apache License

public static JsonElement merge(JsonElement oldJson, JsonElement newJson, boolean useCopy, Gson gson) {

    JsonElement newDataElement;//www  . j a v  a2 s  . co m
    JsonArray oldArray;
    JsonArray newArray;
    JsonElement oldArrayItem;
    JsonElement newArrayItem;
    JsonObject oldObject;

    if (oldJson == null || oldJson.isJsonNull()) {
        return useCopy ? gson.fromJson(newJson, JsonElement.class) : newJson;
    }

    if (newJson == null || newJson.isJsonNull()) {
        newJson = JsonNull.INSTANCE;
        return newJson;
    }

    if (newJson.isJsonPrimitive()) {
        JsonPrimitive value;
        if (!useCopy) {
            return newJson;
        }
        if (newJson.getAsJsonPrimitive().isBoolean()) {
            value = new JsonPrimitive(newJson.getAsBoolean());
        } else if (newJson.getAsJsonPrimitive().isNumber()) {
            value = new JsonPrimitive(newJson.getAsNumber());
        } else if (newJson.getAsJsonPrimitive().isString()) {
            value = new JsonPrimitive(newJson.getAsString());
        } else {
            value = newJson.getAsJsonPrimitive();
        }
        return value;
    }

    if (newJson.isJsonArray()) {
        if (!oldJson.isJsonArray()) {
            return useCopy ? gson.fromJson(newJson, JsonArray.class) : newJson;
        } else {
            oldArray = oldJson.getAsJsonArray();
            newArray = newJson.getAsJsonArray();

            if (oldArray.size() > newArray.size()) {
                while (oldArray.size() > newArray.size()) {
                    oldArray.remove(oldArray.size() - 1);
                }
            }

            for (int index = 0; index < newArray.size(); index++) {
                if (index < oldArray.size()) {
                    oldArrayItem = oldArray.get(index);
                    newArrayItem = newArray.get(index);
                    oldArray.set(index, merge(oldArrayItem, newArrayItem, useCopy, gson));
                } else {
                    oldArray.add(newArray.get(index));
                }
            }
        }
    } else if (newJson.isJsonObject()) {
        if (!oldJson.isJsonObject()) {
            return useCopy ? gson.fromJson(newJson, JsonObject.class) : newJson;
        } else {
            oldObject = oldJson.getAsJsonObject();
            for (Map.Entry<String, JsonElement> entry : newJson.getAsJsonObject().entrySet()) {
                newDataElement = merge(oldObject.get(entry.getKey()), entry.getValue(), useCopy, gson);
                oldObject.add(entry.getKey(), newDataElement);
            }
        }
    } else {
        return useCopy ? gson.fromJson(newJson, JsonElement.class) : newJson;
    }

    return oldJson;
}

From source file:com.gemini.domain.dto.serialize.GeminiApplicationSerializer.java

@Override
public JsonElement serialize(GeminiApplicationDTO src, Type typeOfSrc, JsonSerializationContext context) {
    JsonObject appElement = new JsonObject();

    //the primitives
    appElement.addProperty("name", src.getName());
    appElement.addProperty("description", src.getDescription());
    appElement.addProperty("custom", src.getCustom());
    appElement.addProperty("backupSize", src.getBackupSize());
    appElement.addProperty("location", src.getLocation());

    //now the networks
    Gson gson = new GsonBuilder().registerTypeAdapter(GeminiNetworkDTO.class, new GeminiNetworkSerializer())
            .create();//ww  w  .  j  a  v a2 s .  c  o m
    JsonArray netArray = new JsonArray();
    src.getNetworks().stream().forEach(n -> netArray.add(gson.toJsonTree(n, GeminiNetworkDTO.class)));
    appElement.add("networks", netArray);

    return appElement;
}

From source file:com.gemini.domain.dto.serialize.GeminiEnvironmentSerializer.java

@Override
public JsonElement serialize(GeminiEnvironmentDTO src, Type typeOfSrc, JsonSerializationContext context) {
    JsonObject envElement = new JsonObject();

    //register all the required custom serializers
    Gson gson = new GsonBuilder().registerTypeAdapter(GeminiNetworkDTO.class, new GeminiNetworkSerializer())
            .registerTypeAdapter(GeminiApplicationDTO.class, new GeminiApplicationSerializer())
            .registerTypeAdapter(GeminiNetworkRouterDTO.class, new GeminiNetworkRouterSerializer())
            .registerTypeAdapter(GeminiSecurityGroupDTO.class, new GeminiSecurityGroupSerializer()).create();

    //first the primitives
    envElement.addProperty("name", src.getName());
    envElement.addProperty("type", src.getType().toString());
    envElement.addProperty("adminUserName", src.getAdminUserName());
    envElement.addProperty("adminPassword", src.getAdminPassword());
    envElement.addProperty("endPoint", src.getEndPoint());

    //now the security group objects
    JsonArray secGrpArray = new JsonArray();
    src.getSecurityGroups().stream().forEach(sg -> {
        JsonElement srcGrp = gson.toJsonTree(sg, GeminiSecurityGroupDTO.class);
        secGrpArray.add(srcGrp);
    });/*from  w  w w  . j  a  va  2  s.  c  o m*/
    envElement.add("securityGroups", secGrpArray);

    //the gateways
    JsonArray gatewayArray = new JsonArray();
    src.getGateways().stream().forEach(g -> {
        JsonElement gateway = gson.toJsonTree(g, GeminiNetworkDTO.class);
        gatewayArray.add(gateway);
    });
    envElement.add("gateways", gatewayArray);

    //the applications
    JsonArray appArray = new JsonArray();
    src.getApplications().stream().forEach(a -> {
        JsonElement app = gson.toJsonTree(a, GeminiApplicationDTO.class);
        appArray.add(app);
    });
    envElement.add("applications", appArray);

    //skip the orphan networks as it only has a meaning in-memory

    //the routers
    JsonArray routerArray = new JsonArray();
    src.getRouters().stream().forEach(r -> {
        JsonElement router = gson.toJsonTree(r, GeminiNetworkRouterDTO.class);
        routerArray.add(router);
    });
    envElement.add("routers", routerArray);

    return envElement;
}

From source file:com.gemini.domain.dto.serialize.GeminiNetworkRouterSerializer.java

@Override
public JsonElement serialize(GeminiNetworkRouterDTO src, Type typeOfSrc, JsonSerializationContext context) {
    JsonObject routerElement = new JsonObject();

    //register all the required custom serializers
    Gson gson = new GsonBuilder().registerTypeAdapter(GeminiNetworkDTO.class, new GeminiNetworkSerializer())
            .create();//  w  w  w  .ja v a 2 s . com

    //first the primitives
    routerElement.addProperty("name", src.getName());
    routerElement.addProperty("cloudID", src.getCloudID());
    routerElement.add("gateway", gson.toJsonTree(src.getGateway(), GeminiNetworkDTO.class));

    //now the routes
    JsonArray routeArray = new JsonArray();
    src.getRoutes().entrySet().stream().forEach(e -> routeArray.add(new JsonPrimitive(
            new StringBuilder().append(e.getKey()).append(",").append(e.getValue()).append("\n").toString())));
    routerElement.add("routes", routeArray);

    //now the interfaces - while each interface is a GeminiSubnetDTO object 
    //we will only add the name and cloudID because it is only a reference to 
    //a subnet created under applications
    JsonArray interfaceArray = new JsonArray();
    src.getInterfaces().stream().forEach(i -> {
        JsonObject intElement = new JsonObject();
        intElement.add("name", new JsonPrimitive(i.getName()));
        intElement.add("cloudID", new JsonPrimitive(i.getCloudID()));
        interfaceArray.add(intElement);
    });
    routerElement.add("interfaces", routeArray);

    return routerElement;
}