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.eel.kitchen.jsonschema.GsonProvider.java

License:Open Source License

private JsonNode gsonToJsonNode(final JsonElement element) {
    if (element.isJsonNull())
        return factory.nullNode();
    if (element.isJsonPrimitive())
        return gsonToValueNode(element.getAsJsonPrimitive());

    return element.isJsonArray() ? gsonToArrayNode(element) : gsonToObjectNode(element);
}

From source file:org.fenixedu.bennu.oauth.api.json.ServiceApplicationAdapter.java

License:Open Source License

@Override
public JsonElement view(ExternalApplication obj, JsonBuilder ctx) {
    JsonElement view = super.view(obj, ctx);
    JsonObject jsonObject = view.getAsJsonObject();
    JsonElement ipAddresses = ((ServiceApplication) obj).getIpAddresses();
    if (ipAddresses == null || ipAddresses.isJsonNull()) {
        ipAddresses = new JsonArray();
    }//from   w w  w  .  j  a v a  2 s. c  o m
    jsonObject.add("ipAddresses", ipAddresses);
    return jsonObject;
}

From source file:org.fenixedu.spaces.domain.Information.java

License:Open Source License

@SuppressWarnings("unchecked")
public <T extends Object> Optional<T> getMetadata(String field) {
    Optional<JsonElement> spec = getClassification().getMetadataSpecJson(field);
    if (spec.isPresent()) {
        JsonObject jsObj = spec.get().getAsJsonObject();
        final String type = jsObj.get("type").getAsString();
        final JsonObject metadata = getMetadata().getAsJsonObject();

        JsonElement fieldValue = metadata.get(field);

        if (fieldValue == null || fieldValue.isJsonNull()) {
            return Optional.empty();
        }//w  w  w  . java2  s .com

        if (Boolean.class.getName().equalsIgnoreCase(type)) {
            return (Optional<T>) Optional.of(new Boolean(fieldValue.getAsBoolean()));
        }
        if (Integer.class.getName().equalsIgnoreCase(type)) {
            return (Optional<T>) Optional.of(new Integer(fieldValue.getAsInt()));
        }
        if (BigDecimal.class.getName().equalsIgnoreCase(type)) {
            return (Optional<T>) Optional.of(fieldValue.getAsBigDecimal());
        }

        return (Optional<T>) Optional.of(new String(fieldValue.getAsString()));
    }
    return Optional.empty();
}

From source file:org.ff4j.elastic.FeatureConverter.java

License:Apache License

@SuppressWarnings({ "unchecked", "rawtypes" })
@Override/*from www. ja va  2  s  .c o  m*/
public Feature deserialize(JsonElement json, Type type, JsonDeserializationContext context) {

    String uid = json.getAsJsonObject().get("uid").getAsString();

    Feature feature = new Feature(uid);

    // Property "enable"
    JsonElement enable = json.getAsJsonObject().get("enable");
    if (enable != null) {
        feature.setEnable(enable.getAsBoolean());
    }

    // Description
    JsonElement description = json.getAsJsonObject().get("description");
    if (description != null) {
        feature.setDescription(description.getAsString());
    }

    // Group
    JsonElement group = json.getAsJsonObject().get("group");
    if (group != null) {
        feature.setGroup(group.getAsString());
    }

    // Permissions
    JsonElement permissions = json.getAsJsonObject().get("permissions");
    if (permissions != null) {
        Set<String> auths = gson.fromJson(permissions, new TypeToken<HashSet<String>>() {
        }.getType());
        feature.setPermissions(auths);
    }

    // Flipping strategy
    JsonElement flippingStrategy = json.getAsJsonObject().get("flippingStrategy");
    if (flippingStrategy != null && !flippingStrategy.isJsonNull()) {
        Map<String, ?> flippingStrategyParameters = gson.fromJson(flippingStrategy,
                new TypeToken<HashMap<String, ?>>() {
                }.getType());
        String flippingStrategyType = flippingStrategyParameters.get("type").toString();
        Map<String, String> initParams = (Map<String, String>) flippingStrategyParameters.get("initParams");
        // Adding flipping strategy
        feature.setFlippingStrategy(
                MappingUtil.instanceFlippingStrategy(uid, flippingStrategyType, initParams));
    }

    // Custom properties
    JsonElement customProperties = json.getAsJsonObject().get("customProperties");
    if (customProperties != null && !customProperties.isJsonNull()) {
        Map<String, LinkedTreeMap<String, Object>> map = new Gson().fromJson(customProperties,
                new TypeToken<com.google.gson.internal.LinkedTreeMap<String, LinkedTreeMap<String, Object>>>() {
                }.getType());
        for (Entry<String, LinkedTreeMap<String, Object>> element : map.entrySet()) {
            LinkedTreeMap<String, Object> propertyValues = element.getValue();
            // Getting values
            String pName = (String) propertyValues.get("name");
            String pType = (String) propertyValues.get("type");
            String pValue = (String) propertyValues.get("value");
            String desc = (String) propertyValues.get("description");
            Object fixedValues = propertyValues.get("fixedValues");
            Set pFixedValues = fixedValues != null ? new HashSet((Collection) fixedValues) : null;
            // Creating property with it
            Property<?> property = PropertyFactory.createProperty(pName, pType, pValue, desc, pFixedValues);
            feature.addProperty(property);
        }
    }

    return feature;
}

From source file:org.fracturedatlas.athena.web.serialization.JsonTicketSerializer.java

License:Open Source License

public PTicket deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    try {//  w w w.  j a v  a  2  s . c o  m
        PTicket pTicket = new PTicket();
        JsonObject ticketObj = json.getAsJsonObject();
        pTicket.setId(JsonUtil.nullSafeGetAsString(ticketObj.get("id")));
        ticketObj.remove("id");
        for (Entry<String, JsonElement> entry : ticketObj.entrySet()) {
            JsonElement val = entry.getValue();
            if (val.isJsonArray()) {
                JsonArray jsonArray = val.getAsJsonArray();
                Iterator<JsonElement> iter = jsonArray.iterator();
                while (iter.hasNext()) {
                    pTicket.getProps().add(entry.getKey(), iter.next().getAsString());
                }

            } else if (val.isJsonNull()) {
                pTicket.put(entry.getKey(), null);
            } else {
                pTicket.put(entry.getKey(), val.getAsString());
            }
        }
        return pTicket;
    } catch (JsonParseException jpe) {
        jpe.printStackTrace();
        throw jpe;
    }
}

From source file:org.fracturedatlas.athena.web.util.JsonUtil.java

License:Open Source License

public static String nullSafeGetAsString(JsonElement e) {
    if (e == null) {
        return null;
    } else if (e.isJsonNull()) {
        return null;
    } else {/* w w  w.ja va2  s .com*/
        return e.getAsString();
    }
}

From source file:org.greenrobot.eventbus.EventBus.java

License:Apache License

private Object getValue(Method.Data data, JsonObject json) {
    if (data == null)
        return null;

    String id = data.getId();/*from ww w  .  j a v  a  2 s . co  m*/
    Class<?> clazz = data.getType();
    boolean isNull = data.getNull();

    JsonElement value = json == null ? new JsonNull() : json.get(id);
    if (!isNull && value.isJsonNull())
        throw new EventBusException(
                "Method.Data[" + data + "], the id[" + id + "] has null value to give " + data.getType());

    if (!value.isJsonNull()) {
        if (clazz == Boolean.class) {
            return value.getAsBoolean();
        } else if (clazz == Integer.class) {
            return value.getAsInt();
        } else if (clazz == Long.class) {
            return value.getAsLong();
        } else if (clazz == Float.class) {
            return value.getAsFloat();
        } else if (clazz == Double.class) {
            return value.getAsDouble();
        } else if (clazz == String.class) {
            return value.getAsString();
        } else if (clazz == Byte.class) {
            return value.getAsByte();
        } else if (clazz == char.class) {
            return value.getAsCharacter();
        } else {
            return new Gson().fromJson(value, clazz);
        }
    }
    return null;
}

From source file:org.greenrobot.eventbus.EventBus.java

License:Apache License

private Bundle getBundleByPage(Page page, JsonObject json) throws Exception {
    if (page != null && json != null && page.getBundleList().size() > 0) {
        Bundle bundle = new Bundle();
        for (Page.Bundle item : page.getBundleList()) {
            String id = item.getId();
            String key = item.getKey();
            Class<? extends Serializable> clazz = item.getType();
            boolean isNull = item.getNull();

            JsonElement value = json.get(id);
            if (!isNull && value.isJsonNull())
                throw new EventBusException(
                        "Page.Bundle[" + item + "], the id[" + id + "] has null value to give " + key);

            if (!value.isJsonNull()) {
                if (clazz == Boolean.class) {
                    bundle.putBoolean(key, value.getAsBoolean());
                } else if (clazz == Integer.class) {
                    bundle.putInt(key, value.getAsInt());
                } else if (clazz == Long.class) {
                    bundle.putLong(key, value.getAsLong());
                } else if (clazz == Float.class) {
                    bundle.putFloat(key, value.getAsFloat());
                } else if (clazz == Double.class) {
                    bundle.putDouble(key, value.getAsDouble());
                } else if (clazz == String.class) {
                    bundle.putString(key, value.getAsString());
                } else if (clazz == Byte.class) {
                    bundle.putByte(key, value.getAsByte());
                } else if (clazz == char.class) {
                    bundle.putChar(key, value.getAsCharacter());
                } else {
                    bundle.putSerializable(key, new Gson().fromJson(value, clazz));
                }//from   www. j av  a 2  s  . c om
            }
        }
        return bundle;
    }
    return null;
}

From source file:org.hibernate.search.backend.elasticsearch.gson.impl.CrawlingJsonAccessor.java

License:LGPL

@Override
public JsonElement getOrCreate(JsonObject root, Supplier<? extends JsonElement> newValueSupplier)
        throws UnexpectedJsonElementTypeException {
    P parent = getParentAccessor().getOrCreate(root);
    JsonElement currentValue = doGet(parent);
    if (currentValue == null || currentValue.isJsonNull()) {
        JsonElement result = newValueSupplier.get();
        doSet(parent, result);//from   ww  w .j  a va 2  s. co m
        return result;
    } else {
        return currentValue;
    }
}

From source file:org.hibernate.search.backend.elasticsearch.gson.impl.TypingJsonAccessor.java

License:LGPL

private T fromElement(JsonElement parent) throws UnexpectedJsonElementTypeException {
    JsonElementType<T> expectedType = getExpectedElementType();
    if (parent == null || parent.isJsonNull()) {
        return null;
    } else if (!expectedType.isInstance(parent)) {
        throw new UnexpectedJsonElementTypeException(this, expectedType, parent);
    } else {/*  w  ww .  j  a  v  a 2s  .  com*/
        return expectedType.fromElement(parent);
    }
}