Example usage for com.google.gson JsonArray get

List of usage examples for com.google.gson JsonArray get

Introduction

In this page you can find the example usage for com.google.gson JsonArray get.

Prototype

public JsonElement get(int i) 

Source Link

Document

Returns the ith element of the array.

Usage

From source file:com.github.autermann.matlab.json.MatlabValueSerializer.java

License:Open Source License

private MatlabArray parseMatlabArray(JsonElement value) {
    JsonArray array = value.getAsJsonArray();
    double[] values = new double[array.size()];
    for (int i = 0; i < array.size(); i++) {
        values[i] = array.get(i).getAsDouble();
    }//from   www  .j a v  a2 s .  c  o  m
    return new MatlabArray(values);
}

From source file:com.github.autermann.matlab.json.MatlabValueSerializer.java

License:Open Source License

private MatlabCell parseMatlabCell(JsonElement value) {
    JsonArray array = value.getAsJsonArray();
    MatlabValue[] cell = new MatlabValue[array.size()];
    for (int i = 0; i < array.size(); i++) {
        cell[i] = deserializeValue(array.get(i));
    }//from   ww  w  .ja  v  a  2 s.  co  m
    return new MatlabCell(cell);
}

From source file:com.github.cc007.headsweeper.controller.HeadSweeperController.java

License:Open Source License

public HeadSweeperController(JsonObject input, HeadSweeper plugin) {
    this.sweeperGames = new ArrayList<>();
    JsonArray sweeperGamesJSON = input.getAsJsonArray("sweeperGames");
    if (sweeperGamesJSON != null) {
        for (int i = 0; i < sweeperGamesJSON.size(); i++) {
            HeadSweeperGame sweeperGame = new HeadSweeperGame(sweeperGamesJSON.get(i).getAsJsonObject(),
                    plugin);//from w ww  . j a v a2s.  co  m
            sweeperGames.add(sweeperGame);
        }
    }
    this.plugin = plugin;
}

From source file:com.github.consiliens.harv.gson.IRequestLogRecordDeserializer.java

License:Open Source License

/**
 * Params name and value are stored as strings. Params are backed by a
 * HashMap so order is not guaranteed. Duplicate keys are not allowed in a
 * HashMap./*from w  w w  .  j a  v a2s .  c o  m*/
 **/
public static void setParamsFromJson(final HttpParams httpParams, final JsonObject obj) {
    final JsonArray requestHttpParamsJson = obj.getAsJsonArray(R.params);

    for (int a = 0; a < requestHttpParamsJson.size(); a++) {
        final JsonArray nvPair = requestHttpParamsJson.get(a).getAsJsonArray();

        httpParams.setParameter(nvPair.get(0).getAsString(), nvPair.get(1).getAsString());
    }
}

From source file:com.github.consiliens.harv.gson.IRequestLogRecordDeserializer.java

License:Open Source License

public Header[] getHeaders(final JsonArray allHeadersJsonArray) {
    final int headerCount = allHeadersJsonArray.size();
    final Header[] headerArray = new Header[headerCount];

    for (int a = 0; a < headerCount; a++) {
        final JsonArray nvPair = allHeadersJsonArray.get(a).getAsJsonArray();

        final String name = nvPair.get(0).getAsString();
        final String value = nvPair.get(1).getAsString();

        headerArray[a] = new BasicHeader(name, value);
    }// www.  j  a  va2s . c om

    return headerArray;
}

From source file:com.github.easyjsonapi.adapters.EasyJsonApiDeserializer.java

License:Apache License

/**
 * Deserializer when occur an success//from   w w w . j a  v a 2  s. c  o m
 * 
 * @param jsonElem
 *            the json element
 * @param jsonContext
 *            the json context
 * @return the json api object with values created
 */
private JsonApi deserializerData(JsonElement jsonElem, JsonDeserializationContext jsonContext) {

    JsonApi request = new JsonApi();

    // FIXME: Find solution for this validation
    if (this.tokenTypesToUse.containsKey(EasyJsonApiTypeToken.TOKEN_ATTR)
            || this.tokenTypesToUse.containsKey(EasyJsonApiTypeToken.TOKEN_META)
            || this.tokenTypesToUse.containsKey(EasyJsonApiTypeToken.TOKEN_DEFAULT)
            || this.tokenTypesToUse.containsKey(EasyJsonApiTypeToken.TOKEN_META_RELATIONSHIP)) {

        // Parse the attribute data
        JsonArray jsonArrayData = jsonElem.getAsJsonObject().get("data").getAsJsonArray();

        // Iterate the data list
        for (int index = 0; index < jsonArrayData.size(); index++) {

            JsonObject jsonData = jsonArrayData.get(index).getAsJsonObject();

            Relationships dataRels = Nullable.RELATIONSHIPS;
            Object dataAttr = Nullable.OBJECT;

            String dataId = JsonTools.getStringInsideJson("id", jsonData);
            String dataType = JsonTools.getStringInsideJson("type", jsonData);

            // Get the attributes json
            if (Assert.notNull(jsonData.get("attributes"))) {
                dataAttr = deserializerDataAttributes(jsonData, jsonContext);
            }

            // Get the relationship json
            if (Assert.notNull(jsonData.get("relationships"))) {
                dataRels = deserializerDataRelationship(jsonData, jsonContext);
            }

            Data jsonApiData = new Data(dataId, dataType, dataAttr, dataRels);
            request.addData(jsonApiData);
        }
    }

    return request;
}

From source file:com.github.easyjsonapi.adapters.EasyJsonApiDeserializer.java

License:Apache License

/**
 * Deserializer when occur an error//from  w w  w  .  j a  va 2 s  .c  o  m
 * 
 * @param jsonElem
 *            the json element
 * @param jsonContext
 *            the json context
 * @return the json api object with values created
 */
private JsonApi deserializerError(JsonElement jsonElem, JsonDeserializationContext jsonContext) {

    JsonApi request = new JsonApi();

    JsonArray jsonArrayErrors = jsonElem.getAsJsonObject().get("errors").getAsJsonArray();

    // Iterate the errors list
    for (int index = 0; index < jsonArrayErrors.size(); index++) {

        JsonObject jsonError = jsonArrayErrors.get(index).getAsJsonObject();

        String jsonApiErrorDetail = JsonTools.getStringInsideJson("detail", jsonError);
        String jsonApiErrorCode = JsonTools.getStringInsideJson("code", jsonError);
        String jsonApiErrorTitle = JsonTools.getStringInsideJson("title", jsonError);
        String jsonApiErrorId = JsonTools.getStringInsideJson("id", jsonError);

        Source jsonApiErrorSource = null;
        HttpStatus jsonApiErrorStatus = null;

        // Get the source json
        if (Assert.notNull(jsonError.get("source"))) {
            JsonObject jsonErrorSource = jsonError.get("source").getAsJsonObject();
            jsonApiErrorSource = jsonContext.deserialize(jsonErrorSource, Source.class);
        }

        // Get the http status json
        if (Assert.notNull(jsonError.get("status"))) {
            JsonObject jsonErrorStatus = jsonError.get("status").getAsJsonObject();
            jsonApiErrorStatus = HttpStatus.getStatus(Integer.valueOf(jsonErrorStatus.getAsString()));
        }

        Error jsonApiError = new Error(jsonApiErrorId, jsonApiErrorTitle, jsonApiErrorStatus, jsonApiErrorCode,
                jsonApiErrorDetail, Nullable.OBJECT, jsonApiErrorSource);

        request.addError(jsonApiError);
    }

    return request;
}

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

License:Apache License

@Override
public SnowplowEventModel apply(SnowplowEventModel event) {
    String context = event.getContexts();
    try {/*  w w  w .j  a  v a  2 s . co 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.messenger4j.profile.GreetingsProfileResponse.java

License:Apache License

public static GreetingsProfileResponse fromJson(JsonObject jsonObject) throws MessengerIOException {
    JsonArray dataArray = getArray(jsonObject, "data");
    if (isJsonArrayEmpty(dataArray)) {
        return new GreetingsProfileResponse(jsonObject.toString());
    }/*from w  ww . j av a  2s  .c  om*/

    JsonArray greetingsJsonArray = getArray(dataArray.get(0).getAsJsonObject(), "greeting");
    if (isJsonArrayEmpty(greetingsJsonArray)) {
        return new GreetingsProfileResponse(jsonObject.toString());
    }

    return new GreetingsProfileResponse(jsonObject.toString(),
            new Gson().fromJson(greetingsJsonArray, Greeting[].class));
}

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

License:Apache License

/**
 * @param sourceMapRoot//  ww  w .ja  v a 2 s. c o  m
 * @throws SourceMapParseException
 */
private void parseMetaMap(JsonObject sourceMapRoot, SourceMapSupplier sectionSupplier)
        throws SourceMapParseException {
    if (sectionSupplier == null) {
        sectionSupplier = new DefaultSourceMapSupplier();
    }

    try {
        // Check basic assertions about the format.
        int version = sourceMapRoot.get("version").getAsInt();
        if (version != 3) {
            throw new SourceMapParseException("Unknown version: " + version);
        }

        String file = sourceMapRoot.get("file").getAsString();
        if (file.isEmpty()) {
            throw new SourceMapParseException("File entry is missing or empty");
        }

        if (sourceMapRoot.has("lineCount") || sourceMapRoot.has("mappings") || sourceMapRoot.has("sources")
                || sourceMapRoot.has("names")) {
            throw new SourceMapParseException("Invalid map format");
        }

        SourceMapGeneratorV3 generator = new SourceMapGeneratorV3();
        JsonArray sections = sourceMapRoot.get("sections").getAsJsonArray();
        for (int i = 0, count = sections.size(); i < count; i++) {
            JsonObject section = sections.get(i).getAsJsonObject();
            if (section.has("map") && section.has("url")) {
                throw new SourceMapParseException(
                        "Invalid map format: section may not have both 'map' and 'url'");
            }
            JsonObject offset = section.get("offset").getAsJsonObject();
            int line = offset.get("line").getAsInt();
            int column = offset.get("column").getAsInt();
            String mapSectionContents;
            if (section.has("url")) {
                String url = section.get("url").getAsString();
                mapSectionContents = sectionSupplier.getSourceMap(url);
                if (mapSectionContents == null) {
                    throw new SourceMapParseException("Unable to retrieve: " + url);
                }
            } else if (section.has("map")) {
                mapSectionContents = section.get("map").getAsString();
            } else {
                throw new SourceMapParseException(
                        "Invalid map format: section must have either 'map' or 'url'");
            }
            generator.mergeMapSection(line, column, mapSectionContents);
        }

        StringBuilder sb = new StringBuilder();
        try {
            generator.appendTo(sb, file);
        } catch (IOException e) {
            // Can't happen.
            throw new RuntimeException(e);
        }

        parse(sb.toString());
    } catch (IOException ex) {
        throw new SourceMapParseException("IO exception: " + ex);
    }
}