Example usage for com.google.gson JsonArray get

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

Introduction

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

Prototype

public JsonElement get(int i) 

Source Link

Document

Returns the ith element of the array.

Usage

From source file:com.arangodb.entity.EntityDeserializers.java

License:Apache License

private static JsonObject getFirstResultAsJsonObject(JsonObject obj) {
    if (obj.has("result")) {
        if (obj.get("result").isJsonArray()) {
            JsonArray result = obj.getAsJsonArray("result");

            if (result.size() > 0) {
                JsonElement jsonElement = result.get(0);
                if (jsonElement.isJsonObject()) {
                    return jsonElement.getAsJsonObject();
                }// w  ww.  ja v  a 2  s  . co  m
            }
        } else if (obj.get("result").isJsonObject()) {
            return obj.getAsJsonObject("result");
        }

    }
    return null;
}

From source file:com.arangodb.entity.EntityDeserializers.java

License:Apache License

private static List<PathEntity<Object, Object>> getPaths(JsonDeserializationContext context, JsonObject visited,
        Class<?> vertexClazz, Class<?> edgeClazz) {
    List<PathEntity<Object, Object>> pathEntities = new ArrayList<PathEntity<Object, Object>>();
    JsonArray paths = visited.getAsJsonArray("paths");
    if (!paths.equals(null)) {
        for (int i = 0, imax = paths.size(); i < imax; i++) {
            JsonObject path = paths.get(i).getAsJsonObject();
            PathEntity<Object, Object> pathEntity = new PathEntity<Object, Object>();

            if (path.has("edges")) {
                pathEntity.setEdges(getEdges(edgeClazz, context, path.getAsJsonArray("edges")));
            }//  ww w.  j  av  a 2  s .  c  o m
            if (path.has("vertices")) {
                pathEntity.setVertices(getVertices(vertexClazz, context, path.getAsJsonArray("vertices")));
            }

            pathEntities.add(pathEntity);
        }
    }
    return pathEntities;
}

From source file:com.arangodb.entity.EntityDeserializers.java

License:Apache License

private static List<VertexEntity<Object>> getVertices(Class<?> vertexClazz, JsonDeserializationContext context,
        JsonArray vertices) {
    List<VertexEntity<Object>> list = new ArrayList<VertexEntity<Object>>();
    if (!vertices.equals(null)) {
        for (int i = 0, imax = vertices.size(); i < imax; i++) {
            JsonObject vertex = vertices.get(i).getAsJsonObject();
            VertexEntity<Object> ve = getVertex(context, vertex, vertexClazz);
            list.add(ve);//from w ww  .  j  a  v  a2  s  .  c o m
        }
    }
    return list;
}

From source file:com.arangodb.entity.EntityDeserializers.java

License:Apache License

private static List<EdgeEntity<Object>> getEdges(Class<?> edgeClazz, JsonDeserializationContext context,
        JsonArray edges) {
    List<EdgeEntity<Object>> list = new ArrayList<EdgeEntity<Object>>();
    if (!edges.equals(null)) {
        for (int i = 0, imax = edges.size(); i < imax; i++) {
            JsonObject edge = edges.get(i).getAsJsonObject();
            EdgeEntity<Object> ve = deserializeBaseParameter(edge, new EdgeEntity<Object>());
            deserializeDocumentParameter(edge, ve);
            if (edgeClazz != null) {
                ve.setEntity(context.deserialize(edge, edgeClazz));
            } else {
                ve.setEntity(context.deserialize(edge, Object.class));
            }/*from w w  w . j  a  v  a 2  s .co  m*/
            list.add(ve);
        }
    }
    return list;
}

From source file:com.ardhi.businessgame.services.BusinessGameService.java

public String loadInstallmentDetails(HttpServletRequest req) {
    String val = "0";
    ArrayList<InstallmentEmployee> employees = new ArrayList<InstallmentEmployee>();
    ArrayList<InstallmentEquipment> equipments = new ArrayList<InstallmentEquipment>();
    ArrayList<String> data = new ArrayList<String>();

    SqlRowSet srs1 = db.getJdbc().queryForRowSet(
            "select installment_employee.id,employee,quality,operational,draw from installment_employee,list_employee,desc_employee,info_employee where installment='"
                    + req.getParameter("id")
                    + "' and installment_employee.id=list_employee.id and list_employee.[desc]=desc_employee.id and name=employee"),
            srs2;//from   w  ww. j a va  2  s. c om

    while (srs1.next()) {
        employees.add(new InstallmentEmployee(srs1.getString("id"), srs1.getString("employee"),
                srs1.getInt("quality"), srs1.getDouble("operational"), srs1.getString("draw")));
    }

    srs1 = db.getJdbc().queryForRowSet(
            "select installment_equipment.id,equipment,quality,durability,size,operational,draw from installment_equipment,desc_equipment,list_equipment,info_equipment where installment='"
                    + req.getParameter("id")
                    + "' and installment_equipment.id=list_equipment.id and list_equipment.[desc]=desc_equipment.id and name=equipment");
    while (srs1.next()) {
        equipments.add(new InstallmentEquipment(srs1.getString("id"), srs1.getString("equipment"),
                srs1.getInt("quality"), srs1.getDouble("durability"), srs1.getDouble("size"),
                srs1.getDouble("operational"), srs1.getString("draw")));
    }

    ArrayList<String> installmentIOdata = calculateInstallmentAndIOByIdInstallment(req.getParameter("id"));
    data.add(installmentIOdata.get(0));
    data.add(installmentIOdata.get(1));
    data.add(installmentIOdata.get(2));
    data.add(installmentIOdata.get(3));
    data.add(installmentIOdata.get(4));
    data.add(installmentIOdata.get(5));
    data.add(installmentIOdata.get(6));
    data.add(installmentIOdata.get(7));
    data.add(gson.toJson(equipments));
    data.add(gson.toJson(employees));

    if (installmentIOdata.get(0).equals("Petrol Power Plant")) {
        srs1 = db.getJdbc().queryForRowSet(
                "select subscription,tariff from installment where id='" + req.getParameter("id") + "'");
        double tariff, subscription;
        if (srs1.next()) {
            subscription = srs1.getDouble("subscription");
            tariff = srs1.getDouble("tariff");
        } else
            return "0";

        srs1 = db.getJdbc()
                .queryForRowSet("select id,type,[user],planned_supply from installment where supply='"
                        + req.getParameter("id") + "'");
        ArrayList<String> types = new ArrayList<String>(), users = new ArrayList<String>(),
                idSupplies = new ArrayList<String>();
        ArrayList<Double> supplies = new ArrayList<Double>();
        while (srs1.next()) {
            idSupplies.add(srs1.getString("id"));
            types.add(srs1.getString("type"));
            users.add(srs1.getString("user"));
            supplies.add(srs1.getDouble("planned_supply"));
        }

        data.add(gson.toJson(subscription));
        data.add(gson.toJson(tariff));
        data.add(gson.toJson(types));
        data.add(gson.toJson(users));
        data.add(gson.toJson(supplies));
        data.add(gson.toJson(idSupplies));

        val = gson.toJson(data);

        types = null;
        users = null;
        supplies = null;
        idSupplies = null;

    } else {
        srs1 = db.getJdbc().queryForRowSet(
                "select supply,planned_supply from installment where id='" + req.getParameter("id") + "'");
        ArrayList<String> idSupplies = new ArrayList<String>(), users = new ArrayList<String>(), tmpSupplies;
        ArrayList<Double> subscriptions = new ArrayList<Double>(), tariffs = new ArrayList<Double>(),
                availables = new ArrayList<Double>();
        JsonParser parser = new JsonParser();
        JsonArray array1;
        int tmp;
        double available, currentKwh;
        String currentSupply;

        if (srs1.next()) {
            currentKwh = srs1.getDouble("planned_supply");
            currentSupply = srs1.getString("supply");
        } else
            return "0";

        srs1 = db.getJdbc().queryForRowSet(
                "select id,[user],subscription,tariff from installment where type='Petrol Power Plant'");
        while (srs1.next()) {
            tmp = 0;
            tmpSupplies = calculateInstallmentAndIOByIdInstallment(srs1.getString("id"));
            array1 = parser.parse(tmpSupplies.get(5)).getAsJsonArray();
            for (int i = 0; i < array1.size(); i++) {
                if ((new Gson().fromJson(array1.get(i), String.class)).equals("Energy")) {
                    tmp = i;
                    break;
                }
            }
            array1 = parser.parse(tmpSupplies.get(6)).getAsJsonArray();
            available = new Gson().fromJson(array1.get(tmp), Double.class);
            srs2 = db.getJdbc().queryForRowSet(
                    "select planned_supply from installment where supply='" + srs1.getString("id") + "'");
            while (srs2.next())
                available -= srs2.getDouble("planned_supply");

            idSupplies.add(srs1.getString("id"));
            users.add(srs1.getString("user"));
            subscriptions.add(srs1.getDouble("subscription"));
            tariffs.add(srs1.getDouble("tariff"));
            availables.add(new BigDecimal(Double.valueOf(available)).setScale(2, BigDecimal.ROUND_HALF_EVEN)
                    .doubleValue());
            tmpSupplies = null;
        }

        data.add(gson.toJson(idSupplies));
        data.add(gson.toJson(users));
        data.add(gson.toJson(subscriptions));
        data.add(gson.toJson(tariffs));
        data.add(gson.toJson(availables));
        data.add(gson.toJson(currentKwh));
        data.add(gson.toJson(currentSupply));

        val = gson.toJson(data);

        idSupplies = null;
        users = null;
        tariffs = null;
        availables = null;
        currentSupply = null;
    }

    installmentIOdata = null;
    employees = null;
    equipments = null;
    data = null;
    srs1 = null;
    srs2 = null;

    gc();

    return val;
}

From source file:com.asakusafw.lang.inspection.json.PortReferenceAdapter.java

License:Apache License

@Override
public InspectionNode.PortReference deserialize(JsonElement json, Type typeOfT,
        JsonDeserializationContext context) throws JsonParseException {
    if (json.isJsonArray() == false) {
        throw new JsonParseException("port reference must be an array"); //$NON-NLS-1$
    }// ww w .  j av a 2  s  .c  o m
    JsonArray array = (JsonArray) json;
    if (array.size() != 2) {
        throw new JsonParseException("port reference must have just 2 elements"); //$NON-NLS-1$
    }
    String nodeId = array.get(0).getAsString();
    String portId = array.get(1).getAsString();
    return new PortReference(nodeId, portId);
}

From source file:com.auth0.android.lock.internal.configuration.ApplicationDeserializer.java

License:Open Source License

private List<Connection> parseStrategy(JsonElement json, JsonDeserializationContext context) {
    final JsonObject strategy = json.getAsJsonObject();
    String name = requiredValue("name", String.class, strategy, context);
    requiredValue("connections", Object.class, strategy, context);

    JsonArray connectionsArray = strategy.getAsJsonArray("connections");
    List<Connection> connections = new ArrayList<>();
    for (int i = 0; i < connectionsArray.size(); i++) {
        final JsonObject connectionJson = connectionsArray.get(i).getAsJsonObject();
        requiredValue("name", String.class, connectionJson, context);
        Type mapType = new TypeToken<LinkedTreeMap<String, Object>>() {
        }.getType();//from www .ja  va 2s . c o m
        Map<String, Object> values = context.deserialize(connectionJson, mapType);
        connections.add(Connection.newConnectionFor(name, values));
    }
    return connections;
}

From source file:com.baidu.cc.configuration.service.impl.VersionServiceImpl.java

License:Apache License

/**
 * ??./*from   w w  w .  j a  va2  s.  c  o m*/
 * 
 * @param file
 *            
 * @param versionId
 *            the version id
 * @throws IOException
 *             ?
 */
@Override
public void importFromFile(File file, Long versionId) throws IOException {
    byte[] byteArray = FileUtils.readFileToByteArray(file);

    Hex encoder = new Hex();
    try {
        byteArray = encoder.decode(byteArray);
    } catch (DecoderException e) {
        throw new IOException(e.getMessage());
    }
    String json = new String(byteArray, SysUtils.UTF_8);

    // parse from gson
    JsonParser jsonParser = new JsonParser();
    JsonElement je = jsonParser.parse(json);

    if (!je.isJsonArray()) {
        throw new RuntimeException("illegal json string. must be json array.");
    }

    JsonArray jsonArray = je.getAsJsonArray();

    int size = jsonArray.size();
    Version version = new Version();
    List<ConfigGroup> groups = new ArrayList<ConfigGroup>();
    ConfigGroup group;
    List<ConfigItem> items;
    ConfigItem item;

    for (int i = 0; i < size; i++) {
        JsonObject jo = jsonArray.get(i).getAsJsonObject();
        group = gson.fromJson(jo, ConfigGroup.class);

        // get sub configuration item
        JsonArray subItemsJson = jo.get(CONFIG_ITEMS_ELE).getAsJsonArray();

        int subSize = subItemsJson.size();
        items = new ArrayList<ConfigItem>();
        for (int j = 0; j < subSize; j++) {
            item = gson.fromJson(subItemsJson.get(j), ConfigItem.class);
            items.add(item);
        }

        group.setConfigItems(items);
        groups.add(group);
    }

    version.setConfigGroups(groups);
    configCopyService.copyConfigItemsFromVersion(version, versionId);
}

From source file:com.balajeetm.mystique.core.lever.MystiqueLever.java

License:Open Source License

/**
 * Update fields./*from w ww  .jav a2  s  .c o m*/
 *
 * @param source the source
 * @param dependencies the dependencies
 * @param aces the aces
 * @param fields the fields
 * @param path the path
 * @return the boolean
 */
public Boolean updateFields(JsonElement source, JsonObject dependencies, JsonObject aces,
        List<JsonElement> fields, JsonArray path) {
    Boolean isLoopy = Boolean.FALSE;
    try {
        JsonElement field = source;
        if (null != path) {
            if (path.size() > 0) {
                try {
                    for (int count = 0; count < path.size(); count++) {
                        JsonElement p = path.get(count);
                        if (isNumber(p)) {
                            field = get(field, p);
                        } else {
                            String key = asString(p);
                            if (count == 0 && MystiqueConstants.AT_DEPS.equals(key)) {
                                field = dependencies;
                                continue;
                            }
                            String ace = getAce(key);
                            if (null != ace) {
                                field = aces.get(ace);
                                continue;
                            }
                            if (isLoopy(key)) {
                                isLoopy = Boolean.TRUE;
                                fields.clear();
                                break;
                            } else {
                                p = getPathField(p, aces);
                                field = get(field, p);
                            }
                        }
                    }
                } catch (IllegalStateException e) {
                    log.info(String.format("Invalid json path %s for %s : %s", path, source, e.getMessage()),
                            e);
                    field = JsonNull.INSTANCE;
                }
            }
            fields.add(field);
        }
    }
    /** Would throw an exception for any invalid path, which is logged and ignored */
    catch (RuntimeException e) {
        log.warn(String.format("Error getting field from source for %s - %s. Skipping the same", path,
                e.getMessage()), e);
    }
    return isLoopy;
}

From source file:com.balajeetm.mystique.core.lever.MystiqueLever.java

License:Open Source License

/**
 * Gets the field.//from   w  w  w. j a v  a  2 s .  c om
 *
 * @param source the source
 * @param jPath the j path
 * @param deps the deps
 * @param aces the aces
 * @return the field
 */
public JsonElement getField(JsonElement source, JsonArray jPath, JsonObject deps, JsonObject aces) {
    JsonElement field = null;
    try {
        field = source;
        if (null != jPath) {
            for (int count = 0; count < jPath.size(); count++) {
                JsonElement path = jPath.get(count);
                if (isNumber(path)) {
                    field = get(field, path);
                } else {
                    String key = asString(path);
                    if (count == 0 && MystiqueConstants.AT_DEPS.equals(key)) {
                        field = deps;
                        continue;
                    }
                    String ace = getAce(key);
                    if (null != ace) {
                        field = aces.get(ace);
                        continue;
                    }
                    path = getPathField(path, aces);
                    field = get(field, path);
                }
            }
        }
    }
    /** Would throw an exception for any invalid path, which is logged and ignored */
    catch (RuntimeException e) {
        log.warn(String.format("Error getting field from source for %s - %s. Skipping the same", jPath,
                e.getMessage()), e);
        field = null;
    }
    return field;
}