Example usage for android.util JsonReader beginObject

List of usage examples for android.util JsonReader beginObject

Introduction

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

Prototype

public void beginObject() throws IOException 

Source Link

Document

Consumes the next token from the JSON stream and asserts that it is the beginning of a new object.

Usage

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

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

    //noinspection TryFinallyCanBeTryWithResources
    try {//from w w  w  .j a  v  a 2  s  .com
        reader.beginObject();

        BuildInfoData data = new BuildInfoData();
        SimpleDateFormat dateFormat = new SimpleDateFormat(Common.TEAMCITY_DATE_FORMAT);

        while (reader.hasNext()) {
            switch (reader.nextName()) {
            case "status":
                if (data.status == null) {
                    data.status = com.tcity.android.Status.valueOf(reader.nextString());
                }
                break;
            case "running":
                if (reader.nextBoolean()) {
                    data.status = com.tcity.android.Status.RUNNING;
                }
                break;
            case "branchName":
                data.branch = reader.nextString();
                break;
            case "defaultBranch":
                data.isBranchDefault = reader.nextBoolean();
                break;
            case "statusText":
                data.result = reader.nextString();
                break;
            case "waitReason":
                data.waitReason = reader.nextString();
                break;
            case "queuedDate":
                data.queued = dateFormat.parse(reader.nextString());
                break;
            case "startDate":
                data.started = dateFormat.parse(reader.nextString());
                break;
            case "finishDate":
                data.finished = dateFormat.parse(reader.nextString());
                break;
            case "agent":
                data.agent = getAgentName(reader);
                break;
            default:
                reader.skipValue();
            }
        }

        myResult = data;

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

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

/**
 * Populates property data with the JSON data.
 *
 * @param reader/*from w  w w . j a  va  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:com.tcity.android.ui.info.BuildArtifactsTask.java

private void handleFiles(@NotNull JsonReader reader) throws IOException {
    reader.beginArray();/*from www . j  a v  a  2  s  .  c o 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:fiskinfoo.no.sintef.fiskinfoo.Http.BarentswatchApiRetrofit.BarentswatchApi.java

public BarentswatchApi() {
    String directoryPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)
            .toString();/*from  w w w. j  av  a  2s .  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  . j  ava  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;

}

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();
            reader.nextName();//from ww w .  jav a  2s  .  c  o  m
            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.workday.autoparse.json.parser.JsonParserUtils.java

/**
 * Parse the next value as an object. If the next value is {@link JsonToken#NULL}, returns
 * null.//from w  w w .j a  va2  s.c o  m
 * <p/>
 * This method will use the provide parser, or if none is provided, will attempt find an
 * appropriate parser based on the discrimination value found in the next object. If none is
 * found, then this method returns a {@link JSONObject}.
 *
 * @param reader The JsonReader to use. Calls to {@link JsonReader#beginObject()} and {@link
 * JsonReader#endObject()} will be taken care of by this method.
 * @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 expectedType The expected class of the resulting object. If the result is not an
 * instance of this class, an exception is thrown.
 *
 * @throws IllegalStateException if the resulting object is not an instance of {@code
 * expectedType}.
 */
public static Object parseJsonObject(JsonReader reader, JsonObjectParser<?> parser, String key,
        Class<?> expectedType) throws IOException, IllegalStateException {
    if (handleNull(reader)) {
        return null;
    }
    assertType(reader, key, JsonToken.BEGIN_OBJECT);

    final String discriminationName = ContextHolder.getContext().getSettings().getDiscriminationName();
    String discriminationValue = null;
    Object result = null;
    reader.beginObject();
    if (parser != null) {
        result = parser.parseJsonObject(null, reader, discriminationName, null);
    } else if (reader.hasNext()) {
        String firstName = reader.nextName();
        final String discriminationKeyName = ContextHolder.getContext().getSettings().getDiscriminationName();
        if (discriminationKeyName.equals(firstName)) {
            discriminationValue = reader.nextString();
            parser = ContextHolder.getContext().getJsonObjectParserTable().get(discriminationValue);
            if (parser != null) {
                result = parser.parseJsonObject(null, reader, discriminationName, discriminationValue);
            } else {
                result = parseSpecificJsonObjectDelayed(reader, discriminationKeyName, discriminationValue);
            }
        } else {
            result = parseSpecificJsonObjectDelayed(reader, firstName, null);
        }

    }
    reader.endObject();

    if (result == null) {
        result = new JSONObject();
    }

    JsonObjectParser<?> unknownObjectParser = ContextHolder.getContext().getSettings().getUnknownObjectParser();
    if (result instanceof JSONObject && unknownObjectParser != null) {
        result = unknownObjectParser.parseJsonObject((JSONObject) result, null, discriminationName,
                discriminationValue);
    }

    if (expectedType != null && !(expectedType.isInstance(result))) {
        throw new IllegalStateException(
                String.format(Locale.US, "Could not convert value at \"%s\" to %s from %s.", key,
                        expectedType.getCanonicalName(), result.getClass().getCanonicalName()));
    }
    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 .jav a  2s  . co 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:at.ac.tuwien.caa.docscan.logic.DataLog.java

private ShotLog readShotLog(JsonReader reader) throws IOException, ParseException {

    GPS gps = null;//w  w  w  .ja v  a  2 s  .c o  m
    String dateString, fileName = null;
    Date date = null;
    boolean seriesMode = false;

    reader.beginObject();
    while (reader.hasNext()) {
        String name = reader.nextName();
        if (name.equals(FILE_NAME)) {
            fileName = reader.nextString();
        } else if (name.equals(DATE_NAME)) {
            dateString = reader.nextString();
            if (dateString != null)
                date = string2Date(dateString);
        } else if (name.equals(GPS_NAME)) {
            gps = readGPS(reader);
        } else if (name.equals(SERIES_MODE_NAME)) {
            seriesMode = reader.nextBoolean();
        }

    }
    reader.endObject();

    ShotLog shotLog = new ShotLog(fileName, gps, date, seriesMode);
    return shotLog;

}

From source file:dk.cafeanalog.AnalogDownloader.java

public AnalogStatus isOpen() {
    HttpURLConnection connection = null;
    JsonReader reader = null;

    try {/*from w w  w .  j  a  va  2s  .  c  o m*/
        URL url = new URL("http", "cafeanalog.dk", "api/open");
        connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("GET");
        connection.connect();
        reader = new JsonReader(new InputStreamReader(connection.getInputStream()));
        reader.beginObject();
        while (!reader.nextName().equals("open")) {
            reader.skipValue();
        }
        return reader.nextBoolean() ? AnalogStatus.OPEN : AnalogStatus.CLOSED;
    } catch (IOException e) {
        return AnalogStatus.UNKNOWN;
    } finally {
        if (connection != null)
            connection.disconnect();
        if (reader != null) {
            try {
                reader.close();
            } catch (IOException ignored) {
            }
        }
    }
}