Example usage for com.google.gson JsonElement isJsonNull

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

Introduction

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

Prototype

public boolean isJsonNull() 

Source Link

Document

provides check for verifying if this element represents a null value or not.

Usage

From source file:com.heroiclabs.sdk.android.util.json.JsonDeserializerUtils.java

License:Apache License

/**
 * Extract a JsonElement member as a String from a JsonObject, if it exists.
 *
 * @param object The JsonObject to extract from.
 * @param memberName The member name to extract.
 * @return A String representation of the corresponding JsonElement.
 *//* ww  w .ja va  2s  .  c  o  m*/
protected static String extractAsString(final @NonNull JsonObject object, final @NonNull String memberName) {
    if (object.has(memberName)) {
        final JsonElement member = object.get(memberName);
        if (member.isJsonNull()) {
            return null;
        }
        return member.toString();
    }
    return null;
}

From source file:com.heroiclabs.sdk.android.util.json.JsonDeserializerUtils.java

License:Apache License

/**
 * Extract a JsonElement member as a Long from a JsonObject, if it exists.
 *
 * @param object The JsonObject to extract from.
 * @param memberName The member name to extract.
 * @return A Long representation of the corresponding JsonElement.
 *///from  w  w w.  j  ava2 s. c  o  m
protected static Long extractAsLong(final @NonNull JsonObject object, final @NonNull String memberName) {
    if (object.has(memberName)) {
        final JsonElement member = object.get(memberName);
        if (member.isJsonNull()) {
            return null;
        }
        return member.getAsLong();
    }
    return null;
}

From source file:com.hybris.datahub.service.impl.DefaultJsonService.java

License:Open Source License

private String convertJsonValue(final JsonElement jsonElement) {
    if (jsonElement.isJsonPrimitive()) {
        return jsonElement.getAsString();
    } else if (jsonElement.isJsonNull()) {
        return "";
    } else {/*  ww  w. j  ava  2s .com*/
        return jsonElement.toString();
    }
}

From source file:com.ibasco.agql.protocols.valve.steam.webapi.adapters.StoreAppPcRequirementsDeserializer.java

License:Open Source License

@Override
public StoreAppPcRequirements deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    StoreAppPcRequirements requirements = new StoreAppPcRequirements();

    //Fail fast and just return an empty value
    if (json.isJsonNull() || json.isJsonPrimitive()
            || (json.isJsonArray() && json.getAsJsonArray().size() == 0)) {
        return requirements;
    }/*from   w  w w .jav  a2s .co  m*/

    JsonObject object = json.getAsJsonObject();
    if (object.has("minimum")) {
        requirements.setMinimum(object.get("minimum").getAsString());
    }
    if (object.has("recommended")) {
        requirements.setRecommended(object.get("recommended").getAsString());
    }
    return requirements;
}

From source file:com.ibm.streamsx.rest.StreamsRestUtils.java

License:Open Source License

/**
 * Get a member that is expected to exist and be non-null.
 * @param json The JSON object//from  www.ja  v a 2  s.  c  o  m
 * @param member The member name in the object.
 * @return The string value of the member.
 * @throws IllegalStateException if the member does not exist or is null.
 */
static String getRequiredMember(JsonObject json, String member) throws IllegalStateException {
    JsonElement element = json.get(member);
    if (null == element || element.isJsonNull()) {
        throw new IllegalStateException("JSON missing required member " + member);
    }
    return element.getAsString();
}

From source file:com.ibm.streamsx.topology.internal.gson.GsonUtilities.java

License:Open Source License

/**
 * Return a Json array. If the value is not
 * an array then an array containing the single
 * value is returned.//from  ww  w.j av  a2s .c o  m
 * Returns null if the array is not present or present as JSON null.
 */
public static JsonArray array(JsonObject object, String property) {
    if (object.has(property)) {
        JsonElement je = object.get(property);
        if (je.isJsonNull())
            return null;
        if (je.isJsonArray())
            return je.getAsJsonArray();
        JsonArray array = new JsonArray();
        array.add(je);
        return array;
    }
    return null;
}

From source file:com.ibm.streamsx.topology.internal.gson.GsonUtilities.java

License:Open Source License

/**
 * Return a Json object./* w ww .  j  a va 2 s  . c  o m*/
 * Returns null if the object is not present or null.
 */
public static JsonObject jobject(JsonObject object, String property) {
    if (object.has(property)) {
        JsonElement je = object.get(property);
        if (je.isJsonNull())
            return null;
        return je.getAsJsonObject();
    }
    return null;
}

From source file:com.ibm.streamsx.topology.internal.gson.GsonUtilities.java

License:Open Source License

/**
 * Returns a property as a String.//from   w  w  w  .  j a  va  2  s.  com
 * @param object
 * @param property
 * @return Value or null if it is not set.
 */
public static String jstring(JsonObject object, String property) {
    if (object.has(property)) {
        JsonElement je = object.get(property);
        if (je.isJsonNull())
            return null;
        return je.getAsString();
    }
    return null;
}

From source file:com.ibm.streamsx.topology.internal.gson.GsonUtilities.java

License:Open Source License

public static boolean jboolean(JsonObject object, String property) {
    if (object.has(property)) {
        JsonElement je = object.get(property);
        if (je.isJsonNull())
            return false;
        return je.getAsBoolean();
    }/*from  w w w.j a v  a 2s  .  c  o m*/
    return false;
}

From source file:com.ibm.streamsx.topology.internal.streaminganalytics.VcapServices.java

License:Open Source License

/**
 * Get the top-level VCAP services object.
 * //from w  ww. j  a v a 2 s  .c om
 * Object can be one of the following:
 * <ul>
 * <li>JsonObject - assumed to contain VCAP_SERVICES </li>
 * <li>String - assumed to contain serialized VCAP_SERVICES JSON, or the
 * location of a file containing the serialized VCAP_SERVICES JSON</li>
 * <li>null - assumed to be in the environment variable VCAP_SERVICES</li>
 * </ul>
 */
public static JsonObject getVCAPServices(JsonElement rawServices) throws IOException {

    JsonParser parser = new JsonParser();
    String vcapString;
    String vcapContents = null;

    if (rawServices == null || rawServices.isJsonNull()) {
        // if rawServices is null, then pull from the environment
        vcapString = System.getenv("VCAP_SERVICES");
        if (vcapString == null) {
            throw new IllegalStateException(
                    "VCAP_SERVICES are not defined, please set environment variable VCAP_SERVICES or configuration property: "
                            + VCAP_SERVICES);
        }
        // resulting string can be either the serialized JSON or filename
        if (Files.isRegularFile(Paths.get(vcapString))) {
            Path vcapFile = Paths.get(vcapString);
            vcapContents = new String(Files.readAllBytes(vcapFile), StandardCharsets.UTF_8);
        } else {
            vcapContents = vcapString;
        }
    } else if (rawServices.isJsonObject()) {
        return rawServices.getAsJsonObject();
    } else if (rawServices.isJsonPrimitive()) {
        // String can be either the serialized JSON or filename
        String rawString = rawServices.getAsString();
        if (Files.isRegularFile(Paths.get(rawString))) {
            Path vcapFile = Paths.get(rawString);
            vcapContents = new String(Files.readAllBytes(vcapFile), StandardCharsets.UTF_8);
        } else
            vcapContents = rawString;
    } else {
        throw new IllegalArgumentException("Unknown VCAP_SERVICES object class: " + rawServices.getClass());
    }
    return parser.parse(vcapContents).getAsJsonObject();
}