Example usage for javax.json JsonArray getInt

List of usage examples for javax.json JsonArray getInt

Introduction

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

Prototype

int getInt(int index);

Source Link

Document

A convenience method for getJsonNumber(index).intValue() .

Usage

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 w  w  w.j  a v  a  2s  . 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;
}

From source file:io.hops.hopsworks.common.util.WebCommunication.java

public List<NodesTableItem> getNdbinfoNodesTable(String hostAddress, String agentPassword)
        throws GenericException {
    List<NodesTableItem> resultList = new ArrayList<NodesTableItem>();

    String url = createUrl("mysql", hostAddress, "ndbinfo", "nodes");
    String jsonString = fetchContent(url, agentPassword);
    InputStream stream = new ByteArrayInputStream(jsonString.getBytes(StandardCharsets.UTF_8));
    try {/*from   w w  w.j  a  v  a 2 s.  c  o  m*/
        JsonArray json = Json.createReader(stream).readArray();
        if (json.get(0).equals("Error")) {
            resultList.add(new NodesTableItem(null, json.getString(1), null, null, null));
            return resultList;
        }
        for (int i = 0; i < json.size(); i++) {
            JsonArray node = json.getJsonArray(i);
            Integer nodeId = node.getInt(0);
            Long uptime = node.getJsonNumber(1).longValue();
            String status = node.getString(2);
            Integer startPhase = node.getInt(3);
            Integer configGeneration = node.getInt(4);
            resultList.add(new NodesTableItem(nodeId, status, uptime, startPhase, configGeneration));
        }
    } catch (Exception ex) {
        logger.log(Level.SEVERE, "Exception: {0}", ex);
        resultList.add(new NodesTableItem(null, "Error", null, null, null));
    }
    return resultList;
}