Example usage for com.google.gson JsonParser JsonParser

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

Introduction

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

Prototype

@Deprecated
public JsonParser() 

Source Link

Usage

From source file:cheerPackage.JSONUtils.java

public static String getValueFromArrayElement(String jsonArrayString, String attribute, int index)
        throws MalformedJsonException, JsonSyntaxException {
    JsonParser parser = new JsonParser();
    JsonElement json = parser.parse(jsonArrayString);
    if (json.isJsonArray()) {
        JsonElement firstItem = json.getAsJsonArray().get(index);
        if (firstItem.isJsonPrimitive()) {
            return firstItem.getAsString();
        } else if (firstItem.isJsonObject()) {
            return firstItem.getAsJsonObject().get(attribute).getAsString();
        } else {//w  w w  .  j a  v a  2s .  com
            System.out.println(
                    "This function only goes in 1 level (from Array to Object in array, or primitive).");
            return null;
        }
    } else {
        System.out.println("This function only works on Json arrays.");
        return null;
    }
}

From source file:cheerPackage.SocketServer.java

public String getAccessToken(String res, String title) throws JSONException {
    String value = null;/*  w  w w. ja  va2s .  c o m*/
    //just a single Json Object
    JsonParser parser = new JsonParser();
    JsonElement json = parser.parse(res);
    System.out.println(json);
    System.out.println("HERE IS MY value ---->>>");
    JsonObject object = json.getAsJsonObject();
    System.out.println(object.toString());
    object.toString();
    value = object.get(title).getAsString();

    return value;
}

From source file:cheerPackage.SocketServer.java

public void insertTournamentMatchesToDB(String res) throws JSONException {

    //cant deal with null information -->>>>>
    Matches match = new Matches();
    System.out.println("calling from socketServer ...");
    //convert res json Array
    JSONArray jsonArray = new JSONArray(res);
    //iterate JSONobj in the array inserting the to DB..
    for (int i = 0; i < jsonArray.length(); i++) {
        //System.out.println("from for loop the OBJECT");
        JsonParser parser = new JsonParser();
        JsonElement json = parser.parse(jsonArray.get(i).toString());
        JsonObject object = json.getAsJsonObject();
        object.toString();/*from w  ww.j av a2s .  com*/
        match.setId(object.get("id").getAsString());
        match.setType(object.get("type").getAsString());
        match.setDiscipline(object.get("discipline").getAsString());
        match.setStatus(object.get("status").getAsString());
        match.setTournamentId(object.get("tournament_id").getAsString());
        match.setNumber(object.get("number").getAsInt());
        match.setStageNumber(object.get("stage_number").getAsInt());
        match.setGroupNumber(object.get("group_number").getAsInt());
        match.setRoundNumber(object.get("round_number").getAsInt());

        //match.setTimezone("FI");
        /**if(object.get("timeZone").getAsString() == null){
        System.out.println("THE TIMEZONE...");
        match.setTimezone("FI");
        }else{
        match.setTimezone(object.get("timeZone").getAsString());
        }**/

        match.setMatchFormat("knockout");
        match.setOpponents(object.get("opponents").toString());
        /**Date today = new Date();
        today.setHours(0); today.setMinutes(0); today.setSeconds(0);
        match.setDate(today);**/
        viewCtrl.insertMatches(match);
    }

}

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

public GsonJsonConverter(String filePath) {
    parser = new JsonParser();
    this.filePath = filePath;
    factory = new ConfigurationFactory();
}

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;/*from  w  ww. j  a  v a 2s.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

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";
        }/*  w  w  w  .  j  a  va  2 s.  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 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"));
    }/* ww w.  j a v a2 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;/* ww w. j a  v a2 s  . c  o  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.Experiment.java

License:Open Source License

public String export(String tmpDir, String format, String templatesDir) throws Exception {
    String content = "";
    this.data_dir_pass = "*********";
    if ("json".equalsIgnoreCase(format)) {
        content = this.toJSON();
    } else if ("xml".equalsIgnoreCase(format)) {
        content = "<?xml version=\"1.0\"?>\n";
        content += "<experiment id=\"" + this.getExperimentID() + "\">\n";
        JsonObject experiment = new JsonParser().parse(this.toJSON()).getAsJsonObject();

        content += this.generateXMLContent(experiment, 1);
        content += "</experiment>\n";
    } else {/*from  ww w  .  j  ava2s.co m*/
        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.experiment_id + "." + format);
    PrintWriter writer = new PrintWriter(file);
    writer.println(content);
    writer.close();
    return file.getAbsolutePath();
}

From source file:classes.Experiment.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 + "experiment-form.json");
    JsonObject analysis = new JsonParser().parse(jsonObject).getAsJsonObject();

    elements.add(new Pair("Study 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"));
    }// w  w  w .j a v  a  2  s .  co m

    return elements;
}