Example usage for com.google.gson JsonElement isJsonPrimitive

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

Introduction

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

Prototype

public boolean isJsonPrimitive() 

Source Link

Document

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

Usage

From source file:leola.web.WebLeolaLibrary.java

License:Open Source License

/**
 * Converts the {@link JsonElement} into the equivalent {@link LeoObject}
 * /*  ww w . ja  v  a 2 s . com*/
 * @param element
 * @return the {@link LeoObject}
 */
private static LeoObject toLeoObject(JsonElement element) {
    if (element == null || element.isJsonNull()) {
        return LeoObject.NULL;
    }

    if (element.isJsonArray()) {
        JsonArray array = element.getAsJsonArray();
        LeoArray leoArray = new LeoArray(array.size());
        array.forEach(e -> leoArray.add(toLeoObject(e)));
        return leoArray;
    }

    if (element.isJsonObject()) {
        JsonObject object = element.getAsJsonObject();
        LeoMap leoMap = new LeoMap();
        object.entrySet().forEach(entry -> {
            leoMap.putByString(entry.getKey(), toLeoObject(entry.getValue()));
        });

        return leoMap;
    }

    if (element.isJsonPrimitive()) {
        JsonPrimitive primitive = element.getAsJsonPrimitive();
        if (primitive.isBoolean()) {
            return LeoObject.valueOf(primitive.getAsBoolean());
        }

        if (primitive.isNumber()) {
            return LeoObject.valueOf(primitive.getAsDouble());
        }

        if (primitive.isString()) {
            return LeoString.valueOf(primitive.getAsString());
        }
    }

    return LeoObject.NULL;
}

From source file:melnorme.lang.utils.gson.GsonHelper.java

License:Open Source License

public String getString(JsonObject jsonObject, String key) throws CommonException {
    JsonElement element = jsonObject.get(key);
    if (element != null && element.isJsonPrimitive() && element.getAsJsonPrimitive().isString()) {
        return element.getAsString();
    } else {/*from   ww  w . jav  a  2s.c o m*/
        throw wrongTypeException(key, "String");
    }
}

From source file:melnorme.lang.utils.gson.GsonHelper.java

License:Open Source License

public boolean getBoolean(JsonObject jsonObject, String key) throws CommonException {
    JsonElement element = jsonObject.get(key);
    if (element != null && element.isJsonPrimitive() && element.getAsJsonPrimitive().isBoolean()) {
        return element.getAsBoolean();
    } else {/*  ww  w . j  a  va 2 s  .  com*/
        throw wrongTypeException(key, "boolean");
    }
}

From source file:melnorme.lang.utils.gson.GsonHelper.java

License:Open Source License

public Number getNumber(JsonObject jsonObject, String key) throws CommonException {
    JsonElement element = jsonObject.get(key);
    if (element != null && element.isJsonPrimitive() && element.getAsJsonPrimitive().isNumber()) {
        return element.getAsNumber();
    } else {/*from  www . j  av  a2s. c  o  m*/
        throw wrongTypeException(key, "Number");
    }
}

From source file:net.daporkchop.toobeetooteebot.text.JsonUtils.java

License:Open Source License

/**
 * Is the given JsonElement a string?/*from   ww w  .j a  v a 2 s.c o  m*/
 */
public static boolean isString(JsonElement json) {
    return json.isJsonPrimitive() && json.getAsJsonPrimitive().isString();
}

From source file:net.daporkchop.toobeetooteebot.text.JsonUtils.java

License:Open Source License

public static boolean isNumber(JsonElement json) {
    return json.isJsonPrimitive() && json.getAsJsonPrimitive().isNumber();
}

From source file:net.daporkchop.toobeetooteebot.text.JsonUtils.java

License:Open Source License

/**
 * Gets the string value of the given JsonElement.  Expects the second parameter to be the name of the element's
 * field if an error message needs to be thrown.
 *///from w  ww  .  j a  v  a 2 s.co m
public static String getString(JsonElement json, String memberName) {
    if (json.isJsonPrimitive()) {
        return json.getAsString();
    } else {
        throw new JsonSyntaxException("Expected " + memberName + " to be a string, was " + toString(json));
    }
}

From source file:net.daporkchop.toobeetooteebot.text.JsonUtils.java

License:Open Source License

/**
 * Gets the boolean value of the given JsonElement.  Expects the second parameter to be the name of the element's
 * field if an error message needs to be thrown.
 *//*from w w w  . ja  v a2  s . c o m*/
public static boolean getBoolean(JsonElement json, String memberName) {
    if (json.isJsonPrimitive()) {
        return json.getAsBoolean();
    } else {
        throw new JsonSyntaxException("Expected " + memberName + " to be a Boolean, was " + toString(json));
    }
}

From source file:net.daporkchop.toobeetooteebot.text.JsonUtils.java

License:Open Source License

/**
 * Gets the float value of the given JsonElement.  Expects the second parameter to be the name of the element's
 * field if an error message needs to be thrown.
 *//*  ww  w  . j av  a 2 s  .  c  o m*/
public static float getFloat(JsonElement json, String memberName) {
    if (json.isJsonPrimitive() && json.getAsJsonPrimitive().isNumber()) {
        return json.getAsFloat();
    } else {
        throw new JsonSyntaxException("Expected " + memberName + " to be a Float, was " + toString(json));
    }
}

From source file:net.daporkchop.toobeetooteebot.text.JsonUtils.java

License:Open Source License

/**
 * Gets the integer value of the given JsonElement.  Expects the second parameter to be the name of the element's
 * field if an error message needs to be thrown.
 *//*from w w  w .  j a  v  a 2 s.  co  m*/
public static int getInt(JsonElement json, String memberName) {
    if (json.isJsonPrimitive() && json.getAsJsonPrimitive().isNumber()) {
        return json.getAsInt();
    } else {
        throw new JsonSyntaxException("Expected " + memberName + " to be a Int, was " + toString(json));
    }
}