Example usage for com.google.gson JsonElement getAsJsonArray

List of usage examples for com.google.gson JsonElement getAsJsonArray

Introduction

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

Prototype

public JsonArray getAsJsonArray() 

Source Link

Document

convenience method to get this element as a JsonArray .

Usage

From source file:citysdk.tourism.client.parser.POIDeserializer.java

License:Open Source License

private void deserializeListPois(ListPointOfInterest list, JsonObject jObject) {
    JsonElement jElement = jObject.get(POI);
    JsonArray jArray = jElement.getAsJsonArray();
    for (int i = 0; i < jArray.size(); i++) {
        PointOfInterest poi = new PointOfInterest();
        getSinglePOI(poi, jArray.get(i).getAsJsonObject());
        list.addPoi(poi);//from w w  w  .  j  av a  2 s  .  c  o  m
    }
}

From source file:citysdk.tourism.client.parser.POIDeserializer.java

License:Open Source License

private void deserializeListEvents(ListEvent list, JsonObject jObject) {
    JsonElement jElement = jObject.get(EVENT);
    JsonArray jArray = jElement.getAsJsonArray();
    for (int i = 0; i < jArray.size(); i++) {
        Event event = new Event();
        getSinglePOI(event, jArray.get(i).getAsJsonObject());
        list.addEvent(event);/*from www .j  a  v a2  s .  co m*/
    }
}

From source file:citysdk.tourism.client.parser.POIDeserializer.java

License:Open Source License

private void deserializeListRoutes(ListRoute list, JsonObject jObject) {
    JsonElement jElement = jObject.get(ROUTES);
    JsonArray jArray = jElement.getAsJsonArray();
    for (int i = 0; i < jArray.size(); i++) {
        Route route = new Route();
        getSinglePOI(route, jArray.get(i).getAsJsonObject());
        list.addRoute(route);/*from  www.j  a va 2s .  c  o  m*/
    }
}

From source file:citysdk.tourism.client.parser.POIDeserializer.java

License:Open Source License

private void deserializeResources(Resources resources, JsonObject jObject) {
    JsonElement jElement = jObject.get(TOURISM);
    JsonArray jArray = jElement.getAsJsonArray();
    for (int i = 0; i < jArray.size(); i++) {
        JsonObject ob = jArray.get(i).getAsJsonObject();
        HypermediaLink resource = getResource(ob);
        resources.addResource(resource);
    }//  w w  w.  j  av  a2  s  .  c o  m
}

From source file:classes.analysis.Analysis.java

License:Open Source License

public String export(String tmpDir, String format, String templatesDir) throws Exception {
    String content = "";
    if ("json".equalsIgnoreCase(format)) {
        content = this.toJSON();
    } else if ("xml".equalsIgnoreCase(format)) {
        content = "<?xml version=\"1.0\"?>\n";
        content += "<analysis id=\"" + this.getAnalysisID() + "\">\n";
        JsonObject analysis = new JsonParser().parse(this.toJSON()).getAsJsonObject();
        JsonElement non_processed_data = analysis.remove("non_processed_data");
        JsonElement processed_data = analysis.remove("processed_data");

        content += this.generateXMLContent(analysis, 1);
        content += "\t<steps>\n";
        content += "\t\t<non_processed_data>\n";
        for (JsonElement subelement : non_processed_data.getAsJsonArray()) {
            content += "\t\t<step>\n";
            content += this.generateXMLContent(subelement, 4);
            content += "\t\t\t</step>\n";
        }//from www. j  a  v a2s. c om
        content += "\t\t</non_processed_data>\n";
        content += "\t\t<processed_data>\n";
        for (JsonElement subelement : processed_data.getAsJsonArray()) {
            content += "\t\t<step>\n";
            content += this.generateXMLContent(subelement, 4);
            content += "\t\t\t</step>\n";
        }
        content += "\t\t</processed_data>\n";
        content += "\t</steps>\n";
        content += "</analysis>\n";
    } else {
        ArrayList<Pair> elements = this.processElementContent(templatesDir, this.toJSON());

        if ("html".equals(format)) {
            content = this.generateHTMLContent(elements);
        } else {
            throw new Exception(format + " is not a valid format");
        }
    }

    File file = new File(tmpDir + File.separator + this.analysis_id + "." + format);
    PrintWriter writer = new PrintWriter(file);
    writer.println(content);
    writer.close();
    return file.getAbsolutePath();
}

From source file:classes.analysis.Analysis.java

License:Open Source License

private String getJsonElementAsString(JsonElement element) {
    String value = "";
    if (element == null) {
        return "-";
    } else if (element.isJsonArray()) {
        JsonArray array = element.getAsJsonArray();
        for (JsonElement subelement : array) {
            value = this.getJsonElementAsString(subelement);
        }//from   w  w w  .ja  v  a  2  s  .  co  m
    } else if (element.isJsonObject()) {
        JsonObject object = element.getAsJsonObject();

        for (Map.Entry<String, JsonElement> entry : object.entrySet()) {
            value += entry.getKey() + ":" + this.getJsonElementAsString(entry.getValue());
        }
    } else if (element.isJsonPrimitive()) {
        value += element.toString().replaceAll("\"", "") + "\n";
    }
    return value;
}

From source file:classes.analysis.Analysis.java

License:Open Source License

private String generateXMLContent(JsonElement jsonCode, int level) {
    String content = "";
    if (jsonCode.isJsonPrimitive()) {
        content += jsonCode.getAsString();
    } else if (jsonCode.isJsonArray()) {
        content += "\n";
        for (JsonElement subelement : jsonCode.getAsJsonArray()) {
            content += this.generateXMLContent(subelement, level + 1);
        }//ww  w . j  a  v a  2 s .  co m
        content += "\n";
        content += String.join("", Collections.nCopies(level - 1, "\t"));
    } else if (jsonCode.isJsonObject()) {
        for (Map.Entry<String, JsonElement> entry : jsonCode.getAsJsonObject().entrySet()) {
            content += String.join("", Collections.nCopies(level, "\t")) + "<" + entry.getKey() + ">"
                    + this.generateXMLContent(entry.getValue(), level + 1) + "</" + entry.getKey() + ">\n";
        }
    }
    return content;
}

From source file:classes.analysis.Step.java

License:Open Source License

protected static String getParameterDescription(JsonElement parameter, int level) {
    if (parameter.isJsonPrimitive()) {
        return parameter.getAsString() + "\n";
    }//from   w w  w.j av  a 2s. c  om

    if (parameter.isJsonArray()) {
        String description = "";
        String prefix = "";
        for (int i = 0; i < level; i++) {
            prefix += "  ";
        }

        for (JsonElement element : parameter.getAsJsonArray()) {
            description += prefix + Step.getParameterDescription(element, level + 1);
        }

        return description;
    }

    if (parameter.isJsonObject()) {
        String description = "";
        String prefix = "";
        for (int i = 0; i < level; i++) {
            prefix += "  ";
        }

        for (Map.Entry<String, JsonElement> member : parameter.getAsJsonObject().entrySet()) {
            description += prefix + "- " + member.getKey() + ": "
                    + Step.getParameterDescription(member.getValue(), level + 1);
        }
        return description;
    }

    return "";
}

From source file:classes.samples.BioCondition.java

License:Open Source License

public String export(String tmpDir, String format, String templatesDir) throws Exception {
    String content = "";
    if ("json".equalsIgnoreCase(format)) {
        content = this.toJSON();
    } else if ("xml".equalsIgnoreCase(format)) {
        content = "<?xml version=\"1.0\"?>\n";
        content += "<biocondition id=\"" + this.getBioConditionID() + "\">\n";
        JsonObject biocondition = new JsonParser().parse(this.toJSON()).getAsJsonObject();
        JsonElement bioreplicates = biocondition.remove("associatedBioreplicates");
        biocondition.remove("associatedExperiments");

        content += this.generateXMLContent(biocondition, 1);
        content += "\t<samples>\n";
        for (JsonElement subelement : bioreplicates.getAsJsonArray()) {
            content += "\t\t<sample>\n";
            content += this.generateXMLContent(subelement, 4);
            content += "\t\t</sample>\n";
        }/*from  w w  w . j  av a  2  s .c om*/
        content += "\t</samples>\n";
        content += "</biocondition>\n";
    } else {
        ArrayList<Pair> elements = this.processElementContent(templatesDir, this.toJSON());

        if ("html".equals(format)) {
            content = this.generateHTMLContent(elements);
        } else {
            throw new Exception(format + " is not a valid format");
        }
    }

    File file = new File(tmpDir + File.separator + this.biocondition_id + "." + format);
    PrintWriter writer = new PrintWriter(file);
    writer.println(content);
    writer.close();
    return file.getAbsolutePath();
}

From source file:club.jmint.mifty.service.MiftyService.java

License:Apache License

public String callProxy(String method, String params, boolean isEncrypt) throws TException {
    //parse parameters and verify signature
    CrossLog.logger.debug("callProxy: " + method + "(in: " + params + ")");
    JsonObject ip;/*from  w  w w.  ja  v  a 2 s .com*/
    try {
        ip = parseInputParams(params, isEncrypt);
    } catch (CrossException ce) {
        return buildOutputByCrossException(ce);
    }

    //extract all parameters
    HashMap<String, String> inputMap = new HashMap<String, String>();
    Iterator<Entry<String, JsonElement>> it = ip.entrySet().iterator();
    Entry<String, JsonElement> en = null;
    String key;
    JsonElement je;
    while (it.hasNext()) {
        en = it.next();
        key = en.getKey();
        je = en.getValue();
        if (je.isJsonArray()) {
            inputMap.put(key, je.getAsJsonArray().toString());
            //System.out.println(je.getAsJsonArray().toString());
        } else if (je.isJsonNull()) {
            inputMap.put(key, je.getAsJsonNull().toString());
            //System.out.println(je.getAsJsonNull().toString());
        } else if (je.isJsonObject()) {
            inputMap.put(key, je.getAsJsonObject().toString());
            //System.out.println(je.getAsJsonObject().toString());
        } else if (je.isJsonPrimitive()) {
            inputMap.put(key, je.getAsJsonPrimitive().getAsString());
            //System.out.println(je.getAsJsonPrimitive().toString());
        } else {
            //unkown type;
        }
    }

    //execute specific method
    Method[] ma = this.getClass().getMethods();
    int idx = 0;
    for (int i = 0; i < ma.length; i++) {
        if (ma[i].getName().equals(method)) {
            idx = i;
            break;
        }
    }
    HashMap<String, String> outputMap = null;
    try {
        Method m = this.getClass().getDeclaredMethod(method, ma[idx].getParameterTypes());
        outputMap = (HashMap<String, String>) m.invoke(this, inputMap);
        CrossLog.logger.debug("callProxy: " + method + "() executed.");
    } catch (NoSuchMethodException nsm) {
        CrossLog.logger.error("callProxy: " + method + "() not found.");
        CrossLog.printStackTrace(nsm);
        return buildOutputError(ErrorCode.CROSSING_ERR_INTERFACE_NOT_FOUND.getCode(),
                ErrorCode.CROSSING_ERR_INTERFACE_NOT_FOUND.getInfo());
    } catch (Exception e) {
        CrossLog.logger.error("callProxy: " + method + "() executed with exception.");
        CrossLog.printStackTrace(e);
        if (e instanceof CrossException) {
            return buildOutputByCrossException((CrossException) e);
        } else {
            return buildOutputError(ErrorCode.COMMON_ERR_UNKOWN.getCode(),
                    ErrorCode.COMMON_ERR_UNKOWN.getInfo());
        }
    }
    //if got error then return immediately
    String ec = outputMap.get("errorCode");
    String ecInfo = outputMap.get("errorInfo");
    if ((ec != null) && (!ec.isEmpty())) {
        return buildOutputError(Integer.parseInt(ec), ecInfo);
    }

    //build response parameters
    JsonObject op = new JsonObject();
    Iterator<Entry<String, String>> ito = outputMap.entrySet().iterator();
    Entry<String, String> eno = null;
    JsonParser jpo = new JsonParser();
    JsonElement jeo = null;
    while (ito.hasNext()) {
        eno = ito.next();
        try {
            jeo = jpo.parse(eno.getValue());
        } catch (JsonSyntaxException e) {
            //            MyLog.logger.error("callProxy: Malformed output parameters["+eno.getKey()+"].");
            //            return buildOutputError(ErrorCode.COMMON_ERR_PARAM_MALFORMED.getCode(), 
            //                  ErrorCode.COMMON_ERR_PARAM_MALFORMED.getInfo());
            //we do assume that it should be a common string
            jeo = new JsonPrimitive(eno.getValue());
        }
        op.add(eno.getKey(), jeo);
    }

    String output = null;
    try {
        output = buildOutputParams(op, isEncrypt);
    } catch (CrossException ce) {
        return buildOutputByCrossException(ce);
    }
    CrossLog.logger.debug("callProxy: " + method + "(out: " + output + ")");
    return output;
}