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.canoo.cog.sonar.SonarResultParser.java

License:Apache License

CityModelImpl parseCity(String cityResultString) {

    JsonElement cityJson = jsonParser.parse(cityResultString);
    JsonArray allComponents = cityJson.getAsJsonArray();

    // Create city
    JsonObject cityObject = allComponents.get(0).getAsJsonObject();
    JsonToModelConverter jsonToModelConverter = new JsonToModelConverter();
    CityModelImpl city = jsonToModelConverter.convertToCity(cityObject);

    // Sort by name
    List<JsonObjectWrapper> javaElements = new ArrayList<>();
    for (JsonElement component : allComponents) {
        javaElements.add(new JsonObjectWrapper(component.getAsJsonObject()));
    }/*from w w  w. ja  v  a 2 s.c  om*/
    Collections.sort(javaElements);

    // Add neighborhoods
    for (JsonObjectWrapper component : javaElements) {
        JsonObject compObject = component.getObject();
        String scopeString = compObject.get("scope").getAsString();
        if (scopeString.equals("DIR")) {
            HoodModelImpl newHood = jsonToModelConverter.convertToHood(compObject);
            city.addHoodToCity(newHood);
        }
    }

    // Add buildings
    for (JsonObjectWrapper component : javaElements) {
        JsonObject compObject = component.getObject();
        String scopeString = compObject.get("scope").getAsString();
        String qualifierString = compObject.get("qualifier").getAsString();
        if (scopeString.equals("FIL") && !qualifierString.equals("UTS")) {
            BuildingModelImpl buildingModel = jsonToModelConverter.convertToBuilding(compObject);
            city.addBuildingToCity(buildingModel);
        }
    }

    // drop empty hoods
    city.removeEmptyHoods();

    return city;
}

From source file:com.ccreanga.bitbucket.rest.client.http.responseparsers.RepositoryParser.java

License:Apache License

@Override
public Repository apply(JsonElement jsonElement) {
    if (jsonElement == null || !jsonElement.isJsonObject()) {
        return null;
    }//w ww  .  ja va 2 s  .co  m

    JsonObject json = jsonElement.getAsJsonObject();

    String sshCloneUrl = null;
    String httpCloneUrl = null;
    String selfUrl = null;

    if (json.has("links")) {
        JsonObject links = json.getAsJsonObject("links");
        selfUrl = linkParser("url", "text").apply(links.get("self").getAsJsonArray().get(0)).getHref();

        if (links.has("clone")) {
            JsonArray arrayCloneLinks = links.get("clone").getAsJsonArray();
            Link first = linkParser().apply(arrayCloneLinks.get(0));
            Link second = linkParser().apply(arrayCloneLinks.get(1));
            if (first.getName().equals("http")) {
                httpCloneUrl = first.getHref();
                sshCloneUrl = second.getHref();
            } else {
                sshCloneUrl = first.getHref();
                httpCloneUrl = second.getHref();
            }
        }
    }

    return new Repository(optionalJsonString(json, "hierarchyId"), json.get("id").getAsLong(),
            json.get("slug").getAsString(), json.get("name").getAsString(), optionalJsonBoolean(json, "public"),
            optionalJsonBoolean(json, "fork"), optionalJsonBoolean(json, "forkable"), sshCloneUrl, httpCloneUrl,
            selfUrl, projectParser().apply(json.getAsJsonObject("project")),
            json.has("origin") ? repositoryParser().apply(json.getAsJsonObject("origin")) : null,
            json.get("scmId").getAsString(), RepositoryState.valueOf(json.get("state").getAsString()),
            json.get("statusMessage").getAsString());

}

From source file:com.cinchapi.concourse.server.ConcourseServer.java

License:Apache License

/**
 * Do the work to jsonify (dump to json string) each of the {@code records},
 * possibly at {@code timestamp} (if it is greater than 0) using the
 * {@code store}./*from   w w  w  .  ja  v  a  2 s . co  m*/
 * 
 * @param records
 * @param timestamp
 * @param identifier - will include the primary key for each record in the
 *            dump, if set to {@code true}
 * @param store
 * @return the json string dump
 */
private static String jsonify0(List<Long> records, long timestamp, boolean identifier, Store store) {
    JsonArray array = new JsonArray();
    for (long record : records) {
        Map<String, Set<TObject>> data = timestamp == 0 ? store.select(record)
                : store.select(record, timestamp);
        JsonElement object = DataServices.gson().toJsonTree(data);
        if (identifier) {
            object.getAsJsonObject().addProperty(GlobalState.JSON_RESERVED_IDENTIFIER_NAME, record);
        }
        array.add(object);
    }
    return array.size() == 1 ? array.get(0).toString() : array.toString();
}

From source file:com.citrus.sdk.payment.MerchantPaymentOption.java

License:Apache License

public static MerchantPaymentOption getMerchantPaymentOptions(JsonObject merchantPaymentOptionsObj,
        Map<String, PGHealth> pgHealthMap) {
    MerchantPaymentOption merchantPaymentOption;
    Set<CardScheme> debitCardSchemeSet = null;
    Set<CardScheme> creditCardSchemeSet = null;
    ArrayList<NetbankingOption> netbankingOptionList = null;

    JsonArray bankArray = merchantPaymentOptionsObj.getAsJsonArray("netBanking");
    JsonArray creditCardArray = merchantPaymentOptionsObj.getAsJsonArray("creditCard");
    JsonArray debitCardArray = merchantPaymentOptionsObj.getAsJsonArray("debitCard");

    int size = -1;
    // Parse credit card scheme
    size = creditCardArray.size();//from  ww w .jav  a  2 s . co m
    for (int i = 0; i < size; i++) {
        JsonElement element = creditCardArray.get(i);
        String cardScheme = element.getAsString();

        if (creditCardSchemeSet == null) {
            creditCardSchemeSet = new HashSet<>();
        }

        creditCardSchemeSet.add(getCardScheme(cardScheme));
    }

    // Parse debit card scheme
    size = debitCardArray.size();
    for (int i = 0; i < size; i++) {
        JsonElement element = debitCardArray.get(i);
        String cardScheme = element.getAsString();

        if (debitCardSchemeSet == null) {
            debitCardSchemeSet = new HashSet<>();
        }

        debitCardSchemeSet.add(getCardScheme(cardScheme));
    }

    // Parse netbanking options
    size = bankArray.size();
    for (int i = 0; i < size; i++) {
        JsonElement element = bankArray.get(i);
        if (element.isJsonObject()) {
            JsonObject bankOption = element.getAsJsonObject();

            element = bankOption.get("bankName");
            String bankName = element.getAsString();

            element = bankOption.get("issuerCode");
            String issuerCode = element.getAsString();

            if (!TextUtils.isEmpty(bankName) && !TextUtils.isEmpty(issuerCode)) {
                NetbankingOption netbankingOption = new NetbankingOption(bankName, issuerCode);

                if (pgHealthMap != null) {
                    netbankingOption.setPgHealth(pgHealthMap.get(issuerCode));
                }

                if (netbankingOptionList == null) {
                    netbankingOptionList = new ArrayList<>();
                }

                netbankingOptionList.add(netbankingOption);
            }
        }
    }

    merchantPaymentOption = new MerchantPaymentOption(creditCardSchemeSet, debitCardSchemeSet,
            netbankingOptionList);

    return merchantPaymentOption;
}

From source file:com.claresco.tinman.json.XapiGroupJson.java

License:Open Source License

@Override
public XapiGroup deserialize(JsonElement arg0, Type arg1, JsonDeserializationContext arg2)
        throws JsonParseException {
    JsonObject theJsonGroup = JsonUtility.convertJsonElementToJsonObject(arg0);

    String theName = JsonUtility.getElementAsString(theJsonGroup, "name");

    JsonArray theJsonMember = null;
    // Retrieve member as JsonArray
    if (theJsonGroup.has("member")) {
        theJsonMember = theJsonGroup.get("member").getAsJsonArray();
    }/* w ww.  ja  v  a  2  s.co  m*/

    XapiAgent[] theMember = null;

    // Initialize an array of agents
    if (theJsonMember != null) {
        theMember = new XapiAgent[theJsonMember.size()];

        // Iterate through the JsonArray
        for (int i = 0; i < theMember.length; i++) {
            theMember[i] = JsonUtility.delegateDeserialization(arg2, theJsonMember.get(i), XapiAgent.class);
        }
    }

    XapiInverseFunctionalIdentifier theId = JsonUtility.delegateDeserialization(arg2, arg0,
            XapiInverseFunctionalIdentifier.class);

    XapiGroup theGroup = new XapiGroup(theName, theMember, theId);

    if (!theGroup.isValid()) {
        throw new XapiBadGroupException("The group is not valid");
    }

    return theGroup;
}

From source file:com.cloudant.client.api.Database.java

License:Open Source License

public List<Index> deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {

    final List<Index> indices = new ArrayList<Index>();

    final JsonObject jsonObject = json.getAsJsonObject();
    JsonArray indArray = jsonObject.get("indexes").getAsJsonArray();
    for (int i = 0; i < indArray.size(); i++) {
        JsonObject ind = indArray.get(i).getAsJsonObject();
        String ddoc = null;//from w w  w  .j a  va2 s  .  c om
        if (!ind.get("ddoc").isJsonNull()) { // ddoc is optional
            ddoc = ind.get("ddoc").getAsString();
        }
        Index idx = new Index(ddoc, ind.get("name").getAsString(), ind.get("type").getAsString());
        JsonArray fldArray = ind.get("def").getAsJsonObject().get("fields").getAsJsonArray();
        for (int j = 0; j < fldArray.size(); j++) {
            Set<Map.Entry<String, JsonElement>> fld = fldArray.get(j).getAsJsonObject().entrySet();
            for (Entry<String, JsonElement> entry : fld) {
                idx.addIndexField(entry.getKey(), SortOrder.valueOf(entry.getValue().getAsString()));
            }
        } //end fldArray
        indices.add(idx);

    } // end indexes

    return indices;
}

From source file:com.cloudant.client.internal.util.IndexDeserializer.java

License:Open Source License

public List<Index> deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {

    final List<Index> indices = new ArrayList<Index>();

    final JsonObject jsonObject = json.getAsJsonObject();
    JsonArray indArray = jsonObject.get("indexes").getAsJsonArray();
    for (int i = 0; i < indArray.size(); i++) {
        JsonObject ind = indArray.get(i).getAsJsonObject();
        String ddoc = null;/* w  ww .  j  av  a  2 s  . c o m*/
        if (!ind.get("ddoc").isJsonNull()) { // ddoc is optional
            ddoc = ind.get("ddoc").getAsString();
        }
        Index idx = new Index(ddoc, ind.get("name").getAsString(), ind.get("type").getAsString());
        JsonArray fldArray = ind.get("def").getAsJsonObject().get("fields").getAsJsonArray();
        for (int j = 0; j < fldArray.size(); j++) {
            Set<Map.Entry<String, JsonElement>> fld = fldArray.get(j).getAsJsonObject().entrySet();
            for (Map.Entry<String, JsonElement> entry : fld) {
                idx.addIndexField(entry.getKey(), IndexField.SortOrder.valueOf(entry.getValue().getAsString()));
            }
        } //end fldArray
        indices.add(idx);

    } // end indexes

    return indices;
}

From source file:com.cloudant.client.internal.views.ViewMultipleRequester.java

License:Open Source License

public List<ViewResponse<K, V>> getViewResponses() throws IOException {
    //build the queries array of data to POST
    JsonArray queries = new JsonArray();
    ViewQueryParameters<K, V> viewQueryParameters = null;
    for (ViewQueryParameters<K, V> params : requestParameters) {
        if (viewQueryParameters == null) {
            viewQueryParameters = params;
        }/*from   w ww. ja  va 2  s .  co m*/
        queries.add(params.asJson());
    }
    JsonObject queryJson = new JsonObject();
    queryJson.add("queries", queries);
    //construct and execute the POST request
    HttpPost post = new HttpPost(viewQueryParameters.getViewURIBuilder().buildEncoded());
    StringEntity entity = new StringEntity(queryJson.toString(), "UTF-8");
    entity.setContentType("application/json");
    post.setEntity(entity);
    JsonObject jsonResponse = ViewRequester.executeRequestWithResponseAsJson(viewQueryParameters, post);
    //loop the returned array creating the ViewResponse objects
    List<ViewResponse<K, V>> responses = new ArrayList<ViewResponse<K, V>>();
    JsonArray jsonResponses = jsonResponse.getAsJsonArray("results");
    if (jsonResponses != null) {
        int index = 0;
        for (ViewQueryParameters<K, V> params : requestParameters) {
            JsonObject response = jsonResponses.get(index).getAsJsonObject();
            responses.add(new ViewResponseImpl<K, V>(params, response));
            index++;
        }
        return responses;
    } else {
        return Collections.emptyList();
    }
}

From source file:com.cloudbase.CBNaturalDeserializer.java

License:Open Source License

private Object handleArray(JsonArray json, JsonDeserializationContext context) {
    Object[] array = new Object[json.size()];
    for (int i = 0; i < array.length; i++)
        array[i] = context.deserialize(json.get(i), Object.class);
    return array;
}

From source file:com.codequicker.quick.templates.data.JsonLookupHandler.java

License:Apache License

@SuppressWarnings("rawtypes")
public Object lookupFromList(Object context, String key, int index) {
    Class clazz = context.getClass();

    if (JsonArray.class.isAssignableFrom(clazz)) {
        JsonArray jsonArray = ((JsonArray) context).getAsJsonArray();

        return jsonArray.get(index);
    }/*from w w  w . ja va2  s . co m*/

    return getNextHandler(context, position).lookupFromList(context, key, index);
}