Example usage for android.util JsonReader peek

List of usage examples for android.util JsonReader peek

Introduction

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

Prototype

public JsonToken peek() throws IOException 

Source Link

Document

Returns the type of the next token without consuming it.

Usage

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

/**
 * Populates property data with the JSON data.
 *
 * @param reader/*from   www .  j  av  a2s .c o  m*/
 *            the {@link android.util.JsonReader} used to read in the data
 * @return
 *              the propertyResults
 * @throws org.json.JSONException
 * @throws java.io.IOException
 */
public List<Property> populateResultsSales(JsonReader reader) throws JSONException, IOException {
    List<Property> propertyResults = new ArrayList<Property>();

    reader.beginObject();
    while (reader.hasNext()) {
        final String name = reader.nextName();
        final boolean isNull = reader.peek() == JsonToken.NULL;
        if (name.equals("results") && !isNull) {
            propertyResults = populateAdsSales(reader);
        } else {
            reader.skipValue();
        }
    }
    reader.endObject();
    return propertyResults;
}

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

/**
 * Populates property data with the JSON data.
 *
 * @param reader/*w w  w.  ja va 2s. c  om*/
 *             the {@link android.util.JsonReader} used to read in the data
 * @return
 *          the propertyResults
 * @throws org.json.JSONException
 * @throws java.io.IOException
 */
public List<Property> populatePropertySales(JsonReader reader) throws JSONException, IOException {
    //   Property property = new Property();
    List<Property> propertyResults = new ArrayList<Property>();

    reader.beginObject();
    while (reader.hasNext()) {
        final String name = reader.nextName();
        final boolean isNull = reader.peek() == JsonToken.NULL;
        if (name.equals("result") && !isNull) {
            propertyResults = populateResultsSales(reader);
        } else {
            reader.skipValue();
        }
    }
    reader.endObject();
    return propertyResults;
}

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  a v a 2  s. c  om
 * @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();
}

From source file:com.dalaran.async.task.http.AbstractHTTPService.java

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
protected List<ContentValues> parseJson(JsonReader reader) throws IOException {

    List<ContentValues> contentValueses = new ArrayList<ContentValues>();
    ContentValues values = new ContentValues();
    Long threadId = 0L;//from   w  ww. ja  va  2s . c  o  m
    boolean notEnd = true;
    String name = "";
    if (reader.hasNext()) { //todo android.util.MalformedJsonException: Use JsonReader.setLenient(true)
        do {
            switch (reader.peek()) {
            case BEGIN_OBJECT:
                values = new ContentValues();
                if (threadId != 0) {
                    values.put("threadId", threadId);
                }
                reader.beginObject();
                break;
            case BEGIN_ARRAY:
                if (values != null && values.getAsLong("threadId") != null) {
                    threadId = values.getAsLong("threadId");
                }
                reader.beginArray();
                break;
            case BOOLEAN:
                values.put(name, reader.nextBoolean());
                break;
            case END_ARRAY:
                reader.endArray();
                break;
            case END_DOCUMENT:
                notEnd = false;
                break;
            case END_OBJECT:
                contentValueses.add(values);
                reader.endObject();
                break;
            case NAME:
                name = reader.nextName();
                break;
            case NULL:
                reader.nextNull();
                break;
            case NUMBER:
                values.put(name, reader.nextDouble());
                break;
            case STRING:
                values.put(name, reader.nextString());
                break;
            default:
                reader.skipValue();
            }
        } while (notEnd);
    }
    return contentValueses;
}

From source file:pedromendes.tempodeespera.HospitalDetailActivity.java

private void readEmergency(JsonReader reader, Emergency hospitalEmergencyDetail) throws IOException {
    while (reader.hasNext()) {
        String fieldDame = reader.nextName();
        if (fieldDame.equals("Emergency")) {
            reader.beginObject();//from   ww  w .  j ava 2 s  .com
            reader.nextName();
            reader.nextString();
            reader.nextName();
            hospitalEmergencyDetail.setDescription(reader.nextString());
            reader.endObject();
        } else if (fieldDame.equals("Queue") && reader.peek() != JsonToken.NULL) {
            reader.beginObject();
            reader.nextName();
            reader.nextString();
            reader.nextName();
            hospitalEmergencyDetail.setName(reader.nextString());
            reader.endObject();
        } else if (fieldDame.equals("Red")) {
            reader.beginObject();
            fillQueue(reader, hospitalEmergencyDetail.getRedQueue());
            reader.endObject();
        } else if (fieldDame.equals("Orange")) {
            reader.beginObject();
            fillQueue(reader, hospitalEmergencyDetail.getOrangeQueue());
            reader.endObject();
        } else if (fieldDame.equals("Yellow")) {
            reader.beginObject();
            fillQueue(reader, hospitalEmergencyDetail.getYellowQueue());
            reader.endObject();
        } else if (fieldDame.equals("Green")) {
            reader.beginObject();
            fillQueue(reader, hospitalEmergencyDetail.getGreenQueue());
            reader.endObject();
        } else if (fieldDame.equals("Blue")) {
            reader.beginObject();
            fillQueue(reader, hospitalEmergencyDetail.getBlueQueue());
            reader.endObject();
        } else if (fieldDame.equals("LastUpdate")) {
            hospitalEmergencyDetail.setLastUpdate(reader.nextString());
        } else {
            reader.skipValue();
        }
    }
}

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

private JSONObject readSingleData(JsonReader jsonReader) throws IOException, JSONException {
    JSONObject jsonObject = new JSONObject();
    jsonReader.beginObject();/*w  ww.j a  v  a  2 s.com*/
    JsonToken token;
    do {
        String name = jsonReader.nextName();
        if ("sId".equals(name)) {
            jsonObject.put("sId", jsonReader.nextString());
        } else if ("val".equals(name)) {
            jsonObject.put("val", jsonReader.nextDouble());
        } else if ("ts".equals(name)) {
            jsonObject.put("ts", jsonReader.nextLong());
        } else if ("_owner".equals(name)) {
            jsonObject.put("_owner", jsonReader.nextString());
        }

        token = jsonReader.peek();
    } while (token != null && !token.equals(JsonToken.END_OBJECT));
    jsonReader.endObject();
    return jsonObject;
}