Example usage for android.util JsonReader hasNext

List of usage examples for android.util JsonReader hasNext

Introduction

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

Prototype

public boolean hasNext() throws IOException 

Source Link

Document

Returns true if the current array or object has another element.

Usage

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

/**
 * Loads the next observation into the property class.
 *
 * @param reader/*  w  w  w.  j  a  v a  2 s  .c  om*/
 *            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: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 {// ww  w  .  j a  v a2  s  .  co  m
            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.workday.autoparse.json.parser.JsonParserUtils.java

/**
 * Call this method when parsing an object, the parser type is unknown, and the first name
 * inside the object was the discrimination name. This method will add all values to a {@link
 * JSONObject} until the discrimination name and a matching parser are found. If no matching
 * parser is ever found, then this method returns the JSONObject.
 *
 * @param reader The reader to use.//from w  w w . j  a  va  2 s .c  o m
 * @param firstName The first name parsed in this object so far. May be null, but the next toke
 * in the JsonReader should be a {@link JsonToken#NAME}.
 * @param firstValue The first value parse in this object so far. May be null, and if {@code
 * firstName} is not null, the next token in the JsonReader should be a value type.
 *
 * @return A custom object or a JSONObject if no appropriate parser was found.
 */
private static Object parseSpecificJsonObjectDelayed(JsonReader reader, String firstName, Object firstValue)
        throws IOException {
    final String discriminationName = ContextHolder.getContext().getSettings().getDiscriminationName();
    JSONObject jsonObject = new JSONObject();
    String name = firstName;
    Object value = firstValue;

    if (name == null && reader.hasNext()) {
        name = reader.nextName();
    }
    if (value == null && name != null) {
        value = parseNextValue(reader, false);
    }

    while (name != null) {
        if (discriminationName.equals(name)) {
            if (!(value instanceof String)) {
                throwDiscriminationValueException(discriminationName, value);
            }
            final String discriminationValue = (String) value;
            JsonObjectParser<?> parser = ContextHolder.getContext().getJsonObjectParserTable()
                    .get(discriminationValue);
            if (parser != null) {
                return parser.parseJsonObject(jsonObject, reader, discriminationName, discriminationValue);
            }
        }

        // No matching parser has been found yet; save the current name and value to the
        // jsonObject.
        try {
            jsonObject.put(name, value);
        } catch (JSONException e) {
            // this should only happen if the name is null, which is impossible here.
            throw new RuntimeException("This should be impossible.", e);
        }

        if (reader.hasNext()) {
            name = reader.nextName();
            value = parseNextValue(reader, false);
        } else {
            name = null;
            value = null;
        }
    }
    return jsonObject;
}

From source file:com.tcity.android.ui.info.BuildArtifactsTask.java

private void handleFiles(@NotNull JsonReader reader) throws IOException {
    reader.beginArray();//from   ww  w  .  jav a2 s .  co m

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

    while (reader.hasNext()) {
        reader.beginObject();

        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:com.example.propertylist.handler.JsonPropertyHandler.java

/**
 * Populates properties data with the JSON data.
 *
 * @param reader/*from  w  ww  . j  av  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;
}

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

/**
 * Populates property data with the JSON data.
 *
 * @param reader/*from  ww w  .  j  a v a  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> 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.thingsee.tracker.REST.KiiBucketRequestAsyncTask.java

private JSONArray readSensorDataFromString(String input, int offset) {
    StringReader reader = new StringReader(input);
    try {/*w  w  w  . j a va  2s  .  com*/
        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.tcity.android.ui.info.BuildArtifactsTask.java

private void handleResponse(@NotNull HttpResponse response) throws IOException {
    JsonReader reader = new JsonReader(new InputStreamReader(response.getEntity().getContent()));

    //noinspection TryFinallyCanBeTryWithResources
    try {/*from   w w w . ja v a2 s.  c  om*/
        reader.beginObject();

        while (reader.hasNext()) {
            switch (reader.nextName()) {
            case "file":
                handleFiles(reader);
                break;
            default:
                reader.skipValue();
            }
        }

        reader.endObject();
    } finally {
        reader.close();
    }
}

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

/**
 * Parse the next value as a {@link Map}. Children will be converted to a known type. In
 * general, this method does not handle {@link Set}s as children.
 *
 * @param reader The JsonReader to use. Calls to {@link JsonReader#beginObject()} and {@link
 * JsonReader#endObject()} will be taken care of by this method.
 * @param map The Map to populate./* ww  w  . jav a2 s  .  c  om*/
 * @param valueClass The type of the Map value, corresponding to V in Map{@literal<}K,
 * V{@literal>}.
 * @param parser The parser to use, or null if this method should find an appropriate one on its
 * own.
 * @param key The key corresponding to the current value. This is used to make more useful error
 * messages.
 * @param <T> The value type of the Map, corresponding to V in Map{@literal<}K, V{@literal>}.
 */
public static <T> void parseAsMap(JsonReader reader, Map<String, T> map, Class<T> valueClass,
        JsonObjectParser<T> parser, String key) throws IOException {
    if (handleNull(reader)) {
        return;
    }

    final String discriminationName = ContextHolder.getContext().getSettings().getDiscriminationName();
    assertType(reader, key, JsonToken.BEGIN_OBJECT);
    reader.beginObject();
    while (reader.hasNext()) {
        T value;
        String name = reader.nextName();
        if (parser != null) {
            reader.beginObject();
            value = parser.parseJsonObject(null, reader, discriminationName, null);
            reader.endObject();
        } else {
            Object o = parseNextValue(reader, true);
            if (!valueClass.isInstance(o)) {
                throwMapException(name, key, valueClass, o);
            }
            value = cast(o);

        }
        map.put(name, value);
    }
    reader.endObject();
}

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

/**
 * Parses an array in the JSON data stream.
 *
 * @param reader/*from w ww . j  a  va 2 s .  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;
}