Example usage for com.google.gson JsonElement getAsJsonObject

List of usage examples for com.google.gson JsonElement getAsJsonObject

Introduction

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

Prototype

public JsonObject getAsJsonObject() 

Source Link

Document

convenience method to get this element as a JsonObject .

Usage

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

License:Open Source License

public static NBTBase parseJSON(JsonElement element) {
    if (element.isJsonObject())
        return parseJSON(element.getAsJsonObject());
    else if (element.isJsonArray())
        return parseJSON(element.getAsJsonArray());
    else if (element.isJsonPrimitive())
        return parseJSON(element.getAsJsonPrimitive());

    return null;/*from  w w w .  j ava2 s. co m*/
}

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

License:Open Source License

public static JsonElement fixNulls(JsonElement element) {
    if (element.isJsonNull())
        return new JsonPrimitive("");
    if (element.isJsonObject())
        return fixNulls(element.getAsJsonObject());
    if (element.isJsonArray())
        return fixNulls(element.getAsJsonArray());
    if (element.isJsonPrimitive())
        return fixNulls(element.getAsJsonPrimitive());
    return null;/*from   w  w  w .j  a va2  s .  co m*/
}

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

License:Open Source License

public String getTypes() {
    HashSet<String> types = new HashSet<>();
    for (JsonElement element : rewards)
        types.add(element.getAsJsonObject().get("type").getAsString());
    return JOINER_COMMA_SPACE.join(types);
}

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

License:Open Source License

public String getHTML() throws IOException {
    StringBuilder sb = new StringBuilder();
    for (JsonElement element : rewards) {
        JsonObject object = element.getAsJsonObject();
        if (object.has(CUSTOMHTML) && !Strings.isNullOrEmpty(object.get(CUSTOMHTML).getAsString()))
            sb.append(object.get(CUSTOMHTML).getAsString());
        else//from  ww w. j a va 2s  .c o m
            sb.append(TypeRegistry.getByName(object.get("type").getAsString())
                    .getHTML(object.getAsJsonObject("data")));
    }
    return sb.toString();
}

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

License:Open Source License

public RewardsDB(String input) {
    editable = false;//from w ww . j av  a  2 s  . c  om
    JsonArray rootArray = JSON_PARSER.parse(input).getAsJsonArray();

    for (JsonElement element : rootArray) {
        Reward reward = new Reward(element.getAsJsonObject());
        map.put(reward.getAmount(), reward);
    }
}

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  2 s .  com
    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.GuildModule.java

License:LGPL

private static Data unpack(JsonElement element) {
    JsonObject object = element.getAsJsonObject();
    Data data = all.stream().filter(dataPredicate -> object.get("id").getAsString().equals(dataPredicate.id))
            .findFirst().orElseGet(Data::new);
    data.id = object.get("id").getAsString();
    data.name = object.get("name").getAsString();
    data.cmdPrefixes.clear();/*from  w  w  w  .j a  va  2s .  c  o  m*/
    object.get("cmdPrefixes").getAsJsonArray()
            .forEach(jsonElement -> data.cmdPrefixes.add(jsonElement.getAsString()));
    data.lang = object.get("lang").getAsString();
    data.flags.clear();
    object.get("flags").getAsJsonObject().entrySet()
            .forEach(entry -> data.flags.put(entry.getKey(), entry.getValue().getAsBoolean()));
    object.get("userPerms").getAsJsonObject().entrySet()
            .forEach(entry -> data.userPerms.put(entry.getKey(), entry.getValue().getAsLong()));
    return data;
}

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

License:LGPL

private static void loadFile(String lang, JsonElement src) {
    if (!src.isJsonObject())
        return;//from w ww. j av a2  s.com
    JsonObject file = src.getAsJsonObject();

    List<Exception> post = new ArrayList<>();

    try {
        if (file.has("translations")) {
            loadTranslation(lang, "", file.get("translations"), post);
        }
    } catch (Exception e) {
        post.add(e);
    }

    try {
        if (file.has("commands")) {
            JsonElement metaSrc = file.get("meta");

            JsonObject meta = metaSrc.isJsonObject() ? metaSrc.getAsJsonObject() : null;

            file.get("commands").getAsJsonObject().entrySet()
                    .forEach(entry -> loadCommand(lang, entry.getKey(), entry.getValue(), meta, post));
        }
    } catch (Exception e) {
        post.add(e);
    }

    if (post.size() > 0) {
        logger().info("Errors occurred while loading I18nModule:");
        post.forEach(e -> logger().error(e));
    }
}

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

License:LGPL

private static void loadTranslation(String lang, String base, JsonElement src, List<Exception> post) {
    if (!src.isJsonObject())
        return;/*from  www .j ava  2 s  .  c o  m*/
    JsonObject t = src.getAsJsonObject();

    t.entrySet().forEach(entry -> {
        if (ConfigUtils.isJsonString(entry.getValue())) {
            localize(lang, base + entry.getKey(), entry.getValue().getAsString());
        }
        if (entry.getValue().isJsonObject()) {
            loadTranslation(lang, base + entry.getKey() + ".", entry.getValue(), post);
        }
    });
}

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

License:LGPL

private static void loadCommand(String lang, String name, JsonElement src, JsonObject metadata,
        List<Exception> post) {
    logger().trace(lang + " - " + name + " - " + src.toString());
    if (!src.isJsonObject())
        return;// w ww. j  av  a2  s  . c  o  m
    JsonObject cmd = src.getAsJsonObject();
    try {
        if (cmd.has("desc") || cmd.has("params") || cmd.has("info")) {
            String desc = cmd.has("desc") ? cmd.get("desc").getAsString()
                    : metadata.get("noDesc").getAsString();
            String params = cmd.has("params") ? cmd.get("params").getAsString()
                    : metadata.get("noParams").getAsString();
            String info = cmd.has("info") ? "\n  " + cmd.get("info").getAsString().replace("\n", "\n  ") : "";
            localize(lang, name + ".usage",
                    desc + "\n" + metadata.get("params").getAsString() + ": " + params + info);
        }
    } catch (Exception e) {
        post.add(e);
    }

    try {
        if (cmd.has("translations")) {
            loadTranslation(lang, name + ".", cmd.get("translations"), post);
        }
    } catch (Exception e) {
        post.add(e);
    }

    try {
        if (cmd.has("subs") && cmd.get("subs").isJsonObject()) {
            cmd.get("subs").getAsJsonObject().entrySet().forEach(
                    entry -> loadCommand(lang, name + "." + entry.getKey(), entry.getValue(), metadata, post));
        }
    } catch (Exception e) {
        post.add(e);
    }
}