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.gemini.domain.dto.serialize.GeminiNetworkSerializer.java

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

    //first the primitives
    netElement.addProperty("name", src.getName());
    netElement.addProperty("description", src.getDescription());
    netElement.addProperty("type", src.getNetworkType());
    netElement.addProperty("cloudID", src.getCloudID());

    //now the subnets
    JsonArray subnetElements = new JsonArray();
    Gson gson = new GsonBuilder().registerTypeAdapter(GeminiSubnetDTO.class, new GeminiSubnetSerializer())
            .create();/*from   w w w. ja v a  2 s .  c o m*/
    src.getSubnets().stream().forEach(s -> {
        JsonElement sElement = gson.toJsonTree(s, GeminiSubnetDTO.class);
        subnetElements.add(sElement);
    });
    netElement.add("subnets", subnetElements);

    return netElement;
}

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

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

    //the primitives first
    secGrpElement.addProperty("name", src.getName());
    secGrpElement.addProperty("description", src.getDescription());
    secGrpElement.addProperty("cloudID", src.getCloudID());

    JsonArray secRuleArray = new JsonArray();
    Gson gson = new GsonBuilder()
            .registerTypeAdapter(GeminiSecurityGroupRuleDTO.class, new GeminiSecurityGroupRuleSerializer())
            .create();/*w  w  w .j a  v a  2  s .  c o  m*/
    src.getSecurityRules().stream()
            .forEach(sr -> secRuleArray.add(gson.toJsonTree(sr, GeminiSecurityGroupRuleDTO.class)));
    secGrpElement.add("rules", secRuleArray);

    return secGrpElement;
}

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

@Override
public JsonElement serialize(GeminiServerDTO src, Type typeOfSrc, JsonSerializationContext context) {
    JsonObject serverElement = new JsonObject();
    Gson gson = new Gson();

    serverElement.addProperty("name", src.getName());
    serverElement.addProperty("cloudID", src.getCloudID());
    serverElement.addProperty("description", src.getDescription());
    serverElement.addProperty("dateCreated", src.getDateCreated());
    serverElement.addProperty("address", src.getAddress());
    serverElement.addProperty("addressType", src.getAddressType());
    serverElement.addProperty("subnetMask", src.getSubnetMask());
    serverElement.addProperty("port", src.getPort());
    serverElement.addProperty("os", src.getOs());
    serverElement.addProperty("type", src.getType());
    serverElement.addProperty("admin", src.getAdmin());
    serverElement.addProperty("password", src.getPassword());
    serverElement.addProperty("serverType", src.getServerType());

    //the metadata, since it is a HashMap GSON requires a type token what type
    //of data is contained in the HashMap
    TypeToken<HashMap<String, String>> metadataType = new TypeToken<HashMap<String, String>>() {
    };//  ww w  .ja v  a 2s .c om
    serverElement.add("metadata", gson.toJsonTree(src.getMetadata(), metadataType.getType()));

    //the security group names
    JsonArray secGrpNames = new JsonArray();
    src.getSecurityGroupNames().stream().forEach(sg -> {
        secGrpNames.add(new JsonPrimitive(sg));
    });
    serverElement.add("securityGroupNames", secGrpNames);

    return serverElement;
}

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

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

    //the primitives
    allocPool.addProperty("start", src.getStart());
    allocPool.addProperty("end", src.getEnd());
    allocPool.addProperty("parent", src.getParent().getName());

    //ignore the parent object it is not needed for the JSON

    //now the servers
    Gson gson = new GsonBuilder().registerTypeAdapter(GeminiServerDTO.class, new GeminiServerSerializer())
            .create();//ww  w . j  a v  a  2  s .c  o m
    JsonArray serverArray = new JsonArray();
    src.getServers().stream().forEach(s -> serverArray.add(gson.toJsonTree(s, GeminiServerDTO.class)));
    allocPool.add("servers", serverArray);

    return allocPool;
}

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

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

    //first the primitives
    subnetElement.addProperty("name", src.getName());
    subnetElement.addProperty("cloudID", src.getCloudID());
    subnetElement.addProperty("cidr", src.getCidr());
    subnetElement.addProperty("gateway", src.getGateway());
    subnetElement.addProperty("enableDHCP", src.isEnableDHCP());
    subnetElement.addProperty("networkType", src.getNetworkType());

    //now the allocation pool
    Gson gson = new GsonBuilder().registerTypeAdapter(GeminiSubnetAllocationPoolDTO.class,
            new GeminiSubnetAllocationPoolSerializer()).create();
    JsonArray allocPool = new JsonArray();
    src.getAllocationPools().stream()//  w w  w.j  a  v a  2 s.  com
            .forEach(ap -> allocPool.add(gson.toJsonTree(ap, GeminiSubnetAllocationPoolDTO.class)));
    subnetElement.add("allocationPool", allocPool);

    return subnetElement;
}

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

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

    //setup our custom serializer
    Gson gson = new GsonBuilder()
            .registerTypeAdapter(GeminiEnvironmentDTO.class, new GeminiEnvironmentSerializer()).create();

    //the primitives
    tenantElement.addProperty("name", src.getName());
    tenantElement.addProperty("domain", src.getDomainName());

    //the users // w  w  w. j  a  v a  2  s .  com
    JsonArray userArray = new JsonArray();
    src.getUsers().stream().forEach(u -> {
        JsonObject jsonUser = new JsonObject();
        jsonUser.addProperty("name", u.getName());
        jsonUser.addProperty("userID", u.getUserID());
        jsonUser.addProperty("password", u.getPassword());
        jsonUser.addProperty("preferences", u.getPreferences());
        userArray.add(jsonUser);
    });
    tenantElement.add("users", userArray);

    //the environments
    JsonArray envArray = new JsonArray();
    src.getEnvironments().stream().forEach(e -> {
        JsonElement jsonEnv = gson.toJsonTree(e);
        envArray.add(jsonEnv);
    });
    tenantElement.add("environments", envArray);
    return tenantElement;
}

From source file:com.ghjansen.cas.ui.desktop.swing.SimulationParameterJsonAdapter.java

License:Open Source License

public final JsonElement serialize(final T object, final Type interfaceType,
        final JsonSerializationContext context) {
    UnidimensionalSimulationParameter parameter = (UnidimensionalSimulationParameter) object;
    final JsonObject member = new JsonObject();
    final JsonObject ruleTypeParameter = new JsonObject();
    final JsonObject ruleConfigurationParameter = new JsonObject();
    final JsonArray stateValues = new JsonArray();
    final JsonObject limitsParameter = new JsonObject();
    final JsonObject initialConditionParameter = new JsonObject();
    final JsonArray sequences = new JsonArray();
    ruleTypeParameter.addProperty("elementar", parameter.getRuleTypeParameter().isElementar());
    int[] states = parameter.getRuleConfigurationParameter().getStateValues();
    for (int i = 0; i < states.length; i++) {
        stateValues.add(new JsonPrimitive(
                parameter.getRuleConfigurationParameter().getStateValues()[states.length - 1 - i]));
    }//from  w ww  .ja  v a  2  s.  c  om
    ruleConfigurationParameter.add("stateValues", stateValues);
    limitsParameter.addProperty("cells", parameter.getLimitsParameter().getCells());
    limitsParameter.addProperty("iterations", parameter.getLimitsParameter().getIterations());
    for (int i = 0; i < parameter.getInitialConditionParameter().getSequences().size(); i++) {
        UnidimensionalSequenceParameter s = (UnidimensionalSequenceParameter) parameter
                .getInitialConditionParameter().getSequences().get(i);
        JsonObject sequence = new JsonObject();
        sequence.addProperty("initialPosition", s.getInitialPosition());
        sequence.addProperty("finalPosition", s.getFinalPosition());
        sequence.addProperty("value", s.getValue());
        sequences.add(sequence);
    }
    initialConditionParameter.add("sequences", sequences);

    member.add("ruleTypeParameter", ruleTypeParameter);
    member.add("ruleConfigurationParameter", ruleConfigurationParameter);
    member.add("limitsParameter", limitsParameter);
    member.add("initialConditionParameter", initialConditionParameter);
    return member;
}

From source file:com.github.abilityapi.abilityapi.external.Metrics.java

License:Open Source License

/**
 * Gets the plugin specific data./*from   ww  w.j  a  va 2s  .c  o  m*/
 * This method is called using Reflection.
 *
 * @return The plugin specific data.
 */
public JsonObject getPluginData() {
    JsonObject data = new JsonObject();

    String pluginName = plugin.getName();
    String pluginVersion = plugin.getVersion().orElse("unknown");

    data.addProperty("pluginName", pluginName);
    data.addProperty("pluginVersion", pluginVersion);

    JsonArray customCharts = new JsonArray();
    for (CustomChart customChart : charts) {
        // Add the data of the custom charts
        JsonObject chart = customChart.getRequestJsonObject(logger, logFailedRequests);
        if (chart == null) { // If the chart is null, we skip it
            continue;
        }
        customCharts.add(chart);
    }
    data.add("customCharts", customCharts);

    return data;
}

From source file:com.github.abilityapi.abilityapi.external.Metrics.java

License:Open Source License

/**
 * Collects the data and sends it afterwards.
 *//*  w ww .j av  a  2s  . c  o m*/
private void submitData() {
    final JsonObject data = getServerData();

    JsonArray pluginData = new JsonArray();
    // Search for all other bStats Metrics classes to get their plugin data
    for (Object metrics : knownMetricsInstances) {
        try {
            Object plugin = metrics.getClass().getMethod("getPluginData").invoke(metrics);
            if (plugin instanceof JsonObject) {
                pluginData.add((JsonObject) plugin);
            }
        } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException ignored) {
        }
    }

    data.add("plugins", pluginData);

    // Create a new thread for the connection to the bStats server
    new Thread(() -> {
        try {
            // Send the data
            sendData(data);
        } catch (Exception e) {
            // Something went wrong! :(
            if (logFailedRequests) {
                logger.warn("Could not submit plugin stats!", e);
            }
        }
    }).start();
}

From source file:com.github.aint.uchananalyzer.Parser.java

License:Open Source License

private static void save2Json(Collection<Post> posts, String pageNumber) {
    JsonArray array = new JsonArray();
    for (Post post : posts) {
        JsonObject innerJson = new JsonObject();
        innerJson.addProperty("author", post.getAuthor());
        innerJson.addProperty("text", post.getText());
        innerJson.addProperty("date", post.getDate().toString());
        innerJson.addProperty("image", post.isHasImage());
        array.add(innerJson);
    }/*from   www.j a  va  2s  .  c  om*/
    JsonObject json = new JsonObject();
    json.add(pageNumber, array);

    try (Writer writer = new FileWriter(pageNumber + ".json")) {
        new GsonBuilder().setPrettyPrinting().create().toJson(json, writer);
    } catch (IOException e) {
        e.printStackTrace();
    }
}