Example usage for com.google.gson JsonArray JsonArray

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

Introduction

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

Prototype

public JsonArray() 

Source Link

Document

Creates an empty JsonArray.

Usage

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

License:Open Source License

/**
 * Fill in variables from a donation//from  w w  w . ja v  a  2  s . co  m
 *
 * @param dataToFormat data to be formatted
 * @param donation     the donation data
 *
 * @return the fully var-replaced JsonElement
 */
public static JsonElement formatText(JsonElement dataToFormat, Donation donation, Reward reward) {
    if (dataToFormat.isJsonPrimitive() && dataToFormat.getAsJsonPrimitive().isString()) {
        return new JsonPrimitive(Helper.formatText(dataToFormat.getAsString(), donation, reward));
    }
    if (dataToFormat.isJsonArray()) {
        JsonArray out = new JsonArray();
        for (JsonElement element : dataToFormat.getAsJsonArray()) {
            out.add(formatText(element, donation, reward));
        }
        return out;
    }
    if (dataToFormat.isJsonObject()) {
        JsonObject out = new JsonObject();
        for (Map.Entry<String, JsonElement> entity : dataToFormat.getAsJsonObject().entrySet()) {
            out.add(entity.getKey(), Helper.formatText(entity.getValue(), donation, reward));
        }
        return out;
    }
    return dataToFormat;
}

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

License:Open Source License

public static JsonPrimitive parseNBT(NBTTagIntArray nbtArray) {
    JsonArray jsonArray = new JsonArray();
    for (int i : nbtArray.func_150302_c())
        jsonArray.add(new JsonPrimitive(i));
    return new JsonPrimitive(NBTTypes[nbtArray.getId()] + ":" + jsonArray.toString());
}

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

License:Open Source License

public static JsonPrimitive parseNBT(NBTTagByteArray nbtArray) {
    JsonArray jsonArray = new JsonArray();
    for (int i : nbtArray.func_150292_c())
        jsonArray.add(new JsonPrimitive(i));
    return new JsonPrimitive(NBTTypes[nbtArray.getId()] + jsonArray.toString());
}

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

License:Open Source License

public static JsonArray parseNBT(NBTTagList nbtArray) {
    JsonArray jsonArray = new JsonArray();
    for (int i = 0; i < nbtArray.tagCount(); i++) {
        // TODO: Yell at Mojang for this...
        switch (nbtArray.func_150303_d()) {
        case 5:/*  w w w .  j  av a2  s  .  com*/
            jsonArray.add(parseNBT(new NBTTagFloat(nbtArray.func_150308_e(i))));
            break;
        case 6:
            jsonArray.add(parseNBT(new NBTTagDouble(nbtArray.func_150309_d(i))));
            break;
        case 8:
            jsonArray.add(parseNBT(new NBTTagString(nbtArray.getStringTagAt(i))));
            break;
        case 10:
            jsonArray.add(parseNBT(nbtArray.getCompoundTagAt(i)));
            break;
        case 11:
            jsonArray.add(parseNBT(new NBTTagIntArray(nbtArray.func_150306_c(i))));
            break;
        }

    }
    return jsonArray;
}

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

License:Open Source License

public static JsonArray fixNulls(JsonArray array) {
    JsonArray newArray = new JsonArray();
    for (JsonElement element : array)
        newArray.add(fixNulls(element));
    return newArray;
}

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

License:Open Source License

public RewardsDB(File file) {
    editable = true;/*from w w w  .  j a  va 2s  . c  om*/
    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.brforgers.bot.handlers.BotIntercommns.java

License:LGPL

private static void transaction(User bot, String msg) {
    if (msg.length() < (IC_CALL.length() + 1) || !msg.startsWith(IC_CALL))
        return;//  www  .j av  a 2 s . c om
    msg = msg.substring(IC_CALL.length());

    char opcode = msg.charAt(0);

    if (opcode == SUPPORTS_IC) {
        pm(bot, IC_CALL + SUPPORTED);
        return;
    }

    if (opcode == TRANSACTION) {
        if (msg.length() < 2)
            return;
        msg = msg.substring(1);
        opcode = msg.charAt(0);

        if (opcode == TRANSACTION_CHECK) {
            msg = msg.substring(2);
            int oldHash = Statistics.parseInt(msg, 0);
            if (oldHash != self.toString().hashCode()) {
                pm(bot, IC_CALL + TRANSACTION + TRANSACTION_UPDATE);
            }
            return;
        }

        if (opcode == TRANSACTION_GET) {
            msg = msg.substring(2);

            if (msg.equals("p")) {
                pm(bot, IC_CALL + TRANSACTION + TRANSACTION_SET + "$p=" + self.p);
            }

            if (msg.equals("cmds")) {
                boolean first = true;
                JsonArray array = new JsonArray();
                String state = array.toString();

                for (int i = 0; i < self.cmds.size(); i++) {
                    state = array.toString();
                    array.add(new JsonPrimitive(self.cmds.get(i)));

                    if (array.toString().length() > 1900) {
                        pm(bot, IC_CALL + TRANSACTION + TRANSACTION_SET + "$cmds" + (first ? "=" : "+")
                                + state);
                        first = false;
                        array = new JsonArray();
                        array.add(new JsonPrimitive(self.cmds.get(i)));
                    }
                }

                pm(bot, IC_CALL + TRANSACTION + TRANSACTION_SET + "$cmds" + (first ? "=" : "+") + state);
                return;
            }

            return;
        }

        if (opcode == TRANSACTION_SET) {
            msg = msg.substring(2);
            if (msg.startsWith("p")) {
                get(bot).p = Statistics.parseInt(msg.substring(2), 0);
                return;
            }

            if (msg.startsWith("cmds")) {
                msg = msg.substring("cmds".length());
                opcode = msg.charAt(0);
                msg = msg.substring(1);
                JsonArray array = new JsonParser().parse(msg).getAsJsonArray();

                if (opcode == '=') {
                    get(bot).cmds.clear();
                }

                if (opcode == '-') {
                    get(bot).cmds.removeIf(s -> StreamSupport.stream(array.spliterator(), false)
                            .anyMatch(j -> s.equals(j.getAsString()))); //I Know, it's lazy.
                }

                if (opcode == '=' || opcode == '+') {
                    get(bot).cmds.addAll(StreamSupport.stream(array.spliterator(), false)
                            .map(JsonElement::getAsString).collect(Collectors.toList()));
                }

                return;
            }
            return;
        }

        if (opcode == TRANSACTION_UPDATE) {
            pm(bot, IC_CALL + TRANSACTION + TRANSACTION_GET + "$p");
            pm(bot, IC_CALL + TRANSACTION + TRANSACTION_GET + "$cmds");
            return;
        }

        return;
    }

    if (msg.startsWith(SUPPORTED)) {
        if (Character.toLowerCase(msg.charAt(1)) == 'y') {
            pm(bot, IC_CALL + TRANSACTION + TRANSACTION_CHECK + TRANSACTION_VALUE
                    + get(bot).toString().hashCode());
        }
    }
}

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);/*from  w  ww  . ja  v a2 s  .c  om*/
    }

    return array;
}

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

License:GNU General Public License

private JsonArray toJSON(List<Pair<Integer, String>> list) {
    JsonArray array = new JsonArray();

    for (Pair<Integer, String> rmanError : list) {
        JsonObject element = new JsonObject();

        element.addProperty("id", rmanError.getFirst());
        element.addProperty("message", rmanError.getSecond());

        array.add(element);/*from w ww. j  a  v a2s .co m*/
    }

    return array;
}

From source file:ch.cyberduck.core.openstack.SwiftSegmentService.java

License:Open Source License

/**
 * Create the appropriate manifest structure for a static large object (SLO).
 * The number of object segments is limited to a configurable amount, default 1000. Each segment,
 * except for the final one, must be at least 1 megabyte (configurable).
 *
 * @param objects Ordered list of segments
 * @return ETag returned by the simple upload total size of segment uploaded path of segment
 *//*  w w  w.  j av  a2s  .  c  o  m*/
public String manifest(final String container, final List<StorageObject> objects) {
    JsonArray manifestSLO = new JsonArray();
    for (StorageObject s : objects) {
        JsonObject segmentJSON = new JsonObject();
        // this is the container and object name in the format {container-name}/{object-name}
        segmentJSON.addProperty("path", String.format("/%s/%s", container, s.getName()));
        // MD5 checksum of the content of the segment object
        segmentJSON.addProperty("etag", s.getMd5sum());
        segmentJSON.addProperty("size_bytes", s.getSize());
        manifestSLO.add(segmentJSON);
    }
    return manifestSLO.toString();
}