Example usage for com.google.gson JsonObject add

List of usage examples for com.google.gson JsonObject add

Introduction

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

Prototype

public void add(String property, JsonElement value) 

Source Link

Document

Adds a member, which is a name-value pair, to self.

Usage

From source file:ccm.pay2spawn.util.JsonNBTHelper.java

License:Open Source License

public static JsonObject fixNulls(JsonObject object) {
    JsonObject newObject = new JsonObject();
    for (Map.Entry<String, JsonElement> entry : object.entrySet())
        newObject.add(entry.getKey(), fixNulls(entry.getValue()));
    return newObject;
}

From source file:ccm.pay2spawn.util.RewardsDB.java

License:Open Source License

public RewardsDB(File file) {
    editable = true;/*from   www  .  j a  v a2  s . co  m*/
    try {
        if (file.exists()) {
            try {
                JsonArray rootArray = JSON_PARSER.parse(new FileReader(file)).getAsJsonArray();

                for (JsonElement element : rootArray) {
                    Reward reward = new Reward(element.getAsJsonObject());
                    map.put(reward.getAmount(), reward);
                }
            } catch (Exception e) {
                Pay2Spawn.getLogger().warn("ERROR TYPE 2: There is an error in your config file.");
                e.printStackTrace();
            }
        } else {
            //noinspection ResultOfMethodCallIgnored
            file.createNewFile();
            JsonArray rootArray = new JsonArray();

            JsonObject group = new JsonObject();
            group.addProperty("name", "EXAMPLE");
            group.addProperty("amount", 2);
            group.addProperty("countdown", 10);
            group.addProperty("message", "&a[$name donated $$amount]");
            JsonArray rewards = new JsonArray();
            for (TypeBase type : TypeRegistry.getAllTypes()) {
                JsonObject element = new JsonObject();
                element.addProperty("type", type.getName());
                //noinspection unchecked
                element.add("data", JsonNBTHelper.parseNBT(type.getExample()));
                rewards.add(element);
            }
            group.add("rewards", rewards);
            rootArray.add(group);

            BufferedWriter bw = new BufferedWriter(new FileWriter(file));
            bw.write(GSON.toJson(rootArray));
            bw.close();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:cf.adriantodt.David.modules.db.DBModule.java

License:LGPL

private DBModule() {
    mainConfig = ConfigUtils.get("main",
            ImmutableMap.<String, java.util.function.Predicate<JsonElement>>builder()
                    .put("ownerID", ConfigUtils::isJsonString).put("token", ConfigUtils::isJsonString).build(),
            () -> {//from  w  w w  . ja va2  s  .c  om
                JsonObject object = new JsonObject();
                object.add("ownerID", null);
                object.add("token", null);
                return object;
            }, false, true);

    JsonObject dbConfig = ConfigUtils.get("db",
            ImmutableMap.<String, java.util.function.Predicate<JsonElement>>builder()
                    .put("hostname", ConfigUtils::isJsonString)
                    .put("port", element -> ConfigUtils.isJsonNumber(element) && element.getAsInt() != 0)
                    .build(),
            () -> {
                JsonObject object = new JsonObject();
                object.addProperty("hostname", "localhost");
                object.addProperty("port", 28015);
                return object;
            }, true, true);

    conn = r.connection().hostname(dbConfig.get("hostname").getAsString()).port(dbConfig.get("port").getAsInt())
            .db("bot").connect();
}

From source file:cf.adriantodt.David.modules.db.I18nModule.java

License:LGPL

public static String generateJsonDump() {
    System.out.println();// w ww. j  a va2 s  .  co m
    JsonObject json = new JsonObject();
    JsonObject parentsJson = new JsonObject();
    JsonObject localizations = new JsonObject();

    parents.forEach(parentsJson::addProperty);
    locales.forEach((k, v) -> {
        JsonObject localization = new JsonObject();
        v.forEach(localization::addProperty);
        localizations.add(k, localization);
    });

    json.add("parents", parentsJson);
    json.add("localizations", localizations);
    return json.toString();
}

From source file:ch.cern.db.flume.source.deserializer.RecoveryManagerDeserializer.java

License:GNU General Public License

private JsonArray recoveryManagerReportsToJSON(List<RecoveryManagerReport> recoveryManagerReports) {
    JsonArray array = new JsonArray();

    for (RecoveryManagerReport recoveryManagerReport : recoveryManagerReports) {
        JsonObject element = new JsonObject();

        element.addProperty("startingTime", JSONUtils.to(recoveryManagerReport.getStartingTime()));

        List<Pair<Integer, String>> rmans = recoveryManagerReport.getRMANs();
        element.add("RMAN-", toJSON(rmans));
        element.add("ORA-", toJSON(recoveryManagerReport.getORAs()));

        element.addProperty("finishTime", JSONUtils.to(recoveryManagerReport.getFinishTime()));
        element.addProperty("returnCode", recoveryManagerReport.getReturnCode());
        element.addProperty("status", rmans.size() == 0 ? "Successful" : "Failed");

        array.add(element);/*w ww.j  a va  2  s . c o m*/
    }

    return array;
}

From source file:ch.eitchnet.csvrestendpoint.marshaller.CsvDataToHeaderJsonMarshaller.java

License:Apache License

@Override
public JsonObject marshall(CSVParser csvParser) {

    JsonObject root = new JsonObject();
    root.addProperty("msg", "-");

    JsonArray data = new JsonArray();

    Set<Entry<String, Integer>> header = csvParser.getHeaderMap().entrySet();
    for (Entry<String, Integer> entry : header) {
        data.add(new JsonPrimitive(entry.getKey()));
    }/*from  ww w .j a  va  2s  . co m*/

    root.add("data", data);

    return root;
}

From source file:ch.eitchnet.csvrestendpoint.marshaller.CsvDataToJsonMarshaller.java

License:Apache License

@Override
public JsonObject marshall(CSVParser csvParser) {

    // validate any references to column names
    Map<String, Integer> headerMap = csvParser.getHeaderMap();
    validate(headerMap);//  w  w  w . j a v a 2s .c o m

    // filter
    List<CSVRecord> allRecords = new ArrayList<>();
    long dataSetSize = 0;
    for (CSVRecord record : csvParser) {
        dataSetSize++;
        if (isSelected(headerMap, record))
            allRecords.add(record);
    }

    // sort
    if (StringHelper.isNotEmpty(sortBy)) {
        Integer columnIndex = headerMap.get(sortBy);
        if (this.ascending)
            allRecords.sort((r1, r2) -> r1.get(columnIndex).compareTo(r2.get(columnIndex)));
        else
            allRecords.sort((r1, r2) -> r2.get(columnIndex).compareTo(r1.get(columnIndex)));
    }

    // paging
    Paging<CSVRecord> paging = Paging.asPage(allRecords, this.pageSize, this.page);

    // get page
    List<CSVRecord> page = paging.getPage();

    // build JSON response
    JsonObject root = new JsonObject();
    root.addProperty("msg", "-");
    root.addProperty("draw", this.draw);
    root.addProperty("dataSetSize", dataSetSize);
    root.addProperty("nrOfElements", paging.getNrOfElements());

    if (StringHelper.isNotEmpty(sortBy))
        root.addProperty("sortBy", this.sortBy);
    root.addProperty("ascending", this.ascending);
    root.addProperty("nrOfPages", paging.getNrOfPages());
    root.addProperty("pageSize", paging.getPageSize());
    root.addProperty("page", paging.getPageToReturn());

    // prune any unwanted columns
    if (!this.returnFields.isEmpty()) {
        headerMap.keySet().retainAll(this.returnFields);
    }

    // add items
    JsonArray data = new JsonArray();
    for (CSVRecord record : page) {
        JsonObject element = new JsonObject();
        for (Entry<String, Integer> entry : headerMap.entrySet()) {
            String column = entry.getKey();
            String value = record.get(entry.getValue());
            element.addProperty(column, value);
        }
        data.add(element);
    }
    root.add("data", data);

    return root;
}

From source file:ch.eitchnet.csvrestendpoint.marshaller.CsvNamesToJsonMarshaller.java

License:Apache License

public JsonObject marshall(List<String> names) {

    JsonObject root = new JsonObject();
    root.addProperty("msg", "-");

    JsonArray data = new JsonArray();

    for (String name : names) {
        data.add(new JsonPrimitive(name));
    }/* w  ww. ja va  2 s  . com*/

    root.add("data", data);

    return root;
}

From source file:ch.epfl.leb.sass.models.fluorophores.internal.DefaultFluorophore.java

License:Open Source License

@Override
public JsonElement serialize(DefaultFluorophore src, Type typeOfSrc, JsonSerializationContext context) {
    JsonObject result = new JsonObject();
    result.add("id", new JsonPrimitive(src.getId()));
    result.add("x", new JsonPrimitive(src.x));
    result.add("y", new JsonPrimitive(src.y));
    result.add("z", new JsonPrimitive(src.z));
    result.add("currentState", new JsonPrimitive(src.getCurrentState()));
    result.add("maxPhotonsPerFrame", new JsonPrimitive(src.getSignal()));
    result.add("bleached", new JsonPrimitive(src.isBleached()));
    result.add("emitting", new JsonPrimitive(src.isOn()));
    result.add("onTime", new JsonPrimitive(src.getOnTimeThisFrame()));
    result.add("photonsEmittedLastFrame", new JsonPrimitive(src.getPhotonsThisFrame()));
    return result;
}

From source file:ch.epfl.leb.sass.models.fluorophores.internal.PhysicalFluorophore.java

License:Open Source License

@Override
public JsonElement serialize(PhysicalFluorophore src, Type typeOfSrc, JsonSerializationContext context) {
    JsonObject result = new JsonObject();
    result.add("id", new JsonPrimitive(src.getId()));
    result.add("x", new JsonPrimitive(src.x));
    result.add("y", new JsonPrimitive(src.y));
    result.add("z", new JsonPrimitive(src.z));
    result.add("currentState", new JsonPrimitive(src.getCurrentState()));
    result.add("maxPhotonsPerFrame", new JsonPrimitive(src.getSignal()));
    result.add("bleached", new JsonPrimitive(src.isBleached()));
    result.add("emitting", new JsonPrimitive(src.isOn()));
    result.add("onTime", new JsonPrimitive(src.getOnTimeThisFrame()));
    result.add("photonsEmittedLastFrame", new JsonPrimitive(src.getPhotonsThisFrame()));
    return result;
}