Example usage for javax.json JsonArray getBoolean

List of usage examples for javax.json JsonArray getBoolean

Introduction

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

Prototype

boolean getBoolean(int index);

Source Link

Document

Returns the boolean value at the specified position.

Usage

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  ww w  . j a  v  a  2s  .c om*/
 * 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;
}