Example usage for com.google.gson JsonObject has

List of usage examples for com.google.gson JsonObject has

Introduction

In this page you can find the example usage for com.google.gson JsonObject has.

Prototype

public boolean has(String memberName) 

Source Link

Document

Convenience method to check if a member with the specified name is present in this object.

Usage

From source file:com.github.daytron.daytronmoney.conversion.ConversionClient.java

License:Open Source License

/**
 * Validates the <code>JsonObject</code> for null and invalid json format.
 * Throws MoneyConversionException if invalid.
 *
 * @param jsonObject The JsonObject to be inspected
 * @param member A JSON member string value for content validation
 * @throws MoneyConversionException if JsonObject is invalid
 *//*from ww  w.j av a2 s.  c o  m*/
private static void validateJsonObject(JsonObject jsonObject, String member) throws MoneyConversionException {
    if (jsonObject == null) {
        throw new MoneyConversionException("Cannot connect to API right now. Try " + "again later.");
    }

    if (!jsonObject.has(member)) {
        throw new MoneyConversionException("Invalid JSON. Cannot find \"" + member + "\" JSON member.");
    }
}

From source file:com.github.jramos.snowplow.operators.MobileContextExtractionOp.java

License:Apache License

@Override
public SnowplowEventModel apply(SnowplowEventModel event) {
    String context = event.getContexts();
    try {//from  w  w w  .  ja v a  2  s  .  c  o m
        String platform = event.getPlatform();
        if (platform.equals(SnowplowPlatform.Mobile.toString())) {

            // minimal tablet vs mobile detection
            if (isTablet(event.getUseragent())) {
                event.setDvce_type(SnowplowDeviceType.Tablet.toString());
                event.setDvce_ismobile(Boolean.FALSE);
            } else {
                event.setDvce_type(SnowplowDeviceType.Mobile.toString());
                event.setDvce_ismobile(Boolean.TRUE);
            }

            JsonObject rootObj = parser.parse(context).getAsJsonObject();
            if (rootObj.has(JSON_DATA)) {
                JsonArray dataArray = rootObj.getAsJsonArray(JSON_DATA);

                // find the correct object by matching on the schema
                JsonObject mobileContextObject = null;
                for (int i = 0; i < dataArray.size(); i++) {
                    JsonElement element = dataArray.get(i);
                    if (element.isJsonObject()) {
                        JsonObject jsonObject = element.getAsJsonObject();
                        if (jsonObject.has(JSON_SCHEMA)) {
                            String schema = jsonObject.getAsJsonPrimitive(JSON_SCHEMA).getAsString();
                            if (schema.contains(SCHEMA_TAG)) {
                                mobileContextObject = jsonObject;
                                break;
                            }
                        }
                    }
                }

                // parse out the mobile fields we want
                if (mobileContextObject != null && mobileContextObject.has(JSON_DATA)) {
                    JsonObject dataObject = mobileContextObject.getAsJsonObject(JSON_DATA);

                    if (dataObject.has(JSON_OPEN_IDFA)) {
                        // use as cross device user id - i.e network user id
                        String openIDFA = dataObject.getAsJsonPrimitive(JSON_OPEN_IDFA).getAsString();
                        event.setNetwork_userid(openIDFA);
                    }

                    String deviceManufacturer = dataObject.getAsJsonPrimitive(JSON_DEVICE_MANUFACTURER)
                            .getAsString();
                    event.setOs_manufacturer(deviceManufacturer);

                    String osType = dataObject.getAsJsonPrimitive(JSON_OS_TYPE).getAsString();
                    event.setOs_family(osType);

                    String osVersion = dataObject.getAsJsonPrimitive(JSON_OS_VERSION).getAsString();
                    String deviceModel = dataObject.getAsJsonPrimitive(JSON_DEVICE_MODEL).getAsString();

                    osNameBuffer.setLength(0);
                    osNameBuffer.append(osType).append(" ").append(osVersion);
                    if (deviceModel != null && deviceModel.trim().length() > 0) {
                        osNameBuffer.append(" (").append(deviceModel.trim()).append(")");
                    }
                    event.setOs_name(osNameBuffer.toString());
                }
            }
        }
    } catch (Exception e) {
        LOG.error("Exception applying operator to mobile context " + context, e);
    }
    return event;
}

From source file:com.github.jsdossier.DocWriter.java

License:Apache License

private static JsonArray getJsonArray(JsonObject object, String name) {
    if (!object.has(name)) {
        object.add(name, new JsonArray());
    }/*from  w  w w  .ja  va2 s  .com*/
    return object.get(name).getAsJsonArray();
}

From source file:com.github.jsdossier.TypeIndex.java

License:Apache License

private synchronized static JsonArray getJsonArray(JsonObject object, String name) {
    if (!object.has(name)) {
        object.add(name, new JsonArray());
    }//from   w w w.  j a v a2s .c o m
    return object.get(name).getAsJsonArray();
}

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

License:Apache License

private AlfrescoResponse fromHttpEntity(HttpEntity entity) throws IOException {
    Reader entityReader = new InputStreamReader(entity.getContent());
    JsonObject responseObject = gson.fromJson(entityReader, JsonObject.class);
    ArrayList<Map<String, Object>> documents = new ArrayList<Map<String, Object>>();

    long lastTransactionId = getStringAsLong(responseObject, LAST_TXN_ID, 0L);
    long lastAclChangesetId = getStringAsLong(responseObject, LAST_ACL_CS_ID, 0L);
    String storeId = getString(responseObject, STORE_ID);
    String storeProtocol = getString(responseObject, STORE_PROTOCOL);

    if (responseObject.has(DOCS) && responseObject.get(DOCS).isJsonArray()) {
        JsonArray docsArray = responseObject.get(DOCS).getAsJsonArray();
        for (JsonElement documentElement : docsArray) {
            Map<String, Object> document = createDocument(documentElement);
            document.put(STORE_ID, storeId);
            document.put(STORE_PROTOCOL, storeProtocol);
            documents.add(document);//from w  w w  . j  a v a2 s  . com
        }
    } else {
        logger.warn("No documents found in response!");
    }

    return new AlfrescoResponse(lastTransactionId, lastAclChangesetId, storeId, storeProtocol, documents);
}

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 {//  www.  j a  v a2s  .  com
            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.");
    }/*  www  .j  a v a2  s  .  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.");
    }//from w w w. ja  va  2 s .com
    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.messenger4j.profile.MessengerProfileClientImpl.java

License:Apache License

@Override
protected ProfileResponse responseFromJson(JsonObject responseJsonObject) {

    try {//ww  w.j a va2  s. c  o m
        if (responseJsonObject.has("data")) {
            return GreetingsProfileResponse.fromJson(responseJsonObject);
        } else {
            return ProfileResponse.fromJson(responseJsonObject);
        }
    } catch (MessengerIOException e) {
        log.error(e);
        return null;
    }
}

From source file:com.github.sommeri.sourcemap.SourceMapConsumerV3.java

License:Apache License

/**
 * Parses the given contents containing a source map.
 *///  w  w w.  ja  v a  2s.c  o m
public void parse(JsonObject sourceMapRoot, SourceMapSupplier sectionSupplier) throws SourceMapParseException {
    // Check basic assertions about the format.
    int version = sourceMapRoot.get("version").getAsInt();
    if (version != 3) {
        throw new SourceMapParseException("Unknown version: " + version);
    }

    this.file = sourceMapRoot.get("file").getAsString();
    if (file.isEmpty()) {
        //SMS: (source map separation):  commented this - I need more tolerant parser
        //throw new SourceMapParseException("File entry is missing or empty ");
    }
    if (sourceMapRoot.has("sourceRoot"))
        this.sourceRoot = sourceMapRoot.get("sourceRoot").getAsString();

    if (sourceMapRoot.has("sections")) {
        // Looks like a index map, try to parse it that way.
        parseMetaMap(sourceMapRoot, sectionSupplier);
        return;
    }

    lineCount = sourceMapRoot.get("lineCount").getAsInt();
    String lineMap = sourceMapRoot.get("mappings").getAsString();

    sources = getJavaStringArray(sourceMapRoot.get("sources").getAsJsonArray());
    if (sourceMapRoot.has("sourcesContent")) {
        sourcesContent = getJavaStringArray(sourceMapRoot.get("sourcesContent").getAsJsonArray());
    } else {
        sourcesContent = new String[sources.length];
    }
    names = getJavaStringArray(sourceMapRoot.get("names").getAsJsonArray());

    lines = new ArrayList<ArrayList<Entry>>(lineCount);

    new MappingBuilder(lineMap).build();
}