Example usage for android.util JsonToken BEGIN_ARRAY

List of usage examples for android.util JsonToken BEGIN_ARRAY

Introduction

In this page you can find the example usage for android.util JsonToken BEGIN_ARRAY.

Prototype

JsonToken BEGIN_ARRAY

To view the source code for android.util JsonToken BEGIN_ARRAY.

Click Source Link

Document

The opening of a JSON array.

Usage

From source file:com.workday.autoparse.json.parser.JsonParserUtils.java

/**
 * Parse the next value as an array, but do not attempt to convert it into a {@link Collection},
 * or to convert any children into known types. The returned object will be a {@link JSONArray}
 * and all children will be JSONObjects, JSONArrays, and primitives.
 *
 * @param reader The JsonReader to use. Calls to {@link JsonReader#beginArray()} and {@link
 * JsonReader#endArray()} will be taken care of by this method.
 * @param key The key corresponding to the current value. This is used to make more useful error
 * messages.//from   ww w .ja  v  a2s . c  o m
 */
public static JSONArray parseAsJsonArray(JsonReader reader, String key) throws IOException {
    if (handleNull(reader)) {
        return null;
    }
    assertType(reader, key, JsonToken.BEGIN_ARRAY);

    JSONArray jsonArray = new JSONArray();
    reader.beginArray();
    while (reader.hasNext()) {
        jsonArray.put(parseNextValue(reader, false));
    }
    reader.endArray();
    return jsonArray;
}

From source file:com.workday.autoparse.json.parser.JsonParserUtils.java

/**
 * Parse an array that may have arrays as children into a {@link Collection}.
 *
 * @param reader The reader to use, whose next token should either be {@link JsonToken#NULL} or
 * {@link JsonToken#BEGIN_ARRAY}.//from  w  ww.  ja v a2 s.c  om
 * @param collection The Collection to populate. If nested, the parametrization must match
 * {@code innerCollectionClasses} with the parametrization of the last collection matching
 * {@code itemType}. If not nested, the parametrization should match {@code itemType}.
 * @param itemParser The parser to use for the items of the most deeply nested Collections. May
 * be null.
 * @param itemType The type of the most deeply nested Collections. May not be null, but may be
 * Object.class.
 * @param innerCollectionClasses A flattened list of Collection classes that are nested within
 * {@code collection}. May be null.
 * @param key The key corresponding to the current value. This is used to make more useful error
 * messages.
 */
// Suppress rawtypes and unchecked on Collection and operations. We are depending on
// innerCollectionClasses to
// provide us with the types of all nested collections, and typeClass to be the parameter of
// the deepest
// collection. Assuming these are correct, all other operations are safe.
@SuppressWarnings({ "rawtypes", "unchecked" })
public static <T> void parseJsonArray(JsonReader reader, Collection collection, JsonObjectParser<T> itemParser,
        Class<T> itemType, List<Class<? extends Collection>> innerCollectionClasses, String key)
        throws IOException {
    if (handleNull(reader)) {
        return;
    }
    assertType(reader, key, JsonToken.BEGIN_ARRAY);

    if (innerCollectionClasses != null && !innerCollectionClasses.isEmpty()) {
        CollectionInitializer nextCollectionInitializer = CollectionInitializerFactory
                .getCollectionInitializerForClass(innerCollectionClasses.get(0));
        reader.beginArray();
        while (reader.hasNext()) {
            Collection nextCollection = nextCollectionInitializer.newInstance();
            parseJsonArray(reader, nextCollection, itemParser, itemType,
                    innerCollectionClasses.subList(1, innerCollectionClasses.size()), key);
            collection.add(nextCollection);
        }
        reader.endArray();
    } else {
        parseFlatJsonArray(reader, collection, itemParser, itemType, key);
    }

}