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:org.rapla.rest.server.jsonpatch.JsonMergePatch.java

License:LGPL

public static JsonMergePatch fromJson(final JsonElement input) throws JsonPatchException {
    if (input.isJsonPrimitive()) {
        throw new JsonPatchException("Only json containers are supported");
    }/* w  w  w .j av a  2  s  .co  m*/
    if (input.isJsonArray()) {
        final JsonElement content = clearNulls(input);
        return new JsonMergePatch(input) {
            @Override
            public JsonElement apply(final JsonElement input) throws JsonPatchException {
                return content;
            }
        };
    } else {
        return new ObjectMergePatch(input);
    }
}

From source file:org.rapla.rest.server.jsonpatch.JsonMergePatch.java

License:LGPL

/**
 * Clear "null values" from a JSON value
 *
 * <p>Non container values are unchanged. For arrays, null elements are
 * removed. From objects, members whose values are null are removed.</p>
 *
 * <p>This method is recursive, therefore arrays within objects, or objects
 * within arrays, or arrays within arrays etc are also affected.</p>
 *
 * @param node the original JSON value// w  w  w  .j a  v a 2 s.  c om
 * @return a JSON value without null values (see description)
 */
protected static JsonElement clearNulls(final JsonElement node) {
    if (node.isJsonPrimitive())
        return node;

    return node.isJsonArray() ? clearNullsFromArray((JsonArray) node) : clearNullsFromObject((JsonObject) node);
}

From source file:org.runbuddy.libtomahawk.infosystem.hatchet.Store.java

License:Open Source License

public int getAsInt(JsonObject object, String memberName) throws IOException {
    JsonElement element = get(object, memberName);
    if (element != null && element.isJsonPrimitive()) {
        return element.getAsInt();
    }/*from w  w w .  ja  v  a2  s. c  om*/
    return -1;
}

From source file:org.runbuddy.libtomahawk.infosystem.hatchet.Store.java

License:Open Source License

public float getAsFloat(JsonObject object, String memberName) throws IOException {
    JsonElement element = get(object, memberName);
    if (element != null && element.isJsonPrimitive()) {
        return element.getAsFloat();
    }//from w  w  w  . java2s . c o m
    return -1;
}

From source file:org.runbuddy.libtomahawk.infosystem.hatchet.Store.java

License:Open Source License

public String getAsString(JsonObject object, String memberName) throws IOException {
    JsonElement element = get(object, memberName);
    if (element != null && element.isJsonPrimitive()) {
        return element.getAsString();
    }/* ww w .j  a  v  a2s. co  m*/
    return null;
}

From source file:org.runbuddy.libtomahawk.infosystem.InfoSystem.java

License:Open Source License

private String getAsString(JsonObject object, String memberName) {
    JsonElement element = object.get(memberName);
    if (element != null && element.isJsonPrimitive()) {
        return element.getAsString();
    }//w  w w . j  a v a2s.c  o m
    return null;
}

From source file:org.runbuddy.libtomahawk.resolver.ScriptAccount.java

License:Open Source License

public void reportScriptJobResult(JsonObject result) {
    JsonElement requestIdNode = result.get("requestId");
    String requestId = null;/*  w  w  w  .ja  v  a  2  s . c o  m*/
    if (requestIdNode != null && requestIdNode.isJsonPrimitive()) {
        requestId = result.get("requestId").getAsString();
    }
    if (requestId != null && !requestId.isEmpty()) {
        ScriptJob job = mJobs.get(requestId);
        if (job != null) {
            JsonElement errorNode = result.get("error");
            if (errorNode == null) {
                job.reportResults(result.get("data"));
            } else if (errorNode.isJsonPrimitive()) {
                job.reportFailure(result.get("error").getAsString());
            } else {
                job.reportFailure("no error message provided");
            }
        } else {
            Log.e(TAG, "reportScriptJobResult - ScriptAccount:" + mName
                    + ", couldn't find ScriptJob with given requestId");
        }
    } else {
        Log.e(TAG, "reportScriptJobResult - ScriptAccount:" + mName + ", requestId is null or empty");
    }
}

From source file:org.runbuddy.libtomahawk.resolver.ScriptUtils.java

License:Open Source License

public static String getNodeChildAsText(JsonElement node, String fieldName) {
    if (node instanceof JsonObject) {
        JsonElement n = ((JsonObject) node).get(fieldName);
        if (n != null && n.isJsonPrimitive()) {
            return n.getAsString();
        }//  ww  w.  j  av a 2  s  . c  om
    }
    return null;
}

From source file:org.runbuddy.libtomahawk.resolver.ScriptUtils.java

License:Open Source License

public static int getNodeChildAsInt(JsonElement node, String fieldName) {
    if (node instanceof JsonObject) {
        JsonElement n = ((JsonObject) node).get(fieldName);
        if (n != null && n.isJsonPrimitive()) {
            return n.getAsInt();
        }/*w w w.j a  va 2s .  c  om*/
    }
    return 0;
}

From source file:org.slc.sli.util.CheckListHelper.java

License:Apache License

/**
 * To find users have been added or not// w ww .  ja  v  a2 s  .c  om
 * API Call http(s)://xxx.xxx.xxx.xxx/api/rest/users
 * get external_id from session.
 * then, find himself/herself from rest users call
 * @param mySession
 * @param token
 * @return
 */
private boolean hasAddedUsers(JsonObject mySession, String token) {
    boolean result = false;
    String path = Constants.API_PREFIX + "/" + Constants.USERS;
    JsonArray jsonArray = getJsonArray(path, token);
    // check if there is an entry not equal to himself in uid field
    if (jsonArray.size() > 1) {
        // get own uid (external_id)
        JsonElement myTenantId = mySession.get(EXTERNAL_ID);
        if (myTenantId != null && myTenantId.isJsonPrimitive()) {
            for (JsonElement jsonElement : jsonArray) {
                JsonObject jsonObject = jsonElement.getAsJsonObject();
                if (jsonObject != null && !jsonObject.isJsonNull()) {
                    JsonElement uid = jsonObject.get(UID);
                    if (uid != null && uid.isJsonPrimitive()) {
                        if (uid.getAsString().equals(myTenantId.getAsString())) {
                            result = true;
                            break;
                        }
                    }
                }
            }
        }
    }

    return result;
}