Example usage for com.google.gson JsonElement isJsonPrimitive

List of usage examples for com.google.gson JsonElement isJsonPrimitive

Introduction

In this page you can find the example usage for com.google.gson JsonElement isJsonPrimitive.

Prototype

public boolean isJsonPrimitive() 

Source Link

Document

provides check for verifying if this element is a primitive or not.

Usage

From source file:com.flipkart.polyguice.config.JsonConfiguration.java

License:Apache License

private void flatten(String prefix, JsonElement element) {
    if (element.isJsonPrimitive()) {
        JsonPrimitive jsonPrim = element.getAsJsonPrimitive();
        if (jsonPrim.isBoolean()) {
            configTab.put(prefix, jsonPrim.getAsBoolean());
        } else if (jsonPrim.isNumber()) {
            configTab.put(prefix, jsonPrim.getAsNumber());
        } else if (jsonPrim.isString()) {
            configTab.put(prefix, jsonPrim.getAsString());
        }//from   ww w  .  ja va  2s .c om
    } else if (element.isJsonObject()) {
        JsonObject jsonObj = element.getAsJsonObject();
        for (Map.Entry<String, JsonElement> entry : jsonObj.entrySet()) {
            String prefix1 = ((prefix != null) ? prefix + "." : "") + entry.getKey();
            flatten(prefix1, entry.getValue());
        }
    }
}

From source file:com.fooock.shodan.model.exploit.ExploitDeserializer.java

License:Open Source License

private Exploit parseJsonExploit(JsonElement json) {
    Exploit exploit = new Exploit();

    JsonObject jsonObject = json.getAsJsonObject();

    String id = jsonObject.get("_id").getAsString();
    String desc = jsonObject.get("description").getAsString();
    String source = jsonObject.get("source").getAsString();

    JsonElement jsonAuthor = jsonObject.get("author");
    if (jsonAuthor != null && !jsonAuthor.isJsonNull()) {
        if (jsonAuthor.isJsonPrimitive()) {
            String author = jsonAuthor.getAsString();
            exploit.setAuthor(author);/*  www  .j  a  v  a  2 s  .  c  o  m*/
        } else {
            JsonArray array = jsonAuthor.getAsJsonArray();
            if (array != null) {
                String resAuthors = "";
                for (JsonElement element : array) {
                    resAuthors += ", " + element.getAsString();
                }
                exploit.setAuthor(resAuthors);
            }
        }
    }

    JsonElement jsonCode = jsonObject.get("code");
    if (jsonCode != null && !jsonCode.isJsonNull()) {
        String code = jsonCode.getAsString();
        exploit.setCode(code);
    }

    JsonElement jsonType = jsonObject.get("type");
    if (jsonType != null && !jsonType.isJsonNull()) {
        String type = jsonType.getAsString();
        exploit.setType(type);
    }

    JsonElement jsonVersion = jsonObject.get("version");
    if (jsonVersion != null && !jsonVersion.isJsonNull()) {
        String version = jsonVersion.getAsString();
        exploit.setVersion(version);
    }

    JsonElement jsonPrivileged = jsonObject.get("privileged");
    if (jsonPrivileged != null && !jsonPrivileged.isJsonNull()) {
        boolean privileged = jsonPrivileged.getAsBoolean();
        exploit.setPrivileged(privileged);
    }

    JsonElement jsonPort = jsonObject.get("port");
    if (jsonPort != null && !jsonPort.isJsonNull()) {
        int port = jsonPort.getAsInt();
        exploit.setPort(port);
    }

    JsonArray jsonBid = jsonObject.getAsJsonArray("bid");
    if (jsonBid != null) {
        String[] bid = new String[jsonBid.size()];
        for (int i = 0; i < jsonBid.size(); i++) {
            bid[i] = jsonBid.get(i).getAsString();
        }
        exploit.setBid(bid);
    }

    JsonArray jsonCve = jsonObject.getAsJsonArray("cve");
    if (jsonCve != null) {
        String[] cve = new String[jsonCve.size()];
        for (int i = 0; i < jsonCve.size(); i++) {
            cve[i] = jsonCve.get(i).getAsString();
        }
        exploit.setCve(cve);
    }

    JsonArray jsonMsb = jsonObject.getAsJsonArray("msb");
    if (jsonMsb != null) {
        String[] msb = new String[jsonMsb.size()];
        for (int i = 0; i < jsonMsb.size(); i++) {
            msb[i] = jsonMsb.get(i).getAsString();
        }
        exploit.setMsb(msb);
    }

    JsonArray jsonOsvdb = jsonObject.getAsJsonArray("osvdb");
    if (jsonOsvdb != null) {
        String[] osvdb = new String[jsonOsvdb.size()];
        for (int i = 0; i < jsonOsvdb.size(); i++) {
            osvdb[i] = jsonOsvdb.get(i).getAsString();
        }
        exploit.setOsvdb(osvdb);
    }

    try {
        JsonArray jsonPlatform = jsonObject.getAsJsonArray("platform");
        if (jsonPlatform != null && jsonPlatform.isJsonArray()) {
            String[] platform = new String[jsonPlatform.size()];
            for (int i = 0; i < jsonPlatform.size(); i++) {
                platform[i] = jsonPlatform.get(i).getAsString();
            }
            exploit.setPlatform(platform);
        }
    } catch (ClassCastException err) {
        JsonElement platPrimitive = jsonObject.get("platform");
        if (platPrimitive != null && !platPrimitive.isJsonNull()) {
            exploit.setPlatform(new String[] { platPrimitive.getAsString() });
        }
    }

    exploit.setId(id);
    exploit.setDescription(desc);
    exploit.setSource(source);

    return exploit;
}

From source file:com.getperka.flatpack.codexes.CharacterCodex.java

License:Apache License

@Override
public Character readNotNull(JsonElement element, DeserializationContext context) throws Exception {
    if (element.isJsonPrimitive()) {
        JsonPrimitive primitive = element.getAsJsonPrimitive();
        // Treat a number as a BMP code point
        if (primitive.isNumber()) {
            return (char) primitive.getAsInt();
        } else {/*from   w  w w  . j  a va  2s .  c  o  m*/
            return primitive.getAsCharacter();
        }
    }
    throw new IllegalArgumentException("Cannot convert " + element.toString() + " to a char");
}

From source file:com.getperka.flatpack.codexes.DateCodex.java

License:Apache License

@Override
public D readNotNull(JsonElement element, DeserializationContext context) throws Exception {
    if (element.isJsonPrimitive()) {
        long instant;
        JsonPrimitive primitive = element.getAsJsonPrimitive();
        if (primitive.isNumber()) {
            instant = primitive.getAsLong();
        } else {/*  w  w  w  . j a  va  2  s .c  om*/
            instant = ISODateTimeFormat.dateTimeParser().parseMillis(primitive.getAsString());
        }
        return constructor.newInstance(instant);
    }
    throw new IllegalArgumentException("Could not parse " + element.toString() + " as a date value");
}

From source file:com.getperka.flatpack.codexes.DynamicCodex.java

License:Apache License

/**
 * Attempt to infer the type from the JsonElement presented.
 * <ul>//from  w  w w  . j ava 2 s.  c  om
 * <li>boolean -> {@link Boolean}
 * <li>number -> {@link BigDecimal}
 * <li>string -> {@link HasUuid} or {@link String}
 * <li>array -> {@link ListCodex}
 * <li>object -> {@link StringMapCodex}
 * </ul>
 */
@Override
public Object readNotNull(JsonElement element, DeserializationContext context) throws Exception {
    if (element.isJsonPrimitive()) {
        JsonPrimitive primitive = element.getAsJsonPrimitive();
        if (primitive.isBoolean()) {
            return primitive.getAsBoolean();
        } else if (primitive.isNumber()) {
            // Always return numbers as BigDecimals for consistency
            return primitive.getAsBigDecimal();
        } else {
            String value = primitive.getAsString();

            // Interpret UUIDs as entity references
            if (UUID_PATTERN.matcher(value).matches()) {
                UUID uuid = UUID.fromString(value);
                HasUuid entity = context.getEntity(uuid);
                if (entity != null) {
                    return entity;
                }
            }

            return value;
        }
    } else if (element.isJsonArray()) {
        return listCodex.get().readNotNull(element, context);
    } else if (element.isJsonObject()) {
        return mapCodex.get().readNotNull(element, context);
    }
    context.fail(new UnsupportedOperationException("Cannot infer data type for " + element.toString()));
    return null;
}

From source file:com.github.GsonPrettyPrinter.java

License:Open Source License

private List<String> toStringList(final JsonElement je) {
    if (je == null || je.isJsonNull())
        return new ArrayList<String>(Arrays.asList(new String[] { "null" }));
    if (je.isJsonPrimitive())
        return Collections.singletonList(je.getAsJsonPrimitive().toString());
    if (je.isJsonArray()) {
        final JsonArray jsonArray = je.getAsJsonArray();
        return arrayToStringList(jsonArray);
    } else if (je.isJsonObject()) {
        final JsonObject jsonObject = je.getAsJsonObject();
        return objectToStringList(jsonObject);
    }/*  w w w  . j av a 2 s  .c o m*/
    throw new RuntimeException("Unsupported Json element: " + je.getClass().getName());
}

From source file:com.github.maoo.indexer.client.WebScriptsAlfrescoClient.java

License:Apache License

private String getString(JsonObject responseObject, String key) {
    if (responseObject.has(key)) {
        JsonElement element = responseObject.get(key);
        if (element.isJsonPrimitive() && element.getAsJsonPrimitive().isString()) {
            return element.getAsString();
        } else {//from  w  ww . ja  va 2s.c om
            logger.warn("The {} property (={}) is not a string in document: {}",
                    new Object[] { key, element, responseObject });
        }
    } else {
        logger.warn("The key {} is missing from document: {}", key, responseObject);
    }
    return "";
}

From source file:com.github.maoo.indexer.client.WebScriptsAlfrescoClient.java

License:Apache License

private String getUsername(JsonObject userObject) {
    if (!userObject.has(USERNAME)) {
        throw new AlfrescoParseException("Json response is missing username.");
    }//  w  ww  .j  ava  2s .  c  om
    JsonElement usernameElement = userObject.get(USERNAME);
    if (!usernameElement.isJsonPrimitive() || !usernameElement.getAsJsonPrimitive().isString()) {
        throw new AlfrescoParseException("Username must be a string. It was: " + usernameElement.toString());
    }
    return usernameElement.getAsString();
}

From source file:com.github.maoo.indexer.client.WebScriptsAlfrescoClient.java

License:Apache License

private List<String> getAuthorities(JsonObject userObject) {
    List<String> authorities = new ArrayList<String>();
    if (!userObject.has(AUTHORITIES)) {
        throw new AlfrescoParseException("Json response is authorities.");
    }//w  w  w . j  a  va  2 s  . c om
    JsonElement authoritiesElement = userObject.get(AUTHORITIES);
    if (!authoritiesElement.isJsonArray()) {
        throw new AlfrescoParseException(
                "Authorities must be a json array. It was: " + authoritiesElement.toString());
    }
    JsonArray authoritiesArray = authoritiesElement.getAsJsonArray();
    for (JsonElement authorityElement : authoritiesArray) {
        if (!authorityElement.isJsonPrimitive()) {
            throw new AlfrescoParseException(
                    "Authority entry must be a string. It was: " + authoritiesElement.toString());
        }
        JsonPrimitive authorityPrimitive = authorityElement.getAsJsonPrimitive();
        if (!authorityPrimitive.isString()) {
            throw new AlfrescoParseException(
                    "Authority entry must be a string. It was: " + authoritiesElement.toString());
        }
        authorities.add(authorityPrimitive.getAsString());
    }
    return authorities;
}

From source file:com.github.strawberry.util.Json.java

License:Open Source License

public static Collection parseArray(String json) {
    JsonArray o = (JsonArray) parser.parse(json);
    Collection c = Lists.newArrayList();
    for (JsonElement value : o) {
        if (!value.isJsonPrimitive()) {
            if (value.isJsonArray()) {
                c.add(parseArray(value.toString()));
            } else if (value.isJsonObject()) {
                c.add(parse(value.toString()));
            }/*from www.  j  a  v a  2  s  . co  m*/

        } else {
            c.add(parsePrimitive(value));

        }
    }
    return c;
}