Example usage for com.google.gson JsonArray size

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

Introduction

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

Prototype

public int size() 

Source Link

Document

Returns the number of elements in the array.

Usage

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

License:Apache License

@Override
public T[] readNotNull(JsonElement element, DeserializationContext context) throws Exception {
    JsonArray array = element.getAsJsonArray();
    @SuppressWarnings("unchecked")
    T[] toReturn = (T[]) Array.newInstance(elementType, array.size());
    for (int i = 0, j = array.size(); i < j; i++) {
        context.pushPath("[" + i + "]");
        toReturn[i] = valueCodex.read(array.get(i), context);
        context.popPath();//from   w w  w  .j a va 2  s .  c  o m
    }
    return toReturn;
}

From source file:com.ghjansen.cas.ui.desktop.swing.SimulationParameterJsonAdapter.java

License:Open Source License

public final T deserialize(final JsonElement elem, final Type interfaceType,
        final JsonDeserializationContext context) throws JsonParseException {
    final JsonObject member = (JsonObject) elem;
    final JsonObject ruleTypeParameterJson = member.getAsJsonObject("ruleTypeParameter");
    final JsonObject ruleConfigurationParameterJson = member.getAsJsonObject("ruleConfigurationParameter");
    final JsonArray stateValuesJson = ruleConfigurationParameterJson.get("stateValues").getAsJsonArray();
    final JsonObject limitsParameterJson = member.getAsJsonObject("limitsParameter");
    final JsonObject initialConditionParameterJson = member.getAsJsonObject("initialConditionParameter");
    final JsonArray sequencesJson = initialConditionParameterJson.get("sequences").getAsJsonArray();

    UnidimensionalRuleTypeParameter ruleTypeParameter = new UnidimensionalRuleTypeParameter(
            ruleTypeParameterJson.get("elementar").getAsBoolean());
    int[] stateValues = new int[stateValuesJson.size()];
    for (int i = 0; i < stateValuesJson.size(); i++) {
        stateValues[i] = stateValuesJson.get(i).getAsInt();
    }/*from  w  w w  .j a  va2s  .  c o m*/
    UnidimensionalRuleConfigurationParameter ruleConfigurationParameter = new UnidimensionalRuleConfigurationParameter(
            stateValues[7], stateValues[6], stateValues[5], stateValues[4], stateValues[3], stateValues[2],
            stateValues[1], stateValues[0]);
    UnidimensionalLimitsParameter limitsParameter = new UnidimensionalLimitsParameter(
            limitsParameterJson.get("cells").getAsInt(), limitsParameterJson.get("iterations").getAsInt());
    UnidimensionalSequenceParameter sequences[] = new UnidimensionalSequenceParameter[sequencesJson.size()];
    for (int i = 0; i < sequencesJson.size(); i++) {
        JsonObject s = sequencesJson.get(i).getAsJsonObject();
        UnidimensionalSequenceParameter sequence = new UnidimensionalSequenceParameter(
                s.get("initialPosition").getAsInt(), s.get("finalPosition").getAsInt(),
                s.get("value").getAsInt());
        sequences[i] = sequence;
    }
    UnidimensionalInitialConditionParameter initialConditionParameter = new UnidimensionalInitialConditionParameter(
            sequences);
    try {
        return (T) new UnidimensionalSimulationParameter(ruleTypeParameter, ruleConfigurationParameter,
                limitsParameter, initialConditionParameter);
    } catch (InvalidSimulationParameterException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return null;
}

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

License:Open Source License

private MatlabMatrix parseMatlabMatrix(JsonElement value) {
    JsonArray array = value.getAsJsonArray();
    double[][] values = new double[array.size()][array.get(0).getAsJsonArray().size()];
    for (int i = 0; i < array.size(); i++) {
        JsonArray innerArray = array.get(i).getAsJsonArray();
        for (int j = 0; j < innerArray.size(); j++) {
            values[i][j] = innerArray.get(j).getAsDouble();
        }/*  w  w w  .ja v a2 s .c om*/
    }
    return new MatlabMatrix(values);
}

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();
    }// w w w .j  a  va 2 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 . jav a2  s.  c o  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 a  2  s.c  o 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 ava  2  s .co 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);
    }/*from www.  j  a v a 2  s . c  o  m*/

    return headerArray;
}

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

License:Apache License

/**
 * Deserializer when occur an success/*from  ww w.j  ava  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//w  ww . ja va 2  s  .  co  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;
}