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.hibernate.search.elasticsearch.query.impl.TwoWayFieldBridgeProjection.java

License:LGPL

private Object convertFieldValue(JsonObject hit, ConversionContext conversionContext) {
    Document tmp = new Document();

    for (String objectFieldName : objectFieldNames) {
        JsonElement jsonValue = extractFieldValue(hit.get("_source").getAsJsonObject(), objectFieldName);
        if (jsonValue == null || jsonValue.isJsonNull()) {
            continue;
        }/*from   w w  w .  jav  a  2s. c  o  m*/
        JsonObject jsonObject = jsonValue.getAsJsonObject();
        addDocumentFieldsRecursively(tmp, jsonObject, objectFieldName);
    }

    // Add to the document the additional fields created when indexing the value
    for (PrimitiveProjection subProjection : primitiveProjections.values()) {
        subProjection.addDocumentField(tmp, hit, conversionContext);
    }

    return conversionContext.twoWayConversionContext(bridge).get(absoluteName, tmp);
}

From source file:org.hibernate.search.elasticsearch.query.impl.TwoWayFieldBridgeProjection.java

License:LGPL

public void addDocumentFieldsRecursively(Document tmp, JsonElement value, String fieldName) {
    if (value == null || value.isJsonNull()) {
        return;/* w w w. j a  v a 2 s  .  c  o m*/
    }

    PrimitiveProjection configuredProjection = primitiveProjections.get(fieldName);
    if (configuredProjection != null) {
        // Those projections are handled separately, see the calling method
        return;
    }

    if (value.isJsonObject()) {
        JsonObject jsonObject = value.getAsJsonObject();
        for (Map.Entry<String, JsonElement> entry : jsonObject.entrySet()) {
            String nestedFieldName = fieldName + "." + entry.getKey();
            JsonElement nestedFieldValue = entry.getValue();
            addDocumentFieldsRecursively(tmp, nestedFieldValue, nestedFieldName);
        }
    } else if (value.isJsonArray()) {
        JsonArray jsonArray = value.getAsJsonArray();
        for (JsonElement nestedValue : jsonArray) {
            addDocumentFieldsRecursively(tmp, nestedValue, fieldName);
        }
    } else {
        JsonPrimitive primitive = value.getAsJsonPrimitive();

        if (primitive.isBoolean()) {
            tmp.add(new StringField(fieldName, String.valueOf(primitive.getAsBoolean()), Store.NO));
        } else if (primitive.isNumber()) {
            tmp.add(new DoubleField(fieldName, primitive.getAsDouble(), Store.NO));
        } else if (primitive.isString()) {
            tmp.add(new StringField(fieldName, primitive.getAsString(), Store.NO));
        } else {
            // TODO HSEARCH-2255 Better raise an exception?
            tmp.add(new StringField(fieldName, primitive.getAsString(), Store.NO));
        }
    }
}

From source file:org.hillview.storage.JsonFileLoader.java

License:Open Source License

private static ContentsKind getKind(@Nullable JsonElement e) {
    if (e == null || e.isJsonNull())
        return ContentsKind.None;
    if (e.isJsonArray() || e.isJsonObject())
        throw new RuntimeException("Values must be simple " + e);
    JsonPrimitive prim = e.getAsJsonPrimitive();
    if (prim.isBoolean())
        return ContentsKind.String;
    if (prim.isNumber())
        return ContentsKind.Double;
    if (prim.isString())
        return ContentsKind.String;
    throw new RuntimeException("Unexpected JSON value " + prim);
}

From source file:org.hillview.storage.JsonFileLoader.java

License:Open Source License

void append(IAppendableColumn[] columns, JsonElement e) {
    if (!e.isJsonObject())
        this.error("JSON array element is not a JsonObject");
    JsonObject obj = e.getAsJsonObject();
    for (IAppendableColumn col : columns) {
        JsonElement el = obj.get(col.getName());
        if (el == null || el.isJsonNull()) {
            col.appendMissing();//from  www.j a va  2 s  .  c  om
            continue;
        }

        if (!el.isJsonPrimitive())
            this.error("JSON array element is a non-primitive field");
        JsonPrimitive prim = el.getAsJsonPrimitive();

        if (prim.isBoolean()) {
            col.append(prim.getAsBoolean() ? "true" : "false");
        } else if (prim.isNumber()) {
            col.append(prim.getAsDouble());
        } else if (prim.isString()) {
            col.parseAndAppendString(prim.getAsString());
        } else {
            this.error("Unexpected Json value" + prim.toString());
        }
    }
}

From source file:org.i3xx.step.uno.impl.util.GsonHashMapDeserializer.java

License:Apache License

public Object deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    //System.out.println("GND deserialize: "+json.toString());

    if (json.isJsonNull())
        return null;
    else if (json.isJsonPrimitive())
        return handlePrimitive(json.getAsJsonPrimitive());
    else if (json.isJsonArray())
        return handleArray(json.getAsJsonArray(), context);
    else/*from ww  w. java 2  s  . c o  m*/
        return handleObject(json.getAsJsonObject(), context);
}

From source file:org.indigo.occiprobe.openstack.CmdbClient.java

License:Apache License

/**
 * This method access the CMDB service in order to retrieve the available 
 * data from a Cloud Provider (i.e. its location, provided services, etc.)
 * @param providerId Represents the identifier of the Cloud provider
 * @return An object with all the information retrieved
 *///from   ww w.  j  a va 2s .  c  o  m
public CloudProviderInfo getProviderData(String providerId) {
    // Call to CMDB API
    String providerUrl = cmdbUrl + "/provider/id/" + providerId + "/has_many/services?include_docs=true";
    WebTarget target = client.target(providerUrl);
    Invocation.Builder invocationBuilder = target.request();
    Response response = invocationBuilder.get();
    String message = response.readEntity(String.class);

    //System.out.println(message);

    // Retrieve the services list
    JsonElement jelement = new JsonParser().parse(message);
    JsonObject parsedRes = jelement.getAsJsonObject();
    JsonArray listArray = parsedRes.getAsJsonArray("rows");
    if (listArray.isJsonNull() || listArray.size() == 0) {
        return null;
    }

    String occiEndpoint = null;
    String keystoneEndpoint = null;
    int type = CloudProviderInfo.OPENNEBULA;
    boolean isMonitored = false;
    boolean isBeta = false;
    boolean isProduction = false;

    Iterator<JsonElement> myIter = listArray.iterator();
    while (myIter.hasNext()) {
        JsonObject currentResource = myIter.next().getAsJsonObject();
        JsonObject currentDoc = currentResource.get("doc").getAsJsonObject();
        JsonObject currentData = currentDoc.get("data").getAsJsonObject();

        String currentServiceType = currentData.get("service_type").getAsString();
        String currentType = currentData.get("type").getAsString();
        JsonElement jsonEndpoint = currentData.get("endpoint");
        String currentEndpoint = null;
        if (jsonEndpoint == null || jsonEndpoint.isJsonNull()) {
            return null;
        }
        currentEndpoint = jsonEndpoint.getAsString();

        if (currentServiceType.equalsIgnoreCase("eu.egi.cloud.vm-management.occi")) {
            occiEndpoint = currentEndpoint;
            JsonElement currentBeta = currentData.get("beta");
            JsonElement currentProduction = currentData.get("in_production");
            JsonElement currentMonitored = currentData.get("node_monitored");

            // Retrieve the rest of info from the OCCI service
            if (currentBeta != null && currentBeta.getAsString().equalsIgnoreCase("Y")) {
                isBeta = true;
            }
            if (currentMonitored != null && currentMonitored.getAsString().equalsIgnoreCase("Y")) {
                isMonitored = true;
            }
            if (currentProduction != null && currentProduction.getAsString().equalsIgnoreCase("Y")) {
                isProduction = true;
            }
        } else if (currentServiceType.equalsIgnoreCase("eu.egi.cloud.vm-management.openstack")) {
            keystoneEndpoint = currentEndpoint;
            type = CloudProviderInfo.OPENSTACK;
        }
    }

    CloudProviderInfo myProvider = new CloudProviderInfo(providerId, occiEndpoint, keystoneEndpoint, type,
            isMonitored, isBeta, isProduction);

    return myProvider;
}

From source file:org.jboss.aerogear.android.cookbook.agreddit.PageConsumer.java

License:Apache License

private String getFromJSON(JsonElement element, String nextIdentifier) {
    String[] identifiers = nextIdentifier.split("\\.");
    for (String identifier : identifiers) {
        element = element.getAsJsonObject().get(identifier);
    }//from   w  w  w .j  av  a 2 s  . c  o m
    if (element.isJsonNull()) {
        return null;
    }

    return element.getAsString();
}

From source file:org.jboss.aerogear.android.impl.pipeline.paging.URIBodyPageParser.java

License:Apache License

private String getFromJSON(JsonElement element, String nextIdentifier) {
    String[] identifiers = nextIdentifier.split("\\.");
    for (String identifier : identifiers) {
        element = element.getAsJsonObject().get(identifier);
    }/* w ww  .  ja  v  a 2  s.c o  m*/

    if (element.isJsonNull()) {
        return null;
    } else {
        return element.getAsString();
    }
}

From source file:org.jbpm.form.builder.services.encoders.FormRepresentationDecoderImpl.java

License:Apache License

private Object fromJsonValue(JsonElement elem) {
    if (elem.isJsonPrimitive() && elem.getAsJsonPrimitive().isString()) {
        return elem.getAsString();
    } else if (elem.isJsonPrimitive() && elem.getAsJsonPrimitive().isNumber()) {
        return elem.getAsNumber();
    } else if (elem.isJsonArray()) {
        return asList(elem.getAsJsonArray());
    } else if (elem.isJsonNull()) {
        return null;
    } else if (elem.isJsonObject()) {
        return asMap(elem.getAsJsonObject());
    } else {/*  w  w w  .j  av a 2s.c o  m*/
        return "";
    }
}

From source file:org.jclouds.json.internal.ParseObjectFromElement.java

License:Apache License

public Object apply(JsonElement input) {
    Object value = null;/*from w w w  .  ja  v a2s  .  c  o  m*/
    if (input == null || input.isJsonNull()) {
        value = null;
    } else if (input.isJsonPrimitive()) {
        JsonPrimitive primitive = input.getAsJsonPrimitive();
        if (primitive.isNumber()) {
            value = primitive.getAsNumber();
        } else if (primitive.isBoolean()) {
            value = primitive.getAsBoolean();
        } else {
            value = primitive.getAsString();
        }
    } else if (input.isJsonArray()) {
        value = Lists.newArrayList(Iterables.transform(input.getAsJsonArray(), this));
    } else if (input.isJsonObject()) {
        value = Maps.<String, Object>newLinkedHashMap(
                Maps.transformValues(JsonObjectAsMap.INSTANCE.apply(input.getAsJsonObject()), this));
    }
    return value;
}