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.rw.legion.input.JsonRecordReader.java

License:Apache License

/**
 * Recursively traverses all levels of a JSON object and adds their contents
 * to the <code>LegionRecord</code>.
 * //from  w w  w .j a va2 s  .  c  o  m
 * @param location  The JSON path leading up to the current depth level.
 * @param element  An element that appears at the current depth level.
 */
private void traverseJson(String location, JsonElement element) {
    if (element.isJsonNull()) {
        record.setField(location, "");
    } else if (element.isJsonPrimitive()) {
        record.setField(location, element.getAsString());
    } else if (element.isJsonObject()) {
        for (Map.Entry<String, JsonElement> entry : element.getAsJsonObject().entrySet()) {
            traverseJson(location + "." + entry.getKey(), entry.getValue());
        }
    } else if (element.isJsonArray()) {
        for (int i = 0; i < element.getAsJsonArray().size(); i++) {
            traverseJson(location + "[" + new Integer(i).toString() + "]", element.getAsJsonArray().get(i));
        }
    }
}

From source file:com.ryanantkowiak.jOptionsHouseAPI.JsonUtilities.java

License:Open Source License

/**
 * Recursively print the contents of a GSON JSON element.
 * /*w w w  .ja  va2  s  .  c o  m*/
 * @param element   the GSON JSON element to be printed
 * @param prefix   output will be prefixed with this string
 */
public static void printJson(JsonElement element, String prefix) {
    if (null == prefix || prefix.isEmpty()) {
        prefix = "";
    }

    if (null == element || element.isJsonNull()) {
        System.out.println(prefix + " [null]");
        return;
    }

    else if (element.isJsonPrimitive()) {
        JsonPrimitive p = element.getAsJsonPrimitive();
        if (p.isBoolean()) {
            System.out.println(prefix + " [bool=" + p.getAsBoolean() + "]");
        } else if (p.isString()) {
            System.out.println(prefix + " [string='" + p.getAsString() + "']");
        } else if (p.isNumber()) {
            System.out.println(prefix + " [number=" + p.getAsDouble() + "]");
        }
    }

    else if (element.isJsonArray()) {
        System.out.println(prefix + " [array]");
        for (int i = 0; i < element.getAsJsonArray().size(); ++i) {
            String newPrefix = prefix + "[" + i + "]";
            printJson(element.getAsJsonArray().get(i), newPrefix);
        }
    }

    else if (element.isJsonObject()) {
        JsonObject obj = element.getAsJsonObject();

        for (Map.Entry<String, JsonElement> entry : obj.entrySet()) {
            String key = entry.getKey();
            JsonElement value = entry.getValue();
            String newPrefix = prefix + "." + key;
            printJson(value, newPrefix);
        }
    }

}

From source file:com.sdk.dyq.sample.weibo.WeiboContentDeserializer.java

License:Apache License

private String stringOrEmpty(JsonElement jsonElement) {
    return jsonElement.isJsonNull() ? "" : jsonElement.getAsString();
}

From source file:com.seleritycorp.common.base.config.ConfigUtils.java

License:Apache License

/**
 * Adds a JSON element to a Config./*from   w  ww  . ja  v  a 2  s .c o m*/
 * 
 * @param element The element to add
 * @param config The config instance to add the element to
 * @param key The key in the config space
 */
private static void loadJson(JsonElement element, ConfigImpl config, String key) {
    if (element.isJsonObject()) {
        loadJson(element.getAsJsonObject(), config, key);
    } else if (element.isJsonArray()) {
        loadJson(element.getAsJsonArray(), config, key);
    } else if (element.isJsonPrimitive()) {
        loadJson(element.getAsJsonPrimitive(), config, key);
    } else if (element.isJsonNull()) {
        // null does not need a dedicated representation, so we
        // skip this case.
    } else {
        throw new UnsupportedOperationException("Unimplemented " + "JsonElement state");
    }
}

From source file:com.seleritycorp.common.base.coreservices.RawCoreServiceClient.java

License:Apache License

/**
 * Calls a CoreServices method./*ww  w  . j  av  a  2  s. com*/
 * 
 * @param method The CoreServices method to call
 * @param params The parameters to pass to the CoreServices call
 * @param token The authentication token. Set to null to perform an unauthenticated call.
 * @param timeoutMillis The read timeout for new data on the connection.
 * @return The call's result JsonElement
 * @throws HttpException if network errors or parsing errors occured on the client.
 * @throws CallErrorException if the server responded with an error.
 */
JsonElement call(String method, JsonElement params, String token, int timeoutMillis)
        throws HttpException, CallErrorException {
    JsonObject header = new JsonObject();
    header.addProperty("user", user);
    if (token != null) {
        header.addProperty("token", token);
    }
    header.addProperty("client", client);

    JsonObject request = new JsonObject();
    request.addProperty("id", uuidGenerator.generate().toString());
    request.addProperty("method", method);
    request.add("params", params);
    request.add("header", header);

    final int effectiveTimeoutMillis = (timeoutMillis > 0) ? timeoutMillis : this.timeoutMillis;

    log.debug("Calling method " + method + " (user: " + user + ")");

    JsonObject responseObj = requestFactory.createPostJson(apiUrl, request)
            .setReadTimeoutMillis(effectiveTimeoutMillis).execute().getBodyAsJsonObject();

    log.debug("Method " + method + " done (user: " + user + ")");

    final JsonElement error = responseObj.get("error");
    if (error != null && !error.isJsonNull()) {
        throw new CallErrorException("Error: " + error.toString());
    }
    return responseObj.get("result");
}

From source file:com.shazam.shazamcrest.FieldsIgnorer.java

License:Apache License

private static void ignorePath(JsonElement jsonElement, String pathToIgnore) {
    if (!jsonElement.isJsonNull()) {
        jsonElement.getAsJsonObject().remove(getLastSegmentOf(pathToIgnore));
    }/*from  www  .  j av a2  s .c om*/
}

From source file:com.shazam.shazamcrest.matcher.DiagnosingCustomisableMatcher.java

License:Apache License

private void appendFieldJsonSnippet(Object actual, Description mismatchDescription, Gson gson) {
    JsonElement jsonTree = gson.toJsonTree(actual);
    if (!jsonTree.isJsonPrimitive() && !jsonTree.isJsonNull()) {
        mismatchDescription.appendText("\n" + gson.toJson(actual));
    }/*from  w  ww. ja  v a 2s.c o m*/
}

From source file:com.sk89q.worldedit.extent.clipboard.io.legacycompat.SignCompatibilityHandler.java

License:Open Source License

@Override
public void updateNBT(BlockStateHolder block, Map<String, Tag> values) {
    for (int i = 0; i < 4; ++i) {
        String key = "Text" + (i + 1);
        Tag value = values.get(key);//  w ww.java 2  s .com
        if (value instanceof StringTag) {
            String storedString = ((StringTag) value).getValue();
            JsonElement jsonElement = null;
            if (storedString != null && storedString.startsWith("{")) {
                try {
                    jsonElement = new JsonParser().parse(storedString);
                } catch (JsonSyntaxException ex) {
                    // ignore: jsonElement will be null in the next check
                }
            }
            if (jsonElement == null) {
                jsonElement = new JsonPrimitive(storedString == null ? "" : storedString);
            }
            if (jsonElement.isJsonObject()) {
                continue;
            }

            if (jsonElement.isJsonNull()) {
                jsonElement = new JsonPrimitive("");
            }

            JsonObject jsonTextObject = new JsonObject();
            jsonTextObject.add("text", jsonElement);
            values.put("Text" + (i + 1), new StringTag(jsonTextObject.toString()));
        }
    }
}

From source file:com.skysql.manager.api.Backups.java

License:Open Source License

public Backups deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException, NullPointerException {

    Backups backups = new Backups();

    JsonElement jsonElement = json.getAsJsonObject().get("backups");
    if (jsonElement.isJsonNull()) {
        backups.setBackupsList(null);// ww  w.  j  a v  a  2  s  .c o m
    } else {

        JsonArray array = jsonElement.getAsJsonArray();
        int length = array.size();

        LinkedHashMap<String, BackupRecord> backupsList = new LinkedHashMap<String, BackupRecord>(length);
        for (int i = 0; i < length; i++) {
            JsonObject backupJson = array.get(i).getAsJsonObject();
            JsonElement element;
            String id = (element = backupJson.get("backupid")).isJsonNull() ? null : element.getAsString();
            String node = (element = backupJson.get("nodeid")).isJsonNull() ? null : element.getAsString();
            String level = (element = backupJson.get("level")).isJsonNull() ? null : element.getAsString();
            String parent = (element = backupJson.get("parentid")).isJsonNull() ? null : element.getAsString();
            String status = (element = backupJson.get("state")).isJsonNull() ? null : element.getAsString();
            String started = (element = backupJson.get("started")).isJsonNull() ? null : element.getAsString();
            String updated = (element = backupJson.get("updated")).isJsonNull() ? null : element.getAsString();
            String restored = (element = backupJson.get("restored")).isJsonNull() ? null
                    : element.getAsString();
            String size = (element = backupJson.get("size")).isJsonNull() ? null : element.getAsString();
            String storage = (element = backupJson.get("backupurl")).isJsonNull() ? null
                    : element.getAsString();
            String log = (element = backupJson.get("log")).isJsonNull() ? null : element.getAsString();
            BackupRecord backupRecord = new BackupRecord(id, status, started, updated, level, node, size,
                    storage, restored, log, parent);
            backupsList.put(id, backupRecord);
        }
        backups.setBackupsList(backupsList);
    }
    return backups;

}

From source file:com.skysql.manager.api.BackupStates.java

License:Open Source License

public BackupStates deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException, NullPointerException {
    BackupStates backupStates = new BackupStates();

    JsonElement jsonElement = json.getAsJsonObject().get("backupStates");
    if (jsonElement.isJsonNull()) {
        backupStates.setBackupStatesDescriptions(null);
    } else {//from w  w w.ja v a 2  s . c  o m
        JsonArray array = jsonElement.getAsJsonArray();
        int length = array.size();

        LinkedHashMap<String, String> descriptions = new LinkedHashMap<String, String>(length);
        for (int i = 0; i < length; i++) {
            JsonObject pair = array.get(i).getAsJsonObject();
            descriptions.put(pair.get("state").getAsString(), pair.get("description").getAsString());
        }
        backupStates.setBackupStatesDescriptions(descriptions);
    }

    return backupStates;
}