Example usage for com.google.gson JsonObject isJsonNull

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

Introduction

In this page you can find the example usage for com.google.gson JsonObject 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:net.minecraftforge.common.crafting.CraftingHelper.java

License:Open Source License

public static IRecipe getRecipe(JsonObject json, JsonContext context) {
    if (json == null || json.isJsonNull())
        throw new JsonSyntaxException("Json cannot be null");
    if (context == null)
        throw new IllegalArgumentException("getRecipe Context cannot be null");

    String type = context.appendModId(JsonUtils.getString(json, "type"));
    if (type.isEmpty())
        throw new JsonSyntaxException("Recipe type can not be an empty string");

    IRecipeFactory factory = recipes.get(new ResourceLocation(type));
    if (factory == null)
        throw new JsonSyntaxException("Unknown recipe type: " + type);

    return factory.parse(context, json);
}

From source file:org.fenixedu.ulisboa.specifications.ui.teacher.PagesAdminBean.java

License:Open Source License

protected static Optional<String> asString(JsonObject jsonObject, String field) {
    if (jsonObject.has(field)) {
        if (jsonObject.get(field) != null && !jsonObject.isJsonNull() && jsonObject.get(field).isJsonPrimitive()
                && !"null".equals(jsonObject.get(field).getAsString())) {
            return Optional.ofNullable(jsonObject.get(field).getAsString());
        }//from  w w  w.  j a va2  s.  c  o m
    }
    return Optional.empty();
}

From source file:org.mitre.provenance.plusobject.json.ProvenanceCollectionDeserializer.java

License:Apache License

public ProvenanceCollection deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    if (!json.isJsonObject()) {
        log.info("Cannot deserialize this: " + json);
        throw new JsonParseException("Can only deserialize objects");
    }//w w w  .  j av a 2  s.c  o m
    JsonObject obj = json.getAsJsonObject();

    ProvenanceCollection col = new ProvenanceCollection();

    JsonElement nodes = obj.get("nodes");
    JsonElement links = obj.get("links");
    JsonElement actors = obj.get("actors");

    if (!nodes.isJsonArray())
        throw new JsonParseException("Missing top-level nodes array");
    if (!links.isJsonArray())
        throw new JsonParseException("Missing top-level links array");
    if (!actors.isJsonArray())
        throw new JsonParseException("Missing top-level actors array");

    JsonArray n = (JsonArray) nodes;
    JsonArray l = (JsonArray) links;
    JsonArray a = (JsonArray) actors;

    for (JsonElement actor : a) {
        if (!actor.isJsonObject())
            throw new JsonParseException("Actors list contains non-object " + actor.toString());

        PLUSActor convertedActor = convertActor((JsonObject) actor);
        // log.info("Adding converted actor " +convertedActor);
        col.addActor(convertedActor);
    }

    for (JsonElement e : n) {
        if (!e.isJsonObject())
            throw new JsonParseException("Node list contains non-object " + e.toString());
        JsonObject o = (JsonObject) e;

        // NPID nodes are dummy stand-ins, and not provenance objects to be added.
        if ("npid".equals(o.get("type").getAsString()))
            continue;

        PLUSObject pobj = convertObject(o, col);
        col.addNode(pobj);
    }

    for (JsonElement linkItem : l) {
        if (!linkItem.isJsonObject())
            throw new JsonParseException("Link list contains non-object " + linkItem.toString());
        JsonObject link = (JsonObject) linkItem;

        if (link == null || link.isJsonNull()) {
            log.warning("Null link; skipping");
            continue;
        } else if (link.get("label") == null) {
            log.warning("Link " + link + " MISSING type");
            continue;
        }

        if (link.get("label") == null || link.get("label").isJsonNull())
            throw new JsonParseException("Missing attribute label on link/edge " + link);
        if (link.get("type") == null || link.get("type").isJsonNull())
            throw new JsonParseException("Missing attribute type on link/edge " + link);

        String label = link.get("label").getAsString();
        String type = link.get("type").getAsString();

        if (PLUSEdge.isProvenanceEdgeType(type) && !"npe".equals(label)) {
            PLUSEdge e = convertEdge(link, col);
            if (e != null)
                col.addEdge(e);
        } else {
            NonProvenanceEdge npe = convertNPE(link);
            if (npe != null)
                col.addNonProvenanceEdge(npe);
        }
    } // End for

    return col;
}

From source file:org.opendaylight.vtn.javaapi.resources.AbstractResource.java

License:Open Source License

/**
 * Check for 400, 404, 409 and 503 errors returns from platform layer
 * components//  ww  w .  j a  va 2s  . c o  m
 * 
 * @param info
 */
protected void checkForSpecificErrors(JsonObject info) {
    if (info != null && !info.isJsonNull() && info.has(VtnServiceJsonConsts.ERROR)) {
        int errorCode = info.get(VtnServiceJsonConsts.ERROR).getAsJsonObject().get(VtnServiceJsonConsts.CODE)
                .getAsInt();
        if (errorCode == UncResultCode.UNC_INVALID_FORMAT.getValue()
                || errorCode == UncResultCode.UNC_NOT_FOUND.getValue()
                || errorCode == UncResultCode.UNC_CONFLICT_FOUND.getValue()
                || errorCode == UncResultCode.UNC_TOO_MANY_ENTITIES_FOUND.getValue()
                || errorCode == UncResultCode.UNC_SERVICE_UNAVILABLE.getValue())
            createErrorInfo(errorCode);
    }
}

From source file:org.openhab.binding.csas.internal.CSASBinding.java

License:Open Source License

private String safeGetString(JsonObject jobject, String value) {
    if (jobject == null || jobject.isJsonNull() || !jobject.has(value))
        return "null";
    return (jobject.get(value).isJsonNull() ? "N/A" : jobject.get(value).getAsString());
}

From source file:org.openhab.binding.km200.handler.KM200GatewayHandler.java

License:Open Source License

/**
 * Checks bridges configuration//from w  ww  .ja va 2 s.c  o m
 */
private boolean checkConfiguration() {
    /* Get HTTP Data from device */
    JsonObject nodeRoot = remoteDevice.getServiceNode("/gateway/DateTime");
    if (nodeRoot == null || nodeRoot.isJsonNull()) {
        logger.debug("Communication is not possible!");
        updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
                "No communication possible with gateway");
        return false;
    }
    logger.debug("Test of the communication to the gateway was successful..");

    /* Testing the received data, is decryption working? */
    try {
        nodeRoot.get("type").getAsString();
        nodeRoot.get("id").getAsString();
    } catch (JsonParseException e) {
        logger.debug("The data is not readable, check the key and password configuration! {}", e.getMessage());
        updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "Wrong gateway configuration");
        return false;
    }
    return true;
}

From source file:org.openhab.binding.km200.internal.handler.KM200DataHandler.java

License:Open Source License

/**
 * This function checks the state of a service on the device
 *//*from w ww  .j ava2s  .co  m*/
public State getProvidersState(String service, String itemType, Map<String, String> itemPara) {
    synchronized (remoteDevice) {
        String type = null;
        KM200ServiceObject object = null;
        JsonObject jsonNode;

        logger.debug("Check state of: {}  item: {}", service, itemType);
        if (remoteDevice.getBlacklistMap().contains(service)) {
            logger.debug("Service on blacklist: {}", service);
            return null;
        }
        if (remoteDevice.containsService(service)) {
            object = remoteDevice.getServiceObject(service);
            if (object.getReadable() == 0) {
                logger.warn("Service is listed as protected (reading is not possible): {}", service);
                return null;
            }
            type = object.getServiceType();
        } else {
            logger.warn("Service is not in the determined device service list: {}", service);
            return null;
        }
        /* For using of virtual services only one receive on the parent service is needed */
        if (!object.getUpdated() || (object.getVirtual() == 1
                && !remoteDevice.getServiceObject(object.getParent()).getUpdated())) {
            if (object.getVirtual() == 1) {
                logger.debug("Receive data for an virtual object");
                /* If it's a virtual service then receive the data from parent service */
                jsonNode = remoteDevice.getServiceNode(object.getParent());
            } else {
                logger.debug("Receive data");
                jsonNode = remoteDevice.getServiceNode(service);
            }

            if (jsonNode == null || jsonNode.isJsonNull()) {
                logger.error("Communication is not possible!");
                return null;
            }
            if (object.getVirtual() == 1) {
                remoteDevice.getServiceObject(object.getParent()).setJSONData(jsonNode);
                remoteDevice.getServiceObject(object.getParent()).setUpdated(true);
            } else {
                object.setJSONData(jsonNode);
            }
            object.setUpdated(true);
        } else {
            /* If already updated then use the saved data */
            if (object.getVirtual() == 1) {
                logger.debug("Get data for an virtual object");
                jsonNode = remoteDevice.getServiceObject(object.getParent()).getJSONData();
            } else {
                logger.debug("Get data");
                jsonNode = object.getJSONData();
            }
        }
        /* Data is received, now parsing it */
        return parseJSONData(jsonNode, type, service, itemType, itemPara);
    }
}

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  www.j ava 2  s .c  om
 * 
 * @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.
 * /* ww  w  .  ja v  a 2s .c o 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

/**
 * To find users have been added or not/*from w ww . j a v  a 2s. 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;
}