Example usage for com.google.gson JsonElement getAsJsonObject

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

Introduction

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

Prototype

public JsonObject getAsJsonObject() 

Source Link

Document

convenience method to get this element as a JsonObject .

Usage

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

License:Open Source License

private POI deserializeCategories(Category list, JsonElement elem) {
    JsonObject json = null;/* w w w  .j av a 2 s.  co m*/
    JsonArray array;

    if (elem.isJsonObject() && elem.getAsJsonObject().has(CATEGORIES)) {
        array = elem.getAsJsonObject().get(CATEGORIES).getAsJsonArray();
        for (int i = 0; i < array.size(); i++) {
            Category cat = new Category();
            json = array.get(i).getAsJsonObject();
            getSinglePOI(cat, json);
            if (json.has(CATEGORIES)) {
                Category subs = (Category) deserializeCategories(new Category(), json);
                for (int j = 0; j < subs.getNumCategories(); j++)
                    cat.addCategory(subs.getCategory(j));
            }

            list.addCategory(cat);
        }
    }

    return list;
}

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

License:Open Source License

/**
 * Gson deserializer for the CitySDK Tourism POI Classes
 */// w  w  w. jav  a2s  .c o m
@Override
public Deserializable deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    JsonObject jObject = json.getAsJsonObject();
    Deserializable deserializable = (Deserializable) inferType(typeOfT);

    if (deserializable.getClass().equals(ListPOIS.class)) {
        deserializeListOfList((ListPOIS) deserializable, jObject);
    } else if (jObject.has(POI)) {
        deserializeListPois((ListPointOfInterest) deserializable, jObject);
    } else if (jObject.has(EVENT)) {
        deserializeListEvents((ListEvent) deserializable, jObject);
    } else if (jObject.has(ROUTES)) {
        deserializeListRoutes((ListRoute) deserializable, jObject);
    } else if (jObject.has(TOURISM)) {
        deserializeResources((Resources) deserializable, jObject);
    } else if (jObject.has(CATEGORIES)) {
        deserializeCategories((Category) deserializable, jObject);
    } else if (jObject.has(TAGS)) {
        deserializeTags((ListTag) deserializable, jObject);
    } else if (deserializable != null) {
        deserializePoiBased(deserializable, jObject);
    }

    return deserializable;
}

From source file:cl.emendare.cleancodegenerator.external.adapter.GsonJsonConverter.java

@Override
public Configuration toConfigurationObject() {

    JsonObject jsonObject = new JsonObject();

    try {/*from  ww w .  ja  va 2  s .c o  m*/
        JsonElement jsonElement = parser.parse(new FileReader(filePath));
        jsonObject = jsonElement.getAsJsonObject();
    } catch (Exception e) {

    }

    Configuration config = factory.create(jsonObject.get("language").toString().replace("\"", ""),
            jsonObject.get("domain").toString().replace("\"", ""),
            jsonObject.get("persistence").toString().replace("\"", ""),
            jsonObject.get("usecase").toString().replace("\"", ""),
            jsonObject.get("module").toString().replace("\"", ""),
            jsonObject.get("package").toString().replace("\"", ""),
            jsonObject.get("author").toString().replace("\"", ""));

    return config;
}

From source file:cl.niclabs.cb.common.MethodParser.java

License:Open Source License

@Override
public Method deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext context)
        throws JsonParseException {
    JsonObject jsonObject = jsonElement.getAsJsonObject();
    String method = jsonObject.get("method").getAsString();
    JsonElement argsJsonElement = jsonObject.get("args");
    switch (method) {
    case "OpenSession": {
        return methodFactory.makeOpenSessionMethod();
    }/*from   www. ja  v  a  2 s  .c o m*/
    case "CloseSession": {
        CloseSessionMethod.Args args = context.deserialize(argsJsonElement, CloseSessionMethod.Args.class);
        return methodFactory.makeCloseSessionMethod(args);
    }
    case "DeleteKeyPair": {
        DeleteKeyPairMethod.Args args = context.deserialize(argsJsonElement, DeleteKeyPairMethod.Args.class);
        return methodFactory.makeDeleteKeyMethod(args);
    }
    case "GenerateKeyPair": {
        GenerateKeyPairMethod.Args args = context.deserialize(argsJsonElement,
                GenerateKeyPairMethod.Args.class);
        return methodFactory.makeGenerateKeyPairMethod(args);
    }
    case "SignInit": {
        SignInitMethod.Args args = context.deserialize(argsJsonElement, SignInitMethod.Args.class);
        return methodFactory.makeSignInitMethod(args);
    }
    case "Sign": {
        SignMethod.Args args = context.deserialize(argsJsonElement, SignMethod.Args.class);
        return methodFactory.makeSignMethod(args);
    }
    case "FindKey": {
        FindKeyMethod.Args args = context.deserialize(argsJsonElement, FindKeyMethod.Args.class);
        return methodFactory.makeFindKeyMethod(args);
    }
    case "SeedRandom": {
        SeedRandomMethod.Args args = context.deserialize(argsJsonElement, SeedRandomMethod.Args.class);
        return methodFactory.makeSeedRandomMethod(args);
    }
    case "GenerateRandom": {
        GenerateRandomMethod.Args args = context.deserialize(argsJsonElement, GenerateRandomMethod.Args.class);
        return methodFactory.makeGenerateRandomMethod(args);
    }
    case "DigestInit": {
        DigestInitMethod.Args args = context.deserialize(argsJsonElement, DigestInitMethod.Args.class);
        return methodFactory.makeDigestInitMethod(args);
    }
    case "Digest": {
        DigestMethod.Args args = context.deserialize(argsJsonElement, DigestMethod.Args.class);
        return methodFactory.makeDigestMethod(args);
    }
    default: {
        throw new JsonParseException("Cannot parse method: " + method);
    }
    }

}

From source file:classes.analysis.Analysis.java

License:Open Source License

private static Analysis parseAnalysisGalaxyData(String origin, String emsuser, JsonObject analysisData) {
    JsonParser parser = new JsonParser();
    JsonArray provenance = (JsonArray) parser.parse(analysisData.get("provenance").getAsString());

    //STEP 1. Find the associations between the steps (inputs and outputs)
    HashMap<String, JsonElement> outputs = new HashMap<String, JsonElement>();
    JsonObject stepJSONobject;//w  w  w .jav a 2 s .  c o  m
    for (JsonElement step_json : provenance) {
        stepJSONobject = step_json.getAsJsonObject();
        for (JsonElement output : stepJSONobject.getAsJsonArray("outputs")) {
            outputs.put(output.getAsJsonObject().get("id").getAsString(), step_json);
        }

        if ("upload1".equalsIgnoreCase(stepJSONobject.get("tool_id").getAsString())) {
            stepJSONobject.remove("step_type");
            stepJSONobject.add("step_type", new JsonPrimitive("external_source"));
        } else {
            stepJSONobject.add("step_type", new JsonPrimitive("processed_data"));
        }
    }
    for (JsonElement step_json : provenance) {
        stepJSONobject = step_json.getAsJsonObject();
        for (JsonElement input : stepJSONobject.getAsJsonArray("inputs")) {
            String id = input.getAsJsonObject().get("id").getAsString();
            if (outputs.containsKey(id)) {
                if (!"external_source"
                        .equalsIgnoreCase(outputs.get(id).getAsJsonObject().get("step_type").getAsString())) {
                    outputs.get(id).getAsJsonObject().remove("step_type");
                    outputs.get(id).getAsJsonObject().add("step_type", new JsonPrimitive("intermediate_data"));
                }

                if (!stepJSONobject.has("used_data")) {
                    stepJSONobject.add("used_data", new JsonArray());
                }
                ((JsonArray) stepJSONobject.get("used_data")).add(new JsonPrimitive(
                        "STxxxx." + outputs.get(id).getAsJsonObject().get("id").getAsString()));
            }
        }
    }

    //STEP 2. Create the instances for the steps
    ArrayList<NonProcessedData> nonProcessedDataList = new ArrayList<NonProcessedData>();
    ArrayList<ProcessedData> processedDataList = new ArrayList<ProcessedData>();
    for (JsonElement step_json : provenance) {
        stepJSONobject = step_json.getAsJsonObject();
        if ("external_source".equalsIgnoreCase(stepJSONobject.get("step_type").getAsString())) {
            nonProcessedDataList.add(ExternalData.parseStepGalaxyData(stepJSONobject, analysisData, emsuser));
        } else if ("intermediate_data".equalsIgnoreCase(stepJSONobject.get("step_type").getAsString())) {
            nonProcessedDataList
                    .add(IntermediateData.parseStepGalaxyData(stepJSONobject, analysisData, emsuser));
        } else if ("processed_data".equalsIgnoreCase(stepJSONobject.get("step_type").getAsString())) {
            processedDataList.add(ProcessedData.parseStepGalaxyData(stepJSONobject, analysisData, emsuser));
        } else {
            throw new InstantiationError("Unknown step type");
        }
    }

    Collections.sort(nonProcessedDataList);
    Collections.sort(processedDataList);

    //STEP 3. Create the instance of analysis
    Analysis analysis = new Analysis();
    analysis.setAnalysisName(analysisData.get("ems_analysis_name").getAsString());
    analysis.setAnalysisType("Galaxy workflow");
    analysis.setNonProcessedData(nonProcessedDataList.toArray(new NonProcessedData[] {}));
    analysis.setProcessedData(processedDataList.toArray(new ProcessedData[] {}));
    analysis.setTags(new String[] { "imported" });
    analysis.setStatus("pending");

    return analysis;
}

From source file:classes.analysis.Analysis.java

License:Open Source License

private ArrayList<Pair> processElementContent(String templatesDir, String jsonObject)
        throws FileNotFoundException {
    ArrayList<Pair> elements = new ArrayList<Pair>();
    ArrayList<Pair> fields = processTemplateFile(templatesDir + File.separator + "analysis-form.json");
    JsonObject analysis = new JsonParser().parse(jsonObject).getAsJsonObject();

    elements.add(new Pair("Analysis details", "", 0, "title"));
    elements.add(new Pair("", "", 1, "section"));
    for (Pair field : fields) {
        elements.add(new Pair(field.value, this.getJsonElementAsString(analysis.get(field.label)), 2, "field"));
    }/*from w w  w. ja  v a 2  s.co m*/

    elements.add(new Pair("Steps in the analysis", "", 0, "title"));
    elements.add(new Pair("", "", 0, "section"));
    JsonArray steps = analysis.get("non_processed_data").getAsJsonArray();
    JsonObject step;
    for (JsonElement _step : steps) {
        step = _step.getAsJsonObject();
        String type = this.getJsonElementAsString(step.get("type"));
        if ("rawdata".equalsIgnoreCase(type)) {
            type = "Raw data step";
        } else {
            type = type.substring(0, 1).toUpperCase() + type.substring(1);
            type = type.replaceAll("_", " ");
        }
        elements.add(new Pair(type, "", 1, "title"));
        elements.add(new Pair("", "", 1, "section"));
        fields = processTemplateFile(
                templatesDir + File.separator + step.get("type").getAsString() + "-form.json");
        for (Pair field : fields) {
            elements.add(new Pair(field.value, this.getJsonElementAsString(step.get(field.label)), 2, "field"));
        }
    }

    steps = analysis.get("processed_data").getAsJsonArray();
    for (JsonElement _step : steps) {
        step = _step.getAsJsonObject();
        String type = this.getJsonElementAsString(step.get("type"));
        type = type.substring(0, 1).toUpperCase() + type.substring(1);
        type = type.replaceAll("_", " ");
        elements.add(new Pair(type, "", 1, "title"));
        elements.add(new Pair("", "", 1, "section"));
        fields = processTemplateFile(
                templatesDir + File.separator + step.get("type").getAsString() + "-form.json");
        for (Pair field : fields) {
            elements.add(new Pair(field.value, this.getJsonElementAsString(step.get(field.label)), 2, "field"));
        }
    }

    return elements;
}

From source file:classes.analysis.Analysis.java

License:Open Source License

private ArrayList<Pair> processTemplateFile(String template) throws FileNotFoundException {
    ArrayList<Pair> content = new ArrayList<Pair>();
    JsonParser parser = new JsonParser();
    JsonElement jsonElement = parser.parse(new FileReader(template));

    JsonArray sections = jsonElement.getAsJsonObject().get("content").getAsJsonArray();
    JsonArray fields;/*from   w  ww .  ja  va2s .  co m*/
    for (JsonElement element : sections) {
        fields = element.getAsJsonObject().get("fields").getAsJsonArray();
        for (JsonElement field : fields) {
            content.add(new Pair(field.getAsJsonObject().get("name").getAsString(),
                    field.getAsJsonObject().get("label").getAsString(), 0, "field"));
        }
    }

    return content;
}

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);
        }/* ww  w  .ja v  a2s.  com*/
    } 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);
        }/*from   w  w  w. j  a  v a  2  s . c  o  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.non_processed_data.ExternalData.java

License:Open Source License

public static ExternalData parseStepGalaxyData(JsonObject step_json_object, JsonObject analysisData,
        String emsuser) {//from  w  ww . j a  v  a2 s  .c om
    ExternalData step = new ExternalData("STxxxx." + step_json_object.get("id").getAsString());

    String prefix = analysisData.get("experiment_id").getAsString() + "/"
            + analysisData.get("analysis_id").getAsString() + "/";
    ArrayList<String> outputs = new ArrayList<String>();
    for (JsonElement output : step_json_object.get("outputs").getAsJsonArray()) {
        outputs.add(prefix + output.getAsJsonObject().get("file").getAsString().replaceAll(" ", "_") + "."
                + output.getAsJsonObject().get("extension").getAsString());
    }
    step.setFilesLocation(outputs.toArray(new String[] {}));

    Date dateNow = new Date();
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
    step.setSubmissionDate(dateFormat.format(dateNow));
    step.setLastEditionDate(dateFormat.format(dateNow));
    step.addOwner(new User(emsuser, ""));
    step.setStepName(step_json_object.get("tool_id").getAsString());
    step.setStepNumber(step_json_object.get("id").getAsInt());

    return step;
}