Example usage for com.google.gson JsonParser parse

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

Introduction

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

Prototype

@Deprecated
public JsonElement parse(JsonReader json) throws JsonIOException, JsonSyntaxException 

Source Link

Usage

From source file:cheerPackage.JSONUtils.java

public static String getJsonAttributeValue(String rawJson, String attribute)
        throws MalformedJsonException, JsonSyntaxException {
    //just a single Json Object
    JsonParser parser = new JsonParser();
    JsonElement json = parser.parse(rawJson);
    if (json.isJsonObject()) {
        return json.getAsJsonObject().get(attribute).getAsString();
    } else if (json.isJsonPrimitive()) {
        return json.getAsString();
    } else {//from   w w  w  . j a va  2  s  .  c o  m
        System.out.println(
                "This function only works on Json objects and primitives, use getValieFromArrayElement for arrays");
        return null;
    }
}

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 {/*  www  .  j  a va2 s .  c  om*/
            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  . j a  v a2s. 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 w w.  ja va  2 s . c o m
        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: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 va  2s.c  om
    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> 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  w  w  .java2 s .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:client.utils.CommandsSettings.java

License:Apache License

private void parseJson() throws IOException {
    JsonParser parser = new JsonParser();
    if (!this.fo.exists() || !this.fo.exists() || !(this.fi.length() > 0)) {
        this.writeDefault();
    }//  ww w.ja  va 2s  .  c om
    JsonElement obj = parser.parse(new FileReader("settings/commands.json"));
    this.commandsObj = obj.getAsJsonObject();
    if (this.commandsObj.toString().contains("null")) {
        System.out.println("Please set up your command settings first");
        System.exit(0);
    }
}

From source file:client.utils.CommandsSettings.java

License:Apache License

private void writeDefault() throws IOException {
    this.fo.mkdir();
    FileWriter set = new FileWriter(this.fi);
    JsonParser parser = new JsonParser();
    String defaultLocalization = "{\n" + "  \"Commands\": {\n" + "    \"points\": \"!points\",\n"
            + "    \"creator\": \"!creator\",\n" + "    \"banme\": \"!banme\",\n"
            + "    \"timeoutenemy\": \"!timeoutenemy\",\n" + "    \"baconspam\": \"!baconspam\",\n"
            + "    \"game\": \"!game\",\n" + "    \"title\": \"!title\"\n" + "  },\n" + "  \"Points\": {\n"
            + "    \"command_add\" : \"add\",\n" + "    \"command_remove\" : \"remove\",\n"
            + "    \"command_get\" : \"get\",\n" + "    \"command_addall\" : \"addall\",\n"
            + "    \"available\" : \"Available commands are: !points <add|remove|get|addall>\",\n"
            + "    \"Get\" : {\n" + "      \"permission_level\" : \"user\",\n"
            + "      \"message\" : \"%s has %s points!\"\n" + "    },\n" + "    \"Add\" : {\n"
            + "      \"permission_level\" : \"broadcaster\",\n"
            + "      \"message\" : \"Points successfully added!\"\n" + "    },\n" + "    \"Remove\" : {\n"
            + "      \"permission_level\" : \"broadcaster\",\n"
            + "      \"message\" : \"Points successfully removed!\"\n" + "    },\n" + "    \"AddAll\" : {\n"
            + "      \"permission_level\" : \"broadcaster\"\n" + "    },\n"
            + "    \"error\" : \"Something went wrong!\"\n" + "  },\n" + "  \"Creator\": {\n"
            + "    \"permission_level\" : \"creator\",\n"
            + "    \"message\" : \"My creator and owner is Hoffs!\"\n" + "  },\n" + "  \"TimeoutEnemy\": {\n"
            + "    \"permission_level\" : \"user\",\n"
            + "    \"initiator\" : \"%s 360 Sacrifice had to be made.\",\n"
            + "    \"enemy\" : \"%s 90 You have an enemy.\"\n" + "  }\n" + "}";
    JsonObject defaultSettings = parser.parse(defaultLocalization).getAsJsonObject();
    set.write(defaultSettings.toString());
    set.flush();/*  w ww .j a  v a2  s  .  c  o  m*/
    set.close();
    System.out.println("Please set up your settings first");
    System.exit(0);
}

From source file:client.utils.Settings.java

License:Apache License

private void getSettingsFromJson() throws IOException {
    JsonParser parser = new JsonParser();
    if (!this.fo.exists() || !this.fo.exists() || !(this.fi.length() > 0)) {
        this.writeDefault();
    }/*  ww w . j a  va 2s .  co m*/
    JsonElement obj = parser.parse(new FileReader("settings/config.json"));
    this.settings = obj.getAsJsonObject();
    if (this.settings.toString().contains("null")) {
        System.out.println("Please set up your settings first");
        System.exit(0);
    } else {
        this.user = this.settings.get("username").getAsString();
        this.token = this.settings.get("token").getAsString();
        this.clientid = this.settings.get("clientid").getAsString();
        this.oauth = this.settings.get("oauth").getAsString();
        this.pointsToAdd = this.settings.get("point_increment").getAsInt();
        this.interval = this.settings.get("point_interval").getAsInt();
        JsonArray arr = this.settings.get("channels").getAsJsonArray();
        for (JsonElement chan : arr) {
            channels.add(chan.getAsString());
        }
    }
}

From source file:client.utils.Settings.java

License:Apache License

private void writeDefault() throws IOException {
    this.fo.mkdir();
    FileWriter set = new FileWriter(this.fi);
    JsonParser parser = new JsonParser();
    JsonObject defaultSettings = parser.parse(
            "{\"username\":\"\",\"token\":\"oauth:\", \"clientid\":\"\", \"oauth\":\"\", \"point_increment\":\"10\", \"point_interval\":\"15\", \"channels\":[]}")
            .getAsJsonObject();//from   w  ww  .ja  v  a2 s .  c o m
    set.write(defaultSettings.toString());
    set.flush();
    set.close();
    System.out.println("Please set up your settings first");
    System.exit(0);
}