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.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();
    for (int i = 0; i < size; i++) {
        JsonElement element = creditCardArray.get(i);
        String cardScheme = element.getAsString();

        if (creditCardSchemeSet == null) {
            creditCardSchemeSet = new HashSet<>();
        }/*  w w  w  .j  av a  2 s  . co  m*/

        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.XapiContextActivitiesJson.java

License:Open Source License

private JsonArray serialize(ArrayList<XapiActivity> theActivityArray,
        JsonSerializationContext theSerializationContext) {
    JsonArray theArray = new JsonArray();
    for (XapiActivity activity : theActivityArray) {
        theArray.add(theSerializationContext.serialize(activity, XapiActivity.class));
    }//from   ww  w. jav a 2 s.  c o m
    if (theArray.size() == 0) {
        return null;
    }
    return theArray;
}

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();
    }//from   w w  w  .j  a  v  a  2  s. c o 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.claresco.tinman.json.XapiPersonJson.java

License:Open Source License

@Override
public JsonElement serialize(XapiPerson arg0, Type arg1, JsonSerializationContext arg2) {
    JsonObject theResult = new JsonObject();

    theResult.addProperty("objectType", "Person");

    if (arg0.hasNames()) {
        JsonArray theNamesJson = JsonUtility.convertToJsonArray(arg0.getNames());

        if (theNamesJson.size() > 0) {
            theResult.add("name", theNamesJson);
        }/*from w w  w .  j  a va 2 s . c om*/
    }

    if (arg0.hasMboxes()) {
        JsonArray theMboxesJson = JsonUtility.convertToJsonArrayFromIRIList(arg0.getMboxes());

        if (theMboxesJson.size() > 0) {
            theResult.add("mbox", theMboxesJson);
        }

    }

    if (arg0.hasMboxSha1sums()) {
        JsonArray theMboxSha1sumsJson = JsonUtility.convertToJsonArray(arg0.getMboxSha1sums());

        if (theMboxSha1sumsJson.size() > 0) {
            theResult.add("mbox_sha1sum", theMboxSha1sumsJson);
        }
    }

    if (arg0.hasOpendIDs()) {
        JsonArray theOpenIDsJson = JsonUtility.convertToJsonArray(arg0.getOpenIDs());

        if (theOpenIDsJson.size() > 0) {
            theResult.add("openid", theOpenIDsJson);
        }
    }

    if (arg0.hasAccounts()) {
        ArrayList<XapiAccount> theAccounts = arg0.getAccounts();

        JsonArray theAccountsJson = new JsonArray();

        for (XapiAccount a : theAccounts) {
            theAccountsJson.add(arg2.serialize(a, XapiAccount.class));
        }

        if (theAccountsJson.size() > 0) {
            theResult.add("account", theAccountsJson);
        }
    }

    return theResult;
}

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  . java2  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 (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.api.DatabaseImpl.java

License:Open Source License

private JsonObject getFindByIndexBody(String selectorJson, FindByIndexOptions options) {

    JsonArray fieldsArray = new JsonArray();
    if (options.getFields().size() > 0) {
        for (String field : options.getFields()) {
            JsonPrimitive jsonField = client.getGson().fromJson(field, JsonPrimitive.class);
            fieldsArray.add(jsonField);//from w w w . j a v a  2s  .  c o  m
        }
    }

    JsonArray sortArray = new JsonArray();
    if (options.getSort().size() > 0) {

        for (IndexField sort : options.getSort()) {
            JsonObject sortObject = new JsonObject();
            sortObject.addProperty(sort.getName(), sort.getOrder().toString());
            sortArray.add(sortObject);
        }
    }

    JsonObject indexObject = new JsonObject();

    //parse and find if valid json issue #28
    JsonObject selectorObject = null;
    boolean isObject = true;
    try {
        selectorObject = getGson().fromJson(selectorJson, JsonObject.class);
    } catch (JsonParseException e) {
        isObject = false;
    }

    if (!isObject) {
        if (selectorJson.startsWith("\"selector\"")) {
            selectorJson = selectorJson.substring(selectorJson.indexOf(":") + 1, selectorJson.length()).trim();
            selectorObject = getGson().fromJson(selectorJson, JsonObject.class);
        } else {
            throw new JsonParseException("selectorJson should be valid json or like " + "\"selector\": {...} ");
        }
    }

    if (selectorObject.has("selector")) {
        indexObject.add("selector", selectorObject.get("selector"));
    } else {
        indexObject.add("selector", selectorObject);
    }

    if (fieldsArray.size() > 0) {
        indexObject.add("fields", fieldsArray);
    }
    if (sortArray.size() > 0) {
        indexObject.add("sort", sortArray);
    }
    if (options.getLimit() != null) {
        indexObject.addProperty("limit", options.getLimit());
    }
    if (options.getSkip() != null) {
        indexObject.addProperty("skip", options.getSkip());
    }
    if (options.getReadQuorum() != null) {
        indexObject.addProperty("r", options.getReadQuorum());
    }
    if (options.getUseIndex() != null) {
        indexObject.add("use_index", getGson().fromJson(options.getUseIndex(), JsonArray.class));
    }

    return indexObject;
}

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;/*  ww  w .  ja  v  a2  s  .co  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.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.comcast.magicwand.spells.appium.dawg.utils.AppiumServerController.java

License:Apache License

/**
 * Checks whether appium server has any running sessions
 * @param url URL of a the server/*  w w w .  j  a va  2s. c  o m*/
 * @return True if there are active sessions; False otherwise
 */
@SuppressWarnings("squid:S1155") // ignore 'Use isEmpty() to check whether the collection is empty or not.' error
public boolean hasRunningSessions(URL url) {
    URL appiumStatusUrl = null;
    try {
        appiumStatusUrl = new URL(url.getProtocol(), url.getHost(), url.getPort(), APPIUM_SESSIONS_ENDPOINT);
        JsonObject response = callAppiumEndpoint(appiumStatusUrl);
        JsonArray sessions = response.getAsJsonArray("value");

        return sessions.size() > 0;
    } catch (MalformedURLException e) {
        LOGGER.error(MALFORMED_URL_MSG + appiumStatusUrl, e);
        return false;
    }
}

From source file:com.commonslibrary.commons.utils.JsonUtils.java

License:Open Source License

/**
 * JSONArray??List?/*  www  .  jav  a2s.c  o m*/
 *
 * @param json
 * @return
 */
public static List<Object> toList(JsonArray json) {
    List<Object> list = new ArrayList<Object>();
    if (json == null) {
        return list;
    }
    for (int i = 0; i < json.size(); i++) {
        Object value = json.get(i);
        if (value instanceof JsonArray) {
            list.add(toList((JsonArray) value));
        } else if (value instanceof JsonObject) {
            list.add(toMap((JsonObject) value));
        } else {
            list.add(value);
        }
    }
    return list;
}