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:org.ppojo.data.JsonElementTypes.java

License:Apache License

public static JsonElementTypes getType(JsonElement json) {
    if (json.isJsonObject())
        return JsonElementTypes.JsonObject;
    if (json.isJsonArray())
        return JsonElementTypes.JsonArray;
    if (json.isJsonNull())
        return JsonElementTypes.JsonNull;
    if (json.isJsonPrimitive()) {
        JsonPrimitive jsonPrimitive = json.getAsJsonPrimitive();
        if (jsonPrimitive.isBoolean())
            return JsonElementTypes.Boolean;
        if (jsonPrimitive.isNumber())
            return JsonElementTypes.Number;
        if (jsonPrimitive.isString())
            return JsonElementTypes.String;
    }// w  w  w  .  java 2 s  . com
    return JsonElementTypes.Unknown;
}

From source file:org.projectbuendia.client.net.OpenMrsErrorListener.java

License:Apache License

/** Parsing the json formatted error response received from the OpenMRS server **/
public String extractMessageFromJson(String json) {
    String message = null;/*from  w w w  .  j a  v  a  2 s. c o m*/
    try {
        JsonObject result = new JsonParser().parse(json).getAsJsonObject();
        if (result.has("error")) {
            JsonObject errorObject = result.getAsJsonObject("error");
            JsonElement element = errorObject.get("message");
            if (element == null || element.isJsonNull()) {
                element = errorObject.get("code");
            }
            if (element != null && element.isJsonPrimitive()) {
                message = element.getAsString();
            }
        }
    } catch (JsonParseException | IllegalStateException | UnsupportedOperationException e) {
        LOG.w("Problem parsing error message: " + e.getMessage());
    }
    return message;
}

From source file:org.projectbuendia.client.net.OpenMrsServer.java

License:Apache License

/**
 * Wraps an ErrorListener so as to extract an error message from the JSON
 * content of a response, if possible./* ww  w  .  j a  va 2  s.  co m*/
 * @param errorListener An error listener.
 * @return A new error listener that tries to pass a more meaningful message
 *     to the original errorListener.
 */
private Response.ErrorListener wrapErrorListener(final Response.ErrorListener errorListener) {
    return new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            String message = error.getMessage();
            try {
                if (error.networkResponse != null && error.networkResponse.data != null) {
                    String text = new String(error.networkResponse.data);
                    JsonObject result = new JsonParser().parse(text).getAsJsonObject();
                    if (result.has("error")) {
                        JsonObject errorObject = result.getAsJsonObject("error");
                        JsonElement element = errorObject.get("message");
                        if (element == null || element.isJsonNull()) {
                            element = errorObject.get("code");
                        }
                        if (element != null && element.isJsonPrimitive()) {
                            message = element.getAsString();
                        }
                    }
                }
            } catch (JsonParseException | IllegalStateException | UnsupportedOperationException e) {
                e.printStackTrace();
            }
            errorListener.onErrorResponse(new VolleyError(message, error));
        }
    };
}

From source file:org.qcert.runtime.DataComparator.java

License:Apache License

private static DType getType(JsonElement obj) {
    if (obj == null) {
        return DType.DT_JNULL;
    } else if (obj.isJsonNull()) {
        return DType.DT_NULL;
    } else if (obj.isJsonPrimitive()) {
        final JsonPrimitive prim = obj.getAsJsonPrimitive();
        if (prim.isBoolean()) {
            return DType.DT_BOOL;
        } else if (prim.isString()) {
            return DType.DT_STRING;
        } else if (prim.isNumber()) {
            final Number num = prim.getAsNumber();
            if (num instanceof LazilyParsedNumber) {
                return DType.DT_LAZYNUM;
            } else if (num instanceof Long || num instanceof Short || num instanceof Integer) {
                return DType.DT_LONG;
            } else if (num instanceof Double || num instanceof Short || num instanceof Float) {
                return DType.DT_DOUBLE;
            } else {
                throw new RuntimeException(
                        "Unknown primitive json number type: " + num + " of type " + num.getClass());
            }//from  w w  w.ja v a2s . c  o  m
        } else {
            throw new RuntimeException("Unknown primitive json type: " + prim);
        }
    } else if (obj.isJsonArray()) {
        return DType.DT_COLL;
    } else if (obj.isJsonObject()) {
        return DType.DT_REC;
    } else {
        throw new RuntimeException("Unknown json type: " + obj + " of type " + obj.getClass());
    }
}

From source file:org.qcert.runtime.UnaryOperators.java

License:Apache License

private static void tostring(StringBuilder sb, JsonElement e) {
    if (e == null || e.isJsonNull()) {
        sb.append("null");
    } else if (e.isJsonPrimitive()) {
        tostring(sb, e.getAsJsonPrimitive());
    } else if (e.isJsonArray()) {
        tostring(sb, e.getAsJsonArray());
    } else if (e.isJsonObject()) {
        tostring(sb, e.getAsJsonObject());
    } else {//from   ww  w  .  j a  v  a  2 s.  c o  m
        sb.append(e.toString());
    }
}

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

License:LGPL

private static JsonElement clearNullsFromArray(final JsonArray node) {
    final JsonArray ret = new JsonArray();

    /*/*from   w  ww . j a v  a 2s  .c  om*/
    * Cycle through array elements. If the element is a null node itself,
    * skip it. Otherwise, add a "cleaned up" element to the result.
    */
    for (final JsonElement element : node)
        if (!element.isJsonNull())
            ret.add(clearNulls(element));

    return ret;
}

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

License:Apache License

/**
 * whether sandbox tenant has data in their mongo Tracking
 * ingestion job doesn't seem like a good solution Possible solution: check
 * mongo for data for that tenant//from   ww  w  . j av a2 s. c  o  m
 * 
 * @param mySession
 * @param token
 * @return
 */
private boolean hasUploadedData(JsonObject mySession, String token) {
    boolean result = false;
    JsonElement myTenantIdJson = mySession.get(TENANT_ID);
    if (myTenantIdJson != null && !myTenantIdJson.isJsonNull()) {
        String myTenantId = myTenantIdJson.getAsString();

        // call API
        // http(s)://xxx.xxx.xxx.xxx/api/rest/v1/tenant_metrics/<tenantId>
        String path = Constants.API_PREFIX + "/" + Constants.API_V1 + "/" + Constants.TENANT_METRIC + "/"
                + myTenantId;
        String jsonText = restClient.makeJsonRequestWHeaders(path, token, true);
        JsonObject jsonObject = (new JsonParser()).parse(jsonText).getAsJsonObject();

        if (jsonObject != null && !jsonObject.isJsonNull()) {
            JsonElement tenantMetricElement = jsonObject.get(myTenantId);

            // find element with the same tenant id if there is multiple
            // records
            if (tenantMetricElement != null && !tenantMetricElement.isJsonNull()) {

                JsonObject tenantMetricObject = tenantMetricElement.getAsJsonObject();
                if (tenantMetricObject != null && !tenantMetricObject.isJsonNull()) {

                    // get educationOrganization element
                    JsonElement educationOrganizationElement = tenantMetricObject.get(EDUCATION_ORGANIZATION);
                    if (educationOrganizationElement != null && !educationOrganizationElement.isJsonNull()) {
                        JsonObject educatinoOrganizationObject = educationOrganizationElement.getAsJsonObject();
                        if (educatinoOrganizationObject != null && !educatinoOrganizationObject.isJsonNull()) {

                            // get entityCount
                            JsonElement entityCountElement = educatinoOrganizationObject.get(ENTITY_COUNT);
                            if (entityCountElement != null && !entityCountElement.isJsonNull()) {
                                int entityCount = entityCountElement.getAsInt();
                                // if there is more than 0, then
                                // consider as data is ready
                                if (entityCount > 0) {
                                    result = true;
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    return result;
}

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

License:Apache License

/**
 * check if ProvisionLandingZone has done Check for landingZone exists and
 * that landingZone.path is defined note: the call retrieves the entire
 * tenants collections, so we need to filter to the developer's tenant. The
 * tenant name is the developer's login email.
 * /*from   www .j ava  2  s.co m*/
 * @param token
 * @return
 */
private boolean hasProvisionedLandingZone(JsonObject mySession, String token) {
    boolean uploadedData = false;
    JsonElement myTenantIdJson = mySession.get(TENANT_ID);
    if (myTenantIdJson != null && !myTenantIdJson.isJsonNull()) {
        String myTenantId = myTenantIdJson.getAsString();
        String path = Constants.API_PREFIX + "/" + Constants.TENANTS;
        JsonArray tenantJsonArray = getJsonArray(path, token);
        for (JsonElement tenantElement : tenantJsonArray) {
            JsonObject tenantObject = tenantElement.getAsJsonObject();
            if (tenantObject != null && !tenantObject.isJsonNull()) {
                JsonElement tenantId = tenantObject.get(TENANT_ID);
                if (tenantId != null && !tenantId.isJsonNull()) {
                    if (myTenantId.equals(tenantId.getAsString())) {
                        // matched with my tenant

                        // find landingZone.path is defined
                        JsonElement landingZoneJsonObject = tenantObject.get(LANDING_ZONE);
                        if (landingZoneJsonObject != null && !landingZoneJsonObject.isJsonNull()) {
                            JsonArray landingZoneArray = landingZoneJsonObject.getAsJsonArray();
                            if (landingZoneArray != null && !landingZoneJsonObject.isJsonNull()) {
                                for (JsonElement landingZone : landingZoneArray) {
                                    JsonObject landingZoneObject = landingZone.getAsJsonObject();
                                    JsonElement landingZonePath = landingZoneObject.get(PATH);
                                    if (landingZonePath != null && !landingZonePath.isJsonNull()) {
                                        uploadedData = true;
                                        break;
                                    }
                                }
                            }
                        }
                        break;
                    }
                }
            }
        }
    }
    return uploadedData;
}

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

License:Apache License

/**
 * Check authorized_ed_orgs exists and is non-empty
 * API call http(s)://xxx.xxx.xxx.xxx/api/rest/apps
 * /*from w  ww . jav a 2s .  c  o  m*/
 * @param jsonArrayApps
 * @return
 */
private boolean hasEnabledApp(JsonArray jsonArrayApps) {
    // Check authorized_ed_orgs exists and is non-empty
    boolean result = false;
    for (JsonElement jsonElement : jsonArrayApps) {
        JsonElement authorizedEdOrgObject = jsonElement.getAsJsonObject().get(AUTHORIZED_ED_ORGS);
        if (authorizedEdOrgObject != null && !authorizedEdOrgObject.isJsonNull()) {
            JsonArray authorizedEdOrgs = authorizedEdOrgObject.getAsJsonArray();
            if (authorizedEdOrgs != null && !authorizedEdOrgs.isJsonNull() && authorizedEdOrgs.size() > 0) {
                result = true;
                break;
            }
        }
    }
    return result;
}

From source file:org.slc.sli.web.controller.HomePageController.java

License:Apache License

/**
 * whether a user has a Developer role and Portal runs as sandbox mode
 * //  w  w  w.  j  a  va  2 s.  c  om
 * @return true if Portal runs as sandbox mode and he/she has a Developer role
 */
private boolean isDeveloperAndSandbox(JsonObject mySession) {

    boolean developer = false;

    boolean is_sandbox = false; //GetterUtil.getBoolean(PropsUtil.get(IS_SANDBOX));
    // check if portal is running sandbox mode.
    if (is_sandbox) {
        // Do a session check to see if sliRoles is "Application Developer"
        JsonElement sli_role_element = mySession.get(SLI_ROLES);
        if (sli_role_element != null && !sli_role_element.isJsonNull()) {
            JsonArray sli_roles = sli_role_element.getAsJsonArray();
            for (JsonElement sli_role : sli_roles) {
                if (sli_role != null && !sli_role.isJsonNull()) {
                    if (sli_role.getAsString().equals(DEVELOPER)) {
                        developer = true;
                        break;
                    }
                }
            }
        }
    }
    return developer;
}