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:com.bizosys.dataservice.sql.KVPairsSerde.java

License:Apache License

public static final void deserJson(final String jsonStr, final Map<String, String> container) {

    if (StringUtils.isEmpty(jsonStr))
        return;//w ww .  j a  v a2s .com

    Gson gson = new Gson();
    JsonElement jsonElem = gson.fromJson(jsonStr, JsonElement.class);
    JsonObject jsonObj = jsonElem.getAsJsonObject();

    if (!jsonObj.has("variables"))
        return;

    Iterator<JsonElement> variablesIterator = jsonObj.get("variables").getAsJsonArray().iterator();

    while (variablesIterator.hasNext()) {
        JsonObject variableObj = (JsonObject) variablesIterator.next();

        if (variableObj.has("key") && variableObj.has("value")) {
            String varKey = variableObj.get("key").getAsString();
            JsonElement obj = variableObj.get("value");
            String varVal = (obj.isJsonNull()) ? null : obj.getAsString();
            container.put(varKey, varVal);
        }
    }
    if (LOG.isDebugEnabled())
        LOG.debug("Parsed variables are : " + container.toString());
}

From source file:com.bzcentre.dapiPush.MeetingPayload.java

License:Open Source License

private Object extractAps(JsonElement in) {
    if (in == null || in.isJsonNull())
        return null;

    if (in.isJsonArray()) {
        List<Object> list = new ArrayList<>();
        JsonArray arr = in.getAsJsonArray();
        for (JsonElement anArr : arr) {
            list.add(extractAps(anArr));
        }/*from   w  w  w  .  ja v  a  2  s.  c  om*/
        return list;
    } else if (in.isJsonObject()) {
        Map<String, Object> map = new LinkedTreeMap<>();
        JsonObject obj = in.getAsJsonObject();
        Set<Map.Entry<String, JsonElement>> entitySet = obj.entrySet();
        for (Map.Entry<String, JsonElement> entry : entitySet) {
            map.put(entry.getKey(), extractAps(entry.getValue()));
            NginxClojureRT.log.debug(TAG + entry.getKey() + "=>" + map.get(entry.getKey()) + "=>"
                    + map.get(entry.getKey()).getClass().getTypeName());
            switch (entry.getKey()) {
            case "dapi":
                this.setDapi(map.get(entry.getKey()));
                break;
            case "acme1":
                this.setAcme1(map.get(entry.getKey()));
                break;
            case "acme2":
                this.setAcme2(map.get(entry.getKey()));
                break;
            case "acme3":
                this.setAcme3(map.get(entry.getKey()));
                break;
            case "acme4":
                this.setAcme4(map.get(entry.getKey()));
                break;
            case "acme5":
                this.setAcme5(map.get(entry.getKey()));
                break;
            case "acme6":
                this.setAcme6(map.get(entry.getKey()));
                break;
            case "acme7":
                this.setAcme7(map.get(entry.getKey()));
                break;
            case "acme8":
                this.setAcme8(map.get(entry.getKey()));
                break;
            case "aps":
                NginxClojureRT.log.debug(TAG + "aps initialized");
                break;
            case "badge":
                this.getAps().setBadge(map.get(entry.getKey()));
                break;
            case "sound":
                this.getAps().setSound(map.get(entry.getKey()));
                break;
            case "alert":
                NginxClojureRT.log.debug(TAG + "alert initialized");
                break;
            case "title":
                this.getAps().getAlert().setTitle(map.get(entry.getKey()));
                break;
            case "body":
                this.getAps().getAlert().setBody(map.get(entry.getKey()));
                break;
            case "action-loc-key":
                this.getAps().getAlert().setActionLocKey(map.get(entry.getKey()));
                break;
            default:
                NginxClojureRT.log.info(TAG + "Unhandled field : " + entry.getKey());
                break;
            }
        }
        return map;
    } else if (in.isJsonPrimitive()) {
        JsonPrimitive prim = in.getAsJsonPrimitive();
        if (prim.isBoolean()) {
            return prim.getAsBoolean();
        } else if (prim.isString()) {
            return prim.getAsString();
        } else if (prim.isNumber()) {
            Number num = prim.getAsNumber();
            // here you can handle double int/long values
            // and return any type you want
            // this solution will transform 3.0 float to long values
            if (Math.ceil(num.doubleValue()) == num.longValue()) {
                return num.longValue();
            } else {
                return num.doubleValue();
            }
        }
    }
    NginxClojureRT.log.info("Handling json null");
    return null;
}

From source file:com.ca.dvs.utilities.raml.RamlUtil.java

License:Open Source License

/**
 * @param jsonString/*from ww  w.ja  v a2  s  . c  om*/
 * @return the appropriate JsonElement object from the parsed jsonString
 */
private static Object getJsonElementValue(String jsonString) {

    JsonParser jsonParser = new JsonParser();
    JsonElement jsonElement = jsonParser.parse(jsonString);

    if (jsonElement.isJsonObject()) {

        return jsonElement.getAsJsonObject();

    } else if (jsonElement.isJsonArray()) {

        return jsonElement.getAsJsonArray();

    } else if (jsonElement.isJsonNull()) {

        return jsonElement.getAsJsonNull();

    } else if (jsonElement.isJsonPrimitive()) {

        return jsonElement.getAsJsonPrimitive();

    } else {

        return null;

    }
}

From source file:com.canoo.dolphin.impl.codec.ValueEncoder.java

License:Apache License

static Object decodeValue(JsonElement jsonElement) {
    if (jsonElement == null || jsonElement.isJsonNull()) {
        return null;
    }//from  www .j  av a  2  s .  c  o  m
    if (!jsonElement.isJsonPrimitive()) {
        throw new JsonParseException("Illegal JSON detected");
    }
    final JsonPrimitive value = (JsonPrimitive) jsonElement;

    if (value.isString()) {
        return value.getAsString();
    } else if (value.isBoolean()) {
        return value.getAsBoolean();
    } else if (value.isNumber()) {
        return value.getAsNumber();
    }
    throw new JsonParseException("Currently only String, Boolean, or Number are allowed as primitives");
}

From source file:com.ccc.crest.core.cache.crest.schema.option.CcpType.java

License:Open Source License

@Override
public CcpType deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    Iterator<Entry<String, JsonElement>> objectIter = ((JsonObject) json).entrySet().iterator();
    while (objectIter.hasNext()) {
        Entry<String, JsonElement> objectEntry = objectIter.next();
        String key = objectEntry.getKey();
        JsonElement value = objectEntry.getValue();
        if (DescriptionKey.equals(key))
            description = checkNull(value);
        else if (OptionalKey.equals(key))
            optional = value.getAsBoolean();
        else if (ExtraKey.equals(key)) {
            JsonElement objectElement = objectEntry.getValue();
            if (objectElement.isJsonArray()) {
                int size = ((JsonArray) objectElement).size();
                for (int i = 0; i < size; i++) {
                    JsonElement childElement = ((JsonArray) objectElement).get(i);
                    extraData.add(childElement.getAsString());
                }/*from   w w w.j av a2  s  .  c  om*/
            } else
                extraData.add(checkNull(value));

        } else if (SubContentKey.equals(key)) {
            if (!value.isJsonNull()) {
                Iterator<Entry<String, JsonElement>> varsIter = ((JsonObject) value).entrySet().iterator();
                do {
                    if (!varsIter.hasNext())
                        break;
                    Entry<String, JsonElement> varsEntry = varsIter.next();
                    CcpType subContent = new CcpType(varsEntry.getKey());
                    subContent = subContent.deserialize(varsEntry.getValue(), typeOfT, context);
                    children.add(subContent);
                } while (true);
            }
        } else if (PrettyTypeKey.equals(key))
            typePrettyName = checkNull(value);
        else if (TypeKey.equals(key))
            type = checkNull(value);
        else
            LoggerFactory.getLogger(getClass())
                    .warn(key + " has a field not currently being handled: \n" + objectEntry.toString());
    }
    return this;
}

From source file:com.ccc.crest.core.cache.crest.schema.option.CcpType.java

License:Open Source License

private String checkNull(JsonElement value) {
    if (value.isJsonNull())
        return null;
    if (NullValue.equals(value.getAsString()))
        return null;
    return value.getAsString();
}

From source file:com.ccreanga.bitbucket.rest.client.http.responseparsers.ListParser.java

License:Apache License

@Override
public List<T> apply(JsonElement json) {
    if ((json == null) || (json.isJsonNull())) {
        return Collections.emptyList();
    } else {/*from ww w .j  a  va 2  s. c om*/
        return StreamSupport.stream(json.getAsJsonArray().spliterator(), false).map(parseFunction)
                .collect(Collectors.toList());
    }
}

From source file:com.ccreanga.bitbucket.rest.client.http.responseparsers.ParserUtil.java

License:Apache License

public static boolean optionalJsonBoolean(JsonObject json, String name) {
    JsonElement element = json.get(name);
    if ((element == null) || (element.isJsonNull()))
        return false;
    return element.getAsBoolean();
}

From source file:com.ccreanga.bitbucket.rest.client.http.responseparsers.ParserUtil.java

License:Apache License

public static long optionalJsonLong(JsonObject json, String name) {
    JsonElement element = json.get(name);
    if ((element == null) || (element.isJsonNull()))
        return -1;
    return element.getAsLong();
}

From source file:com.ccreanga.bitbucket.rest.client.http.responseparsers.ParserUtil.java

License:Apache License

public static String optionalJsonString(JsonObject json, String name) {
    JsonElement element = json.get(name);
    if ((element == null) || (element.isJsonNull()))
        return null;
    return element.getAsString();
}