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: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 ww  .jav  a 2 s . c o  m
        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:classes.samples.BioCondition.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 + "biocondition-form.json");
    JsonObject analysis = new JsonParser().parse(jsonObject).getAsJsonObject();

    elements.add(new Pair("Biological conditions 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  . c o  m*/

    elements.add(new Pair("Samples in the analysis", "", 0, "title"));
    elements.add(new Pair("", "", 0, "section"));
    JsonArray samples = analysis.get("associatedBioreplicates").getAsJsonArray();
    JsonObject sample;
    for (JsonElement _step : samples) {
        sample = _step.getAsJsonObject();
        elements.add(new Pair("Sample", "", 1, "title"));
        elements.add(new Pair("", "", 1, "section"));
        fields = processTemplateFile(templatesDir + File.separator + "bioreplicate-form.json");
        for (Pair field : fields) {
            elements.add(
                    new Pair(field.value, this.getJsonElementAsString(sample.get(field.label)), 2, "field"));
        }

        elements.add(new Pair("Aliquouts", "", 2, "title"));
        elements.add(new Pair("", "", 2, "section"));
        JsonArray aliquots = sample.get("associatedAnalyticalReplicates").getAsJsonArray();
        for (JsonElement aliquot : aliquots) {
            sample = aliquot.getAsJsonObject();
            fields = processTemplateFile(templatesDir + File.separator + "analytical_replicate-form.json");
            for (Pair field : fields) {
                elements.add(new Pair(field.value, this.getJsonElementAsString(sample.get(field.label)), 3,
                        "field"));
            }
        }
    }

    return elements;
}

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 o  m
    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();//from www.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();
    }//from  www.j  a va2s . c o 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();//www  .  j a  v a2  s  .co  m
    set.write(defaultSettings.toString());
    set.flush();
    set.close();
    System.out.println("Please set up your settings first");
    System.exit(0);
}

From source file:clientcommunicator.operations.BuildCityRequest.java

@Override
public void deserialize(String JSON) throws JSONException {
    JsonObject obj = new JsonParser().parse(JSON).getAsJsonObject();
    this.playerIndex = new PlayerIdx(obj.get("playerIndex").getAsInt());
    JsonObject vertLocation = obj.get("vertexLocation").getAsJsonObject();
    int x = vertLocation.get("x").getAsInt();
    int y = vertLocation.get("y").getAsInt();
    String direction = vertLocation.get("direction").getAsString();
    this.location = new VertexLocation(new HexLocation(x, y), Abbreviate.unAbbreviateVertex(direction));
}

From source file:clientcommunicator.operations.BuildRoadRequest.java

@Override
public void deserialize(String JSON) throws JSONException {
    JsonObject obj = new JsonParser().parse(JSON).getAsJsonObject();
    this.playerIndex = new PlayerIdx(obj.get("playerIndex").getAsInt());
    JsonObject roadLocation = obj.getAsJsonObject("roadLocation");
    this.free = obj.get("free").getAsBoolean();
    int x = roadLocation.get("x").getAsInt();
    int y = roadLocation.get("y").getAsInt();
    String direction = roadLocation.get("direction").getAsString();
    this.location = new EdgeLocation(new HexLocation(x, y), Abbreviate.unAbbreviateEdge(direction));
}

From source file:clientcommunicator.operations.BuildSettlementRequest.java

@Override
public void deserialize(String JSON) throws JSONException {
    JsonObject obj = new JsonParser().parse(JSON).getAsJsonObject();
    this.free = obj.get("free").getAsBoolean();
    this.playerIndex = new PlayerIdx(obj.get("playerIndex").getAsInt());
    JsonObject vertLocation = obj.get("vertexLocation").getAsJsonObject();
    int x = vertLocation.get("x").getAsInt();
    int y = vertLocation.get("y").getAsInt();
    String direction = vertLocation.get("direction").getAsString();
    this.location = new VertexLocation(new HexLocation(x, y), Abbreviate.unAbbreviateVertex(direction));
}

From source file:clientcommunicator.operations.BuyDevCardRequest.java

@Override
public void deserialize(String JSON) throws JSONException {
    JsonObject obj = new JsonParser().parse(JSON).getAsJsonObject();
    this.playerIndex = new PlayerIdx(obj.get("playerIndex").getAsInt());
}