Example usage for com.google.gson JsonArray JsonArray

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

Introduction

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

Prototype

public JsonArray() 

Source Link

Document

Creates an empty JsonArray.

Usage

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 . ja v a2 s  . com*/
    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 v a 2s .com
    });
    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();//from  w  ww .  j  a  v  a 2  s  . c o  m

    //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;
}

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  ww  w  . java  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 ww.j  a  v a2 s . c om*/
    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 . jav  a  2  s .  c o m
    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();//  w w  w .  j  a v a 2  s  .c  om
    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()/*ww w .  j a  v a 2s . 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 //  www.  ja  va2s  .  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]));
    }/* ww  w.  j  a  va 2  s . co  m*/
    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;
}