Example usage for com.google.gson JsonElement isJsonObject

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

Introduction

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

Prototype

public boolean isJsonObject() 

Source Link

Document

provides check for verifying if this element is a Json object or not.

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;/*w w  w .  jav  a2s.  c om*/
}

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 ww  w . j  ava2  s . com
}

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   ww w  .  ja va 2 s. c  om
    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 w  w  w .  ja va  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;// ww w .j  a v  a 2 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);
    }
}

From source file:cf.adriantodt.utils.data.ConfigUtils.java

License:LGPL

public static JsonObject requireObject(JsonElement element) {
    if (element == null || !element.isJsonObject())
        throw new NullPointerException("the provided " + JsonElement.class + " is not an " + JsonObject.class);

    return element.getAsJsonObject();
}

From source file:ch.cyberduck.core.dropbox.DropboxExceptionMappingService.java

License:Open Source License

private void parse(final StringBuilder buffer, final String message) {
    final JsonParser parser = new JsonParser();
    try {/*w  ww .j  av a  2  s .co m*/
        final JsonElement element = parser.parse(new StringReader(message));
        if (element.isJsonObject()) {
            final JsonObject json = element.getAsJsonObject();
            final JsonObject error = json.getAsJsonObject("error");
            if (null == error) {
                this.append(buffer, message);
            } else {
                final JsonPrimitive tag = error.getAsJsonPrimitive(".tag");
                if (null == tag) {
                    this.append(buffer, message);
                } else {
                    this.append(buffer, StringUtils.replace(tag.getAsString(), "_", " "));
                }
            }
        }
        if (element.isJsonPrimitive()) {
            this.append(buffer, element.getAsString());
        }
    } catch (JsonParseException e) {
        // Ignore
    }
}

From source file:ch.ethz.inf.vs.hypermedia.corehal.block.CoREHalResourceFuture.java

License:Open Source License

public static JsonElement serializeToJsonTree(CoREHalBase base) {
    Class<? extends CoREHalBase> type = base.getClass();
    if (ProxyFactory.isProxyClass(type)) {
        type = (Class<? extends CoREHalBase>) type.getSuperclass();
    }/*from w ww  .  j av a  2s .com*/
    JsonElement s = CoREHalResourceFuture.getGson().toJsonTree(base, type);
    if (s.isJsonObject() && base.json() != null) {
        JsonObject json = s.getAsJsonObject();
        base.json().entrySet().forEach((el) -> {
            if (!json.has(el.getKey())) {
                json.add(el.getKey(), el.getValue());
            }
        });
    }
    return s;
}

From source file:ch.ethz.inf.vs.hypermedia.corehal.OptionalListDeserializer.java

License:Open Source License

public static JsonElement cleanup(JsonElement serialize) {
    if (serialize.isJsonObject()) {
        serialize.getAsJsonObject().entrySet().removeIf(x -> x.getValue().isJsonNull());
        serialize.getAsJsonObject().entrySet().forEach(x -> cleanup(x.getValue()));
    }//from  w w  w.  j  a  v a2 s.c  o  m
    if (serialize.isJsonArray()) {
        serialize.getAsJsonArray().forEach(OptionalListDeserializer::cleanup);
    }
    return serialize;
}

From source file:ch.icclab.cyclops.consume.data.BillDeserializer.java

License:Open Source License

@Override
public void preDeserialize(Class<? extends T> clazz, JsonElement jsonElement, Gson gson) {

    // valid JSON object
    if (jsonElement != null && jsonElement.isJsonObject()) {
        JsonObject root = jsonElement.getAsJsonObject();

        // map data to string so it can be persisted as jsonb
        if (root.has(Bill.DATA_FIELD.getName())) {
            root.addProperty(Bill.DATA_FIELD.getName(), new Gson().toJson(root.get(Bill.DATA_FIELD.getName())));
        }//from   w ww. ja  v  a 2s. c o  m
    }
}