Example usage for com.google.gson JsonElement isJsonArray

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

Introduction

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

Prototype

public boolean isJsonArray() 

Source Link

Document

provides check for verifying if this element is an array or not.

Usage

From source file:br.com.thiaguten.contrib.JsonContrib.java

License:Apache License

private static void deepJsonSearchByKeyToList(String desiredKey, JsonElement jsonElement,
        List<Object> jsonElementList) {
    if (desiredKey != null && jsonElement != null && jsonElementList != null) {
        if (jsonElement.isJsonObject()) {
            for (Map.Entry<String, JsonElement> entry : ((JsonObject) jsonElement).entrySet()) {
                String key = entry.getKey();
                JsonElement value = entry.getValue();

                if (key.equalsIgnoreCase(desiredKey)) {
                    jsonElementList.add(getJsonElementAsString(value));
                }// www . java  2  s  .c o  m

                deepJsonSearchByKeyToList(desiredKey, entry.getValue(), jsonElementList);
            }
        } else if (jsonElement.isJsonArray()) {
            Iterator<JsonElement> iterator = ((JsonArray) jsonElement).iterator();
            while (iterator.hasNext()) {
                deepJsonSearchByKeyToList(desiredKey, iterator.next(), jsonElementList);
            }
        }
    }
}

From source file:br.com.thiaguten.contrib.JsonContrib.java

License:Apache License

private static List<JsonElement> jsonToJsonElementList(String json, boolean flatten) {
    JsonElement jsonElement = parseObject(json);

    List<JsonElement> elements = null;
    if (jsonElement != null) {
        boolean isJsonObject = jsonElement.isJsonObject();
        boolean isJsonArray = jsonElement.isJsonArray();

        elements = new ArrayList<JsonElement>();

        // Only instances of 'JsonArray' or 'JsonObject' can be flattened
        if (isJsonArray || isJsonObject) {
            if (flatten) {
                deepJsonSearchToList(jsonElement, elements);
            } else {
                if (isJsonObject) {
                    for (Map.Entry<String, JsonElement> entry : ((JsonObject) jsonElement).entrySet()) {
                        elements.add(entry.getValue());
                    }/*ww  w.  j a  v  a2 s. com*/
                } else {
                    Iterator<JsonElement> iterator = ((JsonArray) jsonElement).iterator();
                    while (iterator.hasNext()) {
                        elements.add(iterator.next());
                    }
                }
            }
        } else {
            elements.add(jsonElement);
        }
    }
    return elements;
}

From source file:ca.uhn.fhir.jpa.util.jsonpatch.CopyOperation.java

License:Apache License

@Override
public JsonElement apply(JsonElement original) {
    JsonElement result = duplicate(original);

    JsonElement item = path.head().navigate(result);
    JsonElement data = mySourcePath.head().navigate(original);

    if (item.isJsonObject()) {
        item.getAsJsonObject().add(path.tail(), data);
    } else if (item.isJsonArray()) {

        JsonArray array = item.getAsJsonArray();

        int index = (path.tail().equals("-")) ? array.size() : Integer.valueOf(path.tail());

        List<JsonElement> temp = new ArrayList<JsonElement>();

        Iterator<JsonElement> iter = array.iterator();
        while (iter.hasNext()) {
            JsonElement stuff = iter.next();
            iter.remove();/*w ww  . j  a va  2s . c om*/
            temp.add(stuff);
        }

        temp.add(index, data);

        for (JsonElement stuff : temp) {
            array.add(stuff);
        }

    }

    return result;
}

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

License:Open Source License

/**
 * Fill in variables from a donation/*  www .jav  a2s  .c  om*/
 *
 * @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 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 www .  j  a v a2 s.c o  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;/*  w w w . j  a v  a  2  s. c  om*/
}

From source file:cf.adriantodt.David.Loader.java

License:LGPL

public static void main(String[] args) throws Exception {
    Loader.args = args;//from  w  w w  .ja  v a  2 s.c  o m
    JsonElement src = new JsonParser().parse(resource("/assets/loader/main.json"));

    if (!src.isJsonArray()) {
        LOGGER.error("\"/assets/loader/main.json\" is in a incorrect form. Expected \"" + JsonArray.class
                + "\", got \"" + src.getClass() + "\"");
        return;
    }

    src.getAsJsonArray().forEach(element -> {
        try {
            ModuleManager.add(Class.forName(element.getAsString()));
        } catch (Exception e) {
            LOGGER.error("Failed to load Module " + element, e);
        }
    });

    ModuleManager.firePreReadyEvents();

    new JDABuilder(AccountType.BOT).setToken(DBModule.getConfig().get("token").getAsString())
            .setEventManager(new AnnotatedEventManager()).addListener(ModuleManager.jdaListeners())
            .buildBlocking();

    ModuleManager.firePostReadyEvents();
}

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

License:Open Source License

@Override
public FormList deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    FormList list = new FormList();
    if (json.isJsonArray()) {
        for (JsonElement el : json.getAsJsonArray()) {
            list.add(context.deserialize(el, Form.class));
        }/*from   w  w w.  java 2 s.  c  om*/
    } else {
        list.add(context.deserialize(json, Form.class));
    }
    return list;
}

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

License:Open Source License

@Override
public LinkList deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    LinkList list = new LinkList();
    if (json.isJsonArray()) {
        for (JsonElement el : json.getAsJsonArray()) {
            list.add(context.deserialize(el, Link.class));
        }//from   w w w . j  av  a 2 s . c  o m
    } else {
        list.add(context.deserialize(json, Link.class));
    }
    return list;
}

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()));
    }/* w ww .j av  a2s  .c  o m*/
    if (serialize.isJsonArray()) {
        serialize.getAsJsonArray().forEach(OptionalListDeserializer::cleanup);
    }
    return serialize;
}