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:android.support.test.espresso.web.model.ModelCodec.java

private static Object decodeViaJSONReader(String json) throws IOException {
    JsonReader reader = null;
    try {//from   w w w .  j av a 2 s  .  c  o m
        reader = new JsonReader(new StringReader(json));
        while (true) {
            switch (reader.peek()) {
            case BEGIN_OBJECT:
                return decodeObject(reader);
            case BEGIN_ARRAY:
                return decodeArray(reader);
            default:
                throw new IllegalStateException("Bogus document: " + json);
            }
        }
    } finally {
        if (null != reader) {
            try {
                reader.close();
            } catch (IOException ioe) {
                Log.i(TAG, "json reader - close exception", ioe);
            }
        }
    }
}

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();//ww w .j av a 2 s .c o m
    while (reader.hasNext()) {
        switch (reader.peek()) {
        case BEGIN_OBJECT:
            array.add(decodeObject(reader));
            break;
        case NULL:
            reader.nextNull();
            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:android.support.test.espresso.web.model.ModelCodec.java

private static Object decodeObject(JsonReader reader) throws IOException {
    Map<String, Object> obj = Maps.newHashMap();
    List<String> nullKeys = Lists.newArrayList();
    reader.beginObject();// w ww.  j  a  v  a2s .  co m
    while (reader.hasNext()) {
        String key = reader.nextName();
        Object value = null;
        switch (reader.peek()) {
        case BEGIN_OBJECT:
            obj.put(key, decodeObject(reader));
            break;
        case NULL:
            reader.nextNull();
            nullKeys.add(key);
            obj.put(key, JSONObject.NULL);
            break;
        case STRING:
            obj.put(key, reader.nextString());
            break;
        case BOOLEAN:
            obj.put(key, reader.nextBoolean());
            break;
        case NUMBER:
            obj.put(key, decodeNumber(reader.nextString()));
            break;
        case BEGIN_ARRAY:
            obj.put(key, decodeArray(reader));
            break;
        default:
            throw new IllegalStateException(String.format("%: bogus token.", reader.peek()));
        }
    }
    reader.endObject();
    Object replacement = maybeReplaceMap(obj);
    if (null != replacement) {
        return replacement;
    } else {
        for (String key : nullKeys) {
            obj.remove(key);
        }
    }
    return obj;
}

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

/**
 * If the next value is {@link JsonToken#NULL}, consume it and return {@code true}. Otherwise
 * return {@code false}.//ww w .  j a va2 s.co m
 */
public static boolean handleNull(JsonReader reader) throws IOException {
    if (reader.peek() == JsonToken.NULL) {
        reader.nextNull();
        return true;
    }
    return false;
}

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

public static void assertType(JsonReader reader, String name, JsonToken... expectedTypes) throws IOException {
    JsonToken actualType = reader.peek();
    if (!arrayContains(expectedTypes, actualType)) {
        throw new IllegalStateException(
                String.format(Locale.US, "Expected value of \"%s\" to be one of \"%s\" but found \"%s\".", name,
                        Arrays.toString(expectedTypes), actualType));
    }//from  www.  ja  v a 2  s  .c om
}

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

/**
 * Determines what the next value is and returns it as the appropriate basic type or a custom
 * object, a collection, a {@link JSONObject}, or {@link JSONArray}.
 *
 * @param reader The JsonReader to use. The next token ({@link JsonReader#peek()} must be a
 * value./* ww  w. ja v a 2s  .  c om*/
 * @param convertJsonTypes If {@code true}, and the next value is a JSONArray, it will be
 * converted to a Collection, and if the next value is a JSONObject, it will be parsed into the
 * appropriate object type. If {@code false}, a raw JSONArray or JSONObject will be returned.
 *
 * @return The next value. If the next value is {@link JsonToken#NULL}, then {@code null} is
 * returned.
 */
public static Object parseNextValue(JsonReader reader, boolean convertJsonTypes) throws IOException {
    JsonToken nextToken = reader.peek();
    switch (nextToken) {
    case BEGIN_ARRAY:
        if (convertJsonTypes) {
            Collection<Object> collection = new ArrayList<>();
            parseJsonArray(reader, collection, null, Object.class, null, null);
            return collection;
        } else {
            return parseAsJsonArray(reader, null);
        }
    case BEGIN_OBJECT:
        if (convertJsonTypes) {
            return parseJsonObject(reader, null, null, null);
        } else {
            return parseAsJsonObject(reader, null);
        }
    case BOOLEAN:
        return reader.nextBoolean();
    case NUMBER:
    case STRING:
        return reader.nextString();
    case NULL:
        reader.nextNull();
        return null;

    default:
        throw new IllegalStateException("Unexpected token: " + nextToken);
    }
}

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

public static String nextString(JsonReader reader, String name) throws IOException {
    if (handleNull(reader)) {
        return null;
    }/*from  w  ww.j  a v  a2s. c  o  m*/
    assertType(reader, name, JsonToken.STRING, JsonToken.NUMBER, JsonToken.BOOLEAN);

    if (reader.peek() == JsonToken.BOOLEAN) {
        return String.valueOf(reader.nextBoolean());
    }

    return reader.nextString();
}

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

/**
 * Parses an array in the JSON data stream.
 *
 * @param reader/*  w  w  w.ja  va2s  . 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:com.example.propertylist.handler.JsonPropertyHandler.java

/**
 * Loads the next observation into the property class.
 *
 * @param reader/*from w  ww . j a v a2s . c o m*/
 *            the {@link android.util.JsonReader} containing the observation
 * @throws java.io.IOException
 */
private Property parseSalesData(JsonReader reader) throws IOException {
    Property property = new Property();
    reader.beginObject();
    while (reader.hasNext()) {
        String name = reader.nextName();
        if (name.equals("full_address") && reader.peek() != JsonToken.NULL) {
            property.setFullAddress(reader.nextString());
        } else if (name.equals("daft_url") && reader.peek() != JsonToken.NULL) {
            property.setDaftPropertyUrl(reader.nextString());
        } else if (name.equals("description") && reader.peek() != JsonToken.NULL) {
            property.setDescription(reader.nextString());
        } else if (name.equals("small_thumbnail_url") && reader.peek() != JsonToken.NULL) {
            property.setThumbnailUrl(reader.nextString());
        } else if (name.equals("medium_thumbnail_url") && reader.peek() != JsonToken.NULL) {
            property.setMediumThumbnailUrl(reader.nextString());
        } else if (name.equals("large_thumbnail_url") && reader.peek() != JsonToken.NULL) {
            property.setLargeThumbnailUrl(reader.nextString());
        } else {
            reader.skipValue();
        } // end if hasnext
    } // end while
    reader.endObject();
    return property;
}

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

/**
 * Populates properties data with the JSON data.
 *
 * @param reader/*ww w.  j a  v  a 2s  .  c  om*/
 *            the {@link android.util.JsonReader} used to read in the data
 * @return
 *         the propertyAds
 * @throws org.json.JSONException
 * @throws java.io.IOException
 */
public List<Property> populateAdsSales(JsonReader reader) throws JSONException, IOException {
    List<Property> propertyAds = new ArrayList<Property>();
    reader.beginObject();
    while (reader.hasNext()) {
        final String name = reader.nextName();
        final boolean isNull = reader.peek() == JsonToken.NULL;
        if (name.equals("ads") && !isNull) {
            propertyAds = parseDataArray(reader);
        } else {
            reader.skipValue();
        }
    }
    reader.endObject();
    return propertyAds;
}