Example usage for android.util JsonReader endObject

List of usage examples for android.util JsonReader endObject

Introduction

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

Prototype

public void endObject() throws IOException 

Source Link

Document

Consumes the next token from the JSON stream and asserts that it is the end of the current object.

Usage

From source file:com.murrayc.galaxyzoo.app.LoginUtils.java

public static LoginResult parseLoginResponseContent(final InputStream content) throws IOException {
    //A failure by default.
    LoginResult result = new LoginResult(false, null, null);

    final InputStreamReader streamReader = new InputStreamReader(content, Utils.STRING_ENCODING);
    final JsonReader reader = new JsonReader(streamReader);
    reader.beginObject();//from   w w  w . j a v a  2s.  co m
    boolean success = false;
    String apiKey = null;
    String userName = null;
    String message = null;
    while (reader.hasNext()) {
        final String name = reader.nextName();
        switch (name) {
        case "success":
            success = reader.nextBoolean();
            break;
        case "api_key":
            apiKey = reader.nextString();
            break;
        case "name":
            userName = reader.nextString();
            break;
        case "message":
            message = reader.nextString();
            break;
        default:
            reader.skipValue();
        }
    }

    if (success) {
        result = new LoginResult(true, userName, apiKey);
    } else {
        Log.info("Login failed.");
        Log.info("Login failure message: " + message);
    }

    reader.endObject();
    reader.close();

    streamReader.close();

    return result;
}

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();/*  w w  w. j  a v a 2s . c  o  m*/
        while (reader.hasNext()) {
            reader.beginObject();
            Emergency hospitalEmergencyDetail = new Emergency();
            readEmergency(reader, hospitalEmergencyDetail);
            reader.endObject();
            result.add(hospitalEmergencyDetail);
        }
        reader.endArray();
    }
    return result;
}

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  ww  w.  j  a  v  a  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.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  .j  ava2s  .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: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();// w  w  w  .j ava 2 s .c om
            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.example.propertylist.handler.JsonPropertyHandler.java

/**
 * Populates properties data with the JSON data.
 *
 * @param reader//from ww  w . jav a 2 s  .  c  o  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//w  w w.  j  a va2 s.  com
 *            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/* ww  w  .j av a2 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> 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  ww  w . ja  v  a 2s . c  o 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: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   w  w  w  .  j  av  a2  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();
}