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.cdancy.bitbucket.rest.fallbacks.BitbucketFallbacks.java

License:Apache License

/**
 * Parse list of Error's from output.// w w  w.j a  v a  2s . c  o  m
 *
 * @param output json containing errors hash
 * @return List of Error's or empty list if none could be found
 */
public static List<Error> getErrors(String output) {
    JsonElement element = parser.parse(output);
    JsonObject object = element.getAsJsonObject();
    JsonArray errorsArray = object.get("errors").getAsJsonArray();

    List<Error> errors = Lists.newArrayList();
    Iterator<JsonElement> it = errorsArray.iterator();
    while (it.hasNext()) {
        JsonObject obj = it.next().getAsJsonObject();
        JsonElement context = obj.get("context");
        JsonElement message = obj.get("message");
        JsonElement exceptionName = obj.get("exceptionName");
        Error error = Error.create(!context.isJsonNull() ? context.getAsString() : null,
                !message.isJsonNull() ? message.getAsString() : null,
                !exceptionName.isJsonNull() ? exceptionName.getAsString() : null);
        errors.add(error);
    }

    return errors;
}

From source file:com.claresco.tinman.json.JsonUtility.java

License:Open Source License

protected static boolean isJsonElementNull(JsonElement theElement) {
    return theElement.isJsonNull();
}

From source file:com.claresco.tinman.json.XapiPersonJson.java

License:Open Source License

@Override
public XapiPerson deserialize(JsonElement arg0, Type arg1, JsonDeserializationContext arg2)
        throws JsonParseException {

    if (arg0.isJsonObject()) {
        JsonObject theJsonObject = JsonUtility.convertJsonElementToJsonObject(arg0);
        ArrayList<String> theName = fillTheList("name", theJsonObject);
        ArrayList<String> theMboxSha1sum = fillTheList("mbox_sha1sum", theJsonObject);
        ArrayList<String> theOpenid = fillTheList("openid", theJsonObject);
        ArrayList<XapiIRI> theMbox = new ArrayList<XapiIRI>();
        ArrayList<XapiAccount> theAccounts = new ArrayList<XapiAccount>();

        JsonArray theArray;//from   ww  w.java 2s. c om

        if (JsonUtility.hasElement(theJsonObject, "mbox")) {

            theArray = JsonUtility.getAnArray(theJsonObject, "mbox");
            for (JsonElement e : theArray) {
                if (!e.isJsonNull()) {
                    String theEmailAddress = e.getAsString();
                    if (!theEmailAddress.startsWith("mailto:")) {
                        throw new XapiBadIdentifierException("Mbox has to start with \'mailto:\'");
                    }
                    theMbox.add(new XapiIRI(e.getAsString()));
                }
            }
        }

        if (JsonUtility.hasElement(theJsonObject, "account")) {
            theArray = JsonUtility.getAnArray(theJsonObject, "account");
            for (JsonElement e : theArray) {
                theAccounts.add((XapiAccount) JsonUtility.delegateDeserialization(arg2, e, XapiAccount.class));
            }
        }

        return new XapiPerson(theName, theMbox, theMboxSha1sum, theOpenid, theAccounts);

    } else {
        throw new XapiBadPersonException("XapiPerson should be a JSON object");
    }
}

From source file:com.cloud.bridge.util.JsonAccessor.java

License:Apache License

public int getMatchCount(String propPath) {
    JsonElement jsonElement = tryEval(propPath);
    if (jsonElement == null)
        return -1;

    if (jsonElement.isJsonNull())
        return 0;

    if (jsonElement.isJsonArray())
        return ((JsonArray) jsonElement).size();

    return 1;//from www  . j  a va  2s  .  c om
}

From source file:com.cloudant.client.org.lightcouch.internal.CouchDbUtil.java

License:Open Source License

public static <T> T JsonToObject(Gson gson, JsonElement elem, String key, Class<T> classType) {
    if (elem != null && !elem.isJsonNull()) {
        JsonElement keyElem = elem.getAsJsonObject().get(key);
        if (keyElem != null && !keyElem.isJsonNull()) {
            return gson.fromJson(elem.getAsJsonObject().get(key), classType);
        } else {//from w w w .  j a v a2s  . co m
            return null;
        }
    } else {
        return null;
    }
}

From source file:com.cloudbase.CBNaturalDeserializer.java

License:Open Source License

public Object deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) {
    if (json.isJsonNull())
        return null;
    else if (json.isJsonPrimitive())
        return handlePrimitive(json.getAsJsonPrimitive());
    else if (json.isJsonArray())
        return handleArray(json.getAsJsonArray(), context);
    else/*  w  w w  . j av a2  s.  c om*/
        return handleObject(json.getAsJsonObject(), context);
}

From source file:com.collective.celos.ci.testing.fixtures.compare.json.JsonElementConstructor.java

License:Apache License

private JsonElement deepCopy(String path, JsonElement el, Set<String> ignorePaths) {
    if (el.isJsonArray()) {
        return deepCopyJsonArray(path, el.getAsJsonArray(), ignorePaths);
    }/*  w  w w  . ja v  a2  s . com*/
    if (el.isJsonObject()) {
        return deepCopyJsonObject(path, el.getAsJsonObject(), ignorePaths);
    }
    if (el.isJsonPrimitive() || el.isJsonNull()) {
        return el;
    }
    throw new IllegalStateException(
            "JSONElement should be either Array, Object, Primitive or Null. So you cannot get here");
}

From source file:com.continusec.client.LogTreeHead.java

License:Apache License

/**
 * Create object from a Gson JsonObject.
 * @param o the Gson object.//from   w ww. j a v a2  s .  c  o  m
 * @return the log tree hash.
 */
protected static LogTreeHead fromJsonObject(JsonObject o) {
    JsonElement e2 = o.get("tree_hash");
    if (e2.isJsonNull()) {
        return new LogTreeHead(o.get("tree_size").getAsInt(), null);
    } else {
        return new LogTreeHead(o.get("tree_size").getAsInt(), Base64.decodeBase64(e2.getAsString()));
    }
}

From source file:com.continusec.client.ObjectHash.java

License:Apache License

/**
 * Calculate the objecthash for a Gson JsonElement object, with a custom redaction prefix string.
 * @param o the Gson JsonElement to calculated the objecthash for.
 * @param r the string to use as a prefix to indicate that a string should be treated as a redacted subobject.
 * @return the objecthash for this object
 * @throws ContinusecException upon error
 *//*from  w w  w .  j ava  2  s.co  m*/
public static final byte[] objectHashWithRedaction(JsonElement o, String r) throws ContinusecException {
    if (o == null || o.isJsonNull()) {
        return hashNull();
    } else if (o.isJsonArray()) {
        return hashArray(o.getAsJsonArray(), r);
    } else if (o.isJsonObject()) {
        return hashObject(o.getAsJsonObject(), r);
    } else if (o.isJsonPrimitive()) {
        JsonPrimitive p = o.getAsJsonPrimitive();
        if (p.isBoolean()) {
            return hashBoolean(p.getAsBoolean());
        } else if (p.isNumber()) {
            return hashDouble(p.getAsDouble());
        } else if (p.isString()) {
            return hashString(p.getAsString(), r);
        } else {
            throw new InvalidObjectException();
        }
    } else {
        throw new InvalidObjectException();
    }
}

From source file:com.continuuity.weave.internal.json.JsonUtils.java

License:Apache License

/**
 * Returns a String representation of the given property.
 *//*from www  .  ja v a 2  s  .c  o m*/
public static String getAsString(JsonObject json, String property) {
    JsonElement jsonElement = json.get(property);
    if (jsonElement.isJsonNull()) {
        return null;
    }
    if (jsonElement.isJsonPrimitive()) {
        return jsonElement.getAsString();
    }
    return jsonElement.toString();
}