Example usage for javax.json JsonArray size

List of usage examples for javax.json JsonArray size

Introduction

In this page you can find the example usage for javax.json JsonArray size.

Prototype

int size();

Source Link

Document

Returns the number of elements in this list.

Usage

From source file:at.porscheinformatik.sonarqube.licensecheck.maven.MavenDependencyScanner.java

private static void parseDependencyJson(Set<Dependency> dependencies, JsonArray jsonDependencyArray) {
    for (int i = 0; i < jsonDependencyArray.size(); i++) {
        JsonObject jsonDependency = jsonDependencyArray.getJsonObject(i);
        String scope = jsonDependency.getString("s");
        if ("compile".equals(scope) || "runtime".equals(scope)) {
            if (jsonDependency.containsKey("d")) {
                parseDependencyJson(dependencies, jsonDependency.getJsonArray("d"));
            }//w ww  .  j  ava  2 s  .  c  om

            Dependency dependency = new Dependency(jsonDependency.getString("k"), jsonDependency.getString("v"),
                    null);
            dependencies.add(dependency);
        }
    }
}

From source file:at.porscheinformatik.sonarqube.licensecheck.mavenlicense.MavenLicense.java

public static List<MavenLicense> fromString(String mavenLicenseString) {
    List<MavenLicense> mavenLicenses = new ArrayList<>();

    if (mavenLicenseString != null && mavenLicenseString.startsWith("[")) {
        try (JsonReader jsonReader = Json.createReader(new StringReader(mavenLicenseString))) {
            JsonArray licensesJson = jsonReader.readArray();
            for (int i = 0; i < licensesJson.size(); i++) {
                JsonObject licenseJson = licensesJson.getJsonObject(i);
                mavenLicenses.add(new MavenLicense(licenseJson.getString("licenseNameRegEx"),
                        licenseJson.getString("license")));
            }//  ww  w.  ja  v  a 2  s  . c  om
        }
    } else if (StringUtils.isNotEmpty(mavenLicenseString)) {
        // deprecated - remove with later release
        String[] mavenLicenseEntries = mavenLicenseString.split(";");
        for (String mavenLicenseEntry : mavenLicenseEntries) {
            String[] mavenLicenseEntryParts = mavenLicenseEntry.split("~");
            mavenLicenses.add(new MavenLicense(mavenLicenseEntryParts[0], mavenLicenseEntryParts[1]));
        }
    }

    return mavenLicenses;
}

From source file:org.json.StackExchangeAPI.java

private static void parseStackExchange(String jsonStr) {
    JsonReader reader = null;//from  w w w . java  2s  .  co  m
    try {
        reader = Json.createReader(new StringReader(jsonStr));
        JsonObject jsonObject = reader.readObject();
        reader.close();
        JsonArray array = jsonObject.getJsonArray("items");
        for (JsonObject result : array.getValuesAs(JsonObject.class)) {

            JsonObject ownerObject = result.getJsonObject("owner");
            // int ownerReputation = ownerObject.getInt("reputation");
            //  System.out.println("Reputation:"+ownerReputation);
            int viewCount = result.getInt("view_count");
            System.out.println("View Count :" + viewCount);
            int answerCount = result.getInt("answer_count");
            System.out.println("Answer Count :" + answerCount);
            String link = result.getString("link");
            System.out.println("URL: " + link);
            String title = result.getString("title");
            System.out.println("Title: " + title);
            String body = result.getString("body");
            System.out.println("Body: " + body);
            JsonArray tagsArray = result.getJsonArray("tags");
            StringBuilder tagBuilder = new StringBuilder();
            int i = 1;
            for (JsonValue tag : tagsArray) {
                tagBuilder.append(tag.toString());
                if (i < tagsArray.size())
                    tagBuilder.append(",");
                i++;
            }

            System.out.println("Tags: " + tagBuilder.toString());
            System.out.println("------------------------------------------");
        }

    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

From source file:com.buffalokiwi.aerodrome.jet.JetAPIResponse.java

/**
 * Check the response body for errors/*  w w w  .  j  a  va2  s.co  m*/
 * @param res JSON results
 * @throws JetException if there's an issue
 */
public static final void checkErrors(final JsonObject res, final IAPIResponse apiRes) throws JetException {
    if (res.containsKey("errors")) {
        final JsonArray errors = res.getJsonArray("errors");
        ArrayList<String> messages = new ArrayList<>();

        for (int i = 0; i < errors.size(); i++) {
            messages.add(errors.getString(i, ""));
        }

        throw new JetException(messages, null, apiRes);
    } else if (res.containsKey("error")) {
        throw new JetException(res.getString("error"), null, apiRes);
    }
}

From source file:com.buffalokiwi.aerodrome.jet.products.ProductVariationGroupRec.java

/**
 * Create a new JetProductVariationGroup instance based on Jet JSON.
 * @param sku The parent sku that was queried 
 * @param json JSON response from jet product variation query.
 * @return object/*from w  ww  .j a  va  2s. c  o  m*/
 * @throws IllegalArgumentException if sku or json are null/empty
 * @throws ClassCastException if Any array items are of an incorrect type
 */
public static ProductVariationGroupRec fromJSON(final String sku, final JsonObject json)
        throws IllegalArgumentException, ClassCastException {
    Utils.checkNullEmpty(sku, "sku");
    Utils.checkNull(json, "json");

    final List<String> childSkus = new ArrayList<>();
    final JsonArray skus = json.getJsonArray("children_skus");
    if (skus != null) {
        for (int i = 0; i < skus.size(); i++) {
            final JsonObject entry = skus.getJsonObject(i);
            if (entry != null) {
                final String child = entry.getString("merchant_sku", "");
                if (!child.isEmpty())
                    childSkus.add(child);
            }
        }
    }

    return new ProductVariationGroupRec(sku, Relationship.byText(json.getString("relationship", "")),
            Utils.jsonArrayToLongList(json.getJsonArray("variation_refinements")), childSkus,
            json.getString("group_title", ""));
}

From source file:com.buffalokiwi.aerodrome.jet.Utils.java

/**
 * Convert a json array to a list of integers.
 * if arr is null, then an empty List<Integer> instance is returned.
 * /*  ww  w . j ava2  s  . com*/
 * This is more safe than JsonArray.getValuesAs() as this method
 * has built-in type checking and will throw a ClassCastException if 
 * the type is incorrect or null.
 * 
 * @param arr array
 * @return a list
 * @throws ClassCastException if any element in arr is not an integer
 */
public static List<Long> jsonArrayToLongList(final JsonArray arr) {
    final List<Long> out = new ArrayList<>();
    if (arr == null)
        return out;

    for (int i = 0; i < arr.size(); i++) {
        final long j = Utils.getJsonNumber(arr.getJsonNumber(i)).longValue();
        out.add(j);
    }

    return out;
}

From source file:com.buffalokiwi.aerodrome.jet.Utils.java

/**
 * Convert a json array to a list of booleans.
 * if arr is null, then an empty List<Boolean> instance is returned.
 * /*from  w  w w.jav a  2 s.  c o  m*/
 * This is more safe than JsonArray.getValuesAs() as this method
 * has built-in type checking and will throw a ClassCastException if 
 * the type is incorrect or null.
 * 
 * @param arr array
 * @return a list
 * @throws ClassCastException if any element in arr is not an boolean
 */
public static List<Boolean> jsonArrayToBooleanList(final JsonArray arr) {
    final List<Boolean> out = new ArrayList<>();
    if (arr == null)
        return out;

    for (int i = 0; i < arr.size(); i++) {
        final Boolean b = arr.getBoolean(i);
        if (b == null) {
            throw new ClassCastException(
                    "Element at position " + String.valueOf(i) + " is null - Boolean required");
        }

        out.add(b);

        out.add(arr.getBoolean(i));
    }

    return out;
}

From source file:com.buffalokiwi.aerodrome.jet.Utils.java

/**
 * Convert a json array to a list of JsonObject instances.
 * if arr is null, then an empty List<JsonObject> instance is returned.
 * /*  w w  w. ja v  a  2  s .com*/
 * This is more safe than JsonArray.getValuesAs() as this method
 * has built-in type checking and will throw a ClassCastException if 
 * the type is incorrect or null.
 * 
 * @param arr array
 * @return a list
 * @throws ClassCastException if any element in arr is not convertable to JsonObject
 */
public static List<JsonObject> jsonArrayToJsonObjectList(final JsonArray arr) {
    final List<JsonObject> out = new ArrayList<>();
    if (arr == null)
        return out;

    for (int i = 0; i < arr.size(); i++) {
        final JsonObject o = arr.getJsonObject(i);
        if (o == null) {
            throw new ClassCastException(
                    "Element at position " + String.valueOf(i) + " is null - JsonObject required");
        }

        out.add(o);
    }

    return out;
}

From source file:com.buffalokiwi.aerodrome.jet.Utils.java

/**
 * Convert a json array to a list of Strings.
 * if arr is null, then an empty List<String> instance is returned.
 * /*from  w  w  w .  j  a  v  a2  s  .co m*/
 * This is more safe than JsonArray.getValuesAs() as this method
 * has built-in type checking and will throw a ClassCastException if 
 * the type is incorrect or null.
 * 
 * @param arr array
 * @return a list
 * @throws ClassCastException if any element in arr is not an string
 */
public static List<String> jsonArrayToStringList(final JsonArray arr) {
    final List<String> out = new ArrayList<>();
    if (arr == null)
        return out;

    for (int i = 0; i < arr.size(); i++) {
        final String s;
        try {
            s = arr.getString(i);
        } catch (ClassCastException e) {
            APILog.error(LOG, e, arr.get(i));
            continue;
        }

        if (s == null) {
            throw new ClassCastException(
                    "Element at position " + String.valueOf(i) + " is null - String required");
        }

        out.add(s);
    }

    return out;
}

From source file:com.buffalokiwi.aerodrome.jet.Utils.java

/**
 * Convert a json array to a list of integers.
 * if arr is null, then an empty List<Integer> instance is returned.
 * /*from ww  w .  jav  a  2  s  .co  m*/
 * This is more safe than JsonArray.getValuesAs() as this method
 * has built-in type checking and will throw a ClassCastException if 
 * the type is incorrect or null.
 * 
 * @param arr array
 * @return a list
 * @throws ClassCastException if any element in arr is not an integer
 */
public static List<Integer> jsonArrayToIntList(final JsonArray arr) {
    final List<Integer> out = new ArrayList<>();
    if (arr == null)
        return out;

    for (int i = 0; i < arr.size(); i++) {
        final Integer j;
        try {
            j = arr.getInt(i);
        } catch (ClassCastException e) {
            APILog.error(LOG, e, arr.get(i));
            continue;
        }

        if (j == null) {
            throw new ClassCastException(
                    "Element at position " + String.valueOf(i) + " is null - Integer required");
        }

        out.add(j);
    }

    return out;
}