Example usage for android.util JsonReader nextName

List of usage examples for android.util JsonReader nextName

Introduction

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

Prototype

public String nextName() throws IOException 

Source Link

Document

Returns the next token, a JsonToken#NAME property name , and consumes it.

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 a2  s .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.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./*  ww w.j  ava 2s  . co 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.thingsee.tracker.REST.KiiBucketRequestAsyncTask.java

private JSONObject readSingleData(JsonReader jsonReader) throws IOException, JSONException {
    JSONObject jsonObject = new JSONObject();
    jsonReader.beginObject();/*from   ww w.jav  a2  s  .c  o m*/
    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;
}

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

/**
 * Populates properties data with the JSON data.
 *
 * @param reader/*from   ww w .j a v a2 s  .co m*/
 *            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 w  ww  . java  2 s  . 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.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 ww w. j a v  a 2s .c  o  m*/
        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./*from w  w  w.  j ava  2 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

/**
 * Populates property data with the JSON data.
 *
 * @param reader/*from w w w .ja  v  a  2  s . 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> 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:fiskinfoo.no.sintef.fiskinfoo.Http.BarentswatchApiRetrofit.BarentswatchApi.java

public BarentswatchApi() {
    String directoryPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)
            .toString();/*from  w w  w.  jav  a 2  s . co  m*/
    String fileName = directoryPath + "/FiskInfo/api_setting.json";
    File file = new File(fileName);
    String environment = null;

    if (file.exists()) {
        InputStream inputStream;
        InputStreamReader streamReader;
        JsonReader jsonReader;

        try {
            inputStream = new BufferedInputStream(new FileInputStream(file));
            streamReader = new InputStreamReader(inputStream, "UTF-8");
            jsonReader = new JsonReader(streamReader);

            jsonReader.beginObject();
            while (jsonReader.hasNext()) {
                String name = jsonReader.nextName();
                if (name.equals("environment")) {
                    environment = jsonReader.nextString();
                } else {
                    jsonReader.skipValue();
                }
            }
            jsonReader.endObject();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (IllegalStateException e) {
            e.printStackTrace();
        }
    }

    targetProd = !"pilot".equals(environment);
    currentPath = targetProd ? barentsWatchProdAddress : barentsWatchPilotAddress;
    BARENTSWATCH_API_ENDPOINT = currentPath + "/api/v1/geodata";

    Executor httpExecutor = Executors.newSingleThreadExecutor();
    MainThreadExecutor callbackExecutor = new MainThreadExecutor();
    barentswatchApi = initializeBarentswatchAPI(httpExecutor, callbackExecutor);
}

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

private GPS readGPS(JsonReader reader) throws IOException {

    String longitude = null;/*from   ww  w  .ja va  2 s . c  o  m*/
    String latitude = null;

    reader.beginObject();
    //        reader.beginArray();
    while (reader.hasNext()) {
        String name = reader.nextName();
        if (name.equals(GPS_LONGITUDE_NAME))
            longitude = reader.nextString();
        else if (name.equals(GPS_LATITUDE_NAME))
            latitude = reader.nextString();
        else
            reader.skipValue();
    }
    //        reader.endArray();
    reader.endObject();

    GPS gps = null;
    if (longitude != null && latitude != null)
        gps = new GPS(longitude, latitude);

    return gps;

}