Example usage for android.util JsonReader beginArray

List of usage examples for android.util JsonReader beginArray

Introduction

In this page you can find the example usage for android.util JsonReader beginArray.

Prototype

public void beginArray() throws IOException 

Source Link

Document

Consumes the next token from the JSON stream and asserts that it is the beginning of a new array.

Usage

From source file:android.support.test.espresso.web.model.ModelCodec.java

private static List<Object> decodeArray(JsonReader reader) throws IOException {
    List<Object> array = Lists.newArrayList();
    reader.beginArray();
    while (reader.hasNext()) {
        switch (reader.peek()) {
        case BEGIN_OBJECT:
            array.add(decodeObject(reader));
            break;
        case NULL:
            reader.nextNull();/*from  w  ww  .java2 s  . co m*/
            array.add(null);
            break;
        case STRING:
            array.add(reader.nextString());
            break;
        case BOOLEAN:
            array.add(reader.nextBoolean());
            break;
        case BEGIN_ARRAY:
            array.add(decodeArray(reader));
            break;
        case NUMBER:
            array.add(decodeNumber(reader.nextString()));
            break;
        default:
            throw new IllegalStateException(String.format("%s: bogus token", reader.peek()));
        }
    }

    reader.endArray();
    return array;
}

From source file:com.fuzz.android.limelight.util.JSONTool.java

/**
 * @param reader//from   w ww. j a v a 2 s .  c om
 * @return list of BaseChapter objects and ChapterTransition objects from JSON
 * @throws IOException
 */
public static ArrayList<Chapter> readChapterArray(JsonReader reader) throws IOException {
    ArrayList<Chapter> chapters = new ArrayList<Chapter>();
    reader.beginArray();
    while (reader.hasNext()) {
        reader.beginObject();
        String name = reader.nextName();
        if (name.equals("type")) {
            String type = reader.nextString();

            if (type.equals("transition")) {
                chapters.add(readTransition(reader));
            } else if (type.equals("chapter")) {
                chapters.add(readChapter(reader));
            }
        }
    }
    reader.endArray();

    return chapters;
}

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./* w ww .  jav  a  2s.  c  om*/
 */
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.tcity.android.ui.info.BuildArtifactsTask.java

private void handleFiles(@NotNull JsonReader reader) throws IOException {
    reader.beginArray();

    List<BuildArtifact> result = new ArrayList<>();

    while (reader.hasNext()) {
        reader.beginObject();//  w w w  . j  a  v a  2 s  .  c  o  m

        long size = -1;
        String name = null;
        String contentHref = null;
        String childrenHref = null;

        while (reader.hasNext()) {
            switch (reader.nextName()) {
            case "size":
                size = reader.nextLong();
                break;
            case "name":
                name = reader.nextString();
                break;
            case "children":
                childrenHref = getHref(reader);
                break;
            case "content":
                contentHref = getHref(reader);
                break;
            default:
                reader.skipValue();
            }
        }

        if (name == null) {
            throw new IllegalStateException("Invalid artifacts json: \"name\" is absent");
        }

        if (contentHref == null && childrenHref == null) {
            throw new IllegalStateException("Invalid artifacts json: \"content\" and \"children\" are absent");
        }

        result.add(new BuildArtifact(size, name, contentHref, childrenHref));

        reader.endObject();
    }

    reader.endArray();

    myResult = result;
}

From source file:org.opensilk.music.ui2.loader.PluginLoader.java

public List<ComponentName> readDisabledPlugins() {
    List<ComponentName> list = new ArrayList<>();
    String json = settings.getString(PREF_DISABLED_PLUGINS, null);
    Timber.v("Read disabled plugins=" + json);
    if (json != null) {
        JsonReader jr = new JsonReader(new StringReader(json));
        try {/*from   w w w . j  a  v a 2s  .  c  om*/
            jr.beginArray();
            while (jr.hasNext()) {
                list.add(ComponentName.unflattenFromString(jr.nextString()));
            }
            jr.endArray();
        } catch (IOException e) {
            settings.remove(PREF_DISABLED_PLUGINS);
            list.clear();
        } finally {
            IOUtils.closeQuietly(jr);
        }
    }
    return list;
}

From source file:com.thingsee.tracker.REST.KiiBucketRequestAsyncTask.java

private JSONArray readSensorDataFromString(String input, int offset) {
    StringReader reader = new StringReader(input);
    try {//from   www .ja v  a2 s  . co m
        reader.skip(offset);
    } catch (IOException e1) {
        e1.printStackTrace();
    }
    JsonReader jsonReader = new JsonReader(reader);
    JSONArray jsonArray = new JSONArray();
    try {
        jsonReader.beginArray();
        while (jsonReader.hasNext()) {
            JSONObject jsonObject = readSingleData(jsonReader);
            jsonArray.put(jsonObject);
        }
        jsonReader.endArray();
    } catch (IOException e) {
        // Ignore for brevity
    } catch (JSONException e) {
        // Ignore for brevity
    }
    try {
        jsonReader.close();
    } catch (IOException e) {
        // Ignore for brevity
    }
    reader.close();
    return jsonArray;
}

From source file:com.example.propertylist.handler.JsonPropertyHandler.java

/**
 * Parses an array in the JSON data stream.
 *
 * @param reader//  ww w.  j ava 2s.c  o  m
 *            the {@link android.util.JsonReader} used to read in the data
 * @return
 *           the propertySalesPopulated
 * @throws java.io.IOException
 */

public List<Property> parseDataArray(JsonReader reader) throws IOException {
    List<Property> propertySalesPopulated = new ArrayList<Property>();
    Property allProperty;
    reader.beginArray();
    reader.peek();
    while (reader.hasNext()) {
        allProperty = parseSalesData(reader);
        if ((allProperty.getFullAddress() != null && allProperty.mFullAddress.length() > 0)
                && (allProperty.getThumbnailUrl() != null && allProperty.mThumbnailUrl.length() > 0)) {
            propertySalesPopulated.add(allProperty);
        }
        reader.peek();
    }
    reader.endArray();
    return propertySalesPopulated;
}

From source file:pedromendes.tempodeespera.HospitalDetailActivity.java

public List<Emergency> readResult(JsonReader reader) throws IOException {
    List<Emergency> result = new ArrayList<Emergency>();
    String name = reader.nextName();
    if (name.equals("Result")) {
        reader.beginArray();
        while (reader.hasNext()) {
            reader.beginObject();/*ww  w  . j a v a 2 s  .  c o  m*/
            Emergency hospitalEmergencyDetail = new Emergency();
            readEmergency(reader, hospitalEmergencyDetail);
            reader.endObject();
            result.add(hospitalEmergencyDetail);
        }
        reader.endArray();
    }
    return result;
}

From source file:at.ac.tuwien.caa.docscan.logic.DataLog.java

private ArrayList<ShotLog> readList(JsonReader reader) throws ParseException {

    ArrayList<ShotLog> shotLogs = new ArrayList<>();

    try {//  ww  w.  j  a  va  2  s. c om
        reader.beginArray();
        while (reader.hasNext()) {
            shotLogs.add(readShotLog(reader));
        }
        reader.endArray();

    } catch (IOException e) {
        e.printStackTrace();
    }

    return shotLogs;

}

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

/**
 * Parse an array that has only non-array 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  ww  w. j ava 2 s  .c  o  m*/
 * @param collection The Collection to populate. The parametrization should match {@code
 * typeClass}.
 * @param itemParser The parser to use for items of the array. May be null.
 * @param typeClass The type of items to expect in the array. May not be null, but may be
 * Object.class.
 * @param key The key corresponding to the current value. This is used to make more useful error
 * messages.
 */
private static <T> void parseFlatJsonArray(JsonReader reader, Collection<T> collection,
        JsonObjectParser<T> itemParser, Class<T> typeClass, String key) throws IOException {
    if (handleNull(reader)) {
        return;
    }

    Converter<T> converter = null;
    if (Converters.isConvertibleFromString(typeClass)) {
        converter = Converters.getConverter(typeClass);
    }

    final String discriminationName = ContextHolder.getContext().getSettings().getDiscriminationName();

    reader.beginArray();
    while (reader.hasNext()) {
        Object nextValue;
        final JsonToken nextToken = reader.peek();
        if (itemParser != null && nextToken == JsonToken.BEGIN_OBJECT) {
            reader.beginObject();
            nextValue = itemParser.parseJsonObject(null, reader, discriminationName, null);
            reader.endObject();
        } else if (converter != null && (nextToken == JsonToken.NUMBER || nextToken == JsonToken.STRING)) {
            nextValue = converter.convert(reader.nextString());
        } else {
            nextValue = parseNextValue(reader);
        }

        if (typeClass.isInstance(nextValue)) {
            // This is safe since we are calling class.isInstance()
            @SuppressWarnings("unchecked")
            T toAdd = (T) nextValue;
            collection.add(toAdd);
        } else {
            throw new IllegalStateException(
                    String.format(Locale.US, "Could not convert value in array at \"%s\" to %s from %s.", key,
                            typeClass.getCanonicalName(), getClassName(nextValue)));
        }
    }
    reader.endArray();
}