Example usage for com.google.gson.stream JsonReader skipValue

List of usage examples for com.google.gson.stream JsonReader skipValue

Introduction

In this page you can find the example usage for com.google.gson.stream JsonReader skipValue.

Prototype

public void skipValue() throws IOException 

Source Link

Document

Skips the next value recursively.

Usage

From source file:vogar.OutcomeStore.java

License:Apache License

private void loadOutcomes(Map<String, AnnotatedOutcome> map, File file, long fileDate) throws IOException {
    JsonReader in = new JsonReader(new FileReader(file));
    in.beginObject();//from  ww  w  .j a  v  a  2s .c  o  m
    while (in.hasNext()) {
        String outcomeName = in.nextName();
        AnnotatedOutcome annotatedOutcome = map.get(outcomeName);
        if (annotatedOutcome == null) {
            in.skipValue();
            continue;
        }

        Result result = null;
        in.beginObject();
        while (in.hasNext()) {
            String fieldName = in.nextName();
            if (fieldName.equals("result")) {
                result = Result.valueOf(in.nextString());
            } else {
                in.skipValue();
            }
        }
        in.endObject();

        annotatedOutcome.add(fileDate, new Outcome(outcomeName, result, Collections.<String>emptyList()));
    }
    in.endObject();
    in.close();
}

From source file:zack.yovel.clear.infrastructure.model.datapoints.ForecastIoParser.java

License:Apache License

@Override
public WeatherReport parse(String response) {
    WeatherReport output = new WeatherReport();
    JsonReader reader = new JsonReader(new InputStreamReader(new ByteArrayInputStream(response.getBytes())));
    try {/*from w w  w .  j av  a 2 s  . c  om*/
        JsonToken token = reader.peek();
        switch (token) {
        case BEGIN_OBJECT:
            doParse(reader, output);
            break;
        default: // In case api changes and we receive a different json structure, skip a value (and basically break down gracefully).
            Log.d(TAG, "parse found a different json structure. Root element is not an object.");
            reader.skipValue();
        }
    } catch (IOException e) {
        // No real IO operations are done here, hence no exception should be thrown.
        Log.e(TAG, "parse failed with exception: " + e.getMessage());
    }
    return output;
}

From source file:zack.yovel.clear.infrastructure.model.datapoints.ForecastIoParser.java

License:Apache License

private void doParse(JsonReader reader, WeatherReport output) throws IOException {
    reader.beginObject();//w w w.j  av  a2  s.  com
    while (reader.hasNext()) {
        String name = reader.nextName();
        if (name.equals("latitude")) {
            output.setLatitude(reader.nextDouble());
        } else if (name.equals("longitude")) {
            output.setLongitude(reader.nextDouble());
        } else if (name.equals("currently")) {
            output.setCurrently(parseDataPoint(reader));
        } else if (name.equals("hourly")) {
            output.setHourly(parseDataBlock(reader));
        } else if (name.equals("daily")) {
            output.setDaily(parseDataBlock(reader));
        } else if (name.equals("alerts")) {
            output.setAlerts(parseAlerts(reader));
        } else {
            reader.skipValue();
        }
    }
    reader.endObject();
}

From source file:zack.yovel.clear.infrastructure.model.datapoints.ForecastIoParser.java

License:Apache License

private ArrayList<Alert> parseAlerts(JsonReader reader) throws IOException {
    ArrayList<Alert> output = new ArrayList<Alert>();
    reader.beginArray();/*from w  w  w.j  a  v  a 2  s . co m*/
    boolean exit = false;
    while (!exit && reader.hasNext()) {
        JsonToken token = reader.peek();
        switch (token) {
        case BEGIN_OBJECT:
            output.add(parseAlert(reader));
            break;
        case END_ARRAY:
            exit = true;
            break;
        default:
            reader.skipValue();
        }
    }
    reader.endArray();
    return output;
}

From source file:zack.yovel.clear.infrastructure.model.datapoints.ForecastIoParser.java

License:Apache License

private Alert parseAlert(JsonReader reader) throws IOException {
    Alert output = new Alert();
    reader.beginObject();/*from  w  ww  .j  a  v a 2 s. c o  m*/
    String name = reader.nextName();
    if (name.equals("title")) {
        output.setTitle(reader.nextString());
    } else if (name.equals("expires")) {
        output.setExpires(reader.nextLong());
    } else if (name.equals("description")) {
        output.setDescription(reader.nextString());
    } else if (name.equals("uri")) {
        output.setUri(reader.nextString());
    } else {
        reader.skipValue();
    }
    reader.endObject();
    return output;
}

From source file:zack.yovel.clear.infrastructure.model.datapoints.ForecastIoParser.java

License:Apache License

private void getDataBlockProperty(JsonReader reader, DataBlock output) throws IOException {
    String name = reader.nextName();
    if (name.equals("icon")) {
        output.setIcon(reader.nextString());
    } else if (name.equals("data")) {
        output.setData(getDataPointList(reader));
    } else {/*w  w  w.  ja  v a2 s  .c o m*/
        reader.skipValue();
    }
}

From source file:zack.yovel.clear.infrastructure.model.datapoints.ForecastIoParser.java

License:Apache License

private ArrayList<DataPoint> getDataPointList(JsonReader reader) throws IOException {
    ArrayList<DataPoint> output = new ArrayList<DataPoint>();
    reader.beginArray();//from ww  w .  j a v a 2 s .  c om
    while (reader.hasNext()) {
        JsonToken token = reader.peek();
        switch (token) {
        case BEGIN_OBJECT:
            output.add(parseDataPoint(reader));
            break;
        default:
            reader.skipValue();
        }
    }
    reader.endArray();
    return output;
}

From source file:zack.yovel.clear.infrastructure.model.datapoints.ForecastIoParser.java

License:Apache License

private void getDataPointProperty(JsonReader reader, DataPoint output) throws IOException {
    String name = reader.nextName();
    if (name.equals("time")) {
        output.setTime(reader.nextLong() * 1000); // forecast api gives time in seconds, while Java, Android and joda time work with milliseconds...
    } else if (name.equals("icon")) {
        output.setIcon(reader.nextString());
    } /*else if (name.equals("precipIntensity")) {
      output.setPrecipIntensity(reader.nextDouble());
      } else if (name.equals("precipProbability")) {
      output.setPrecipProbability(reader.nextDouble());
      } else if (name.equals("precipType")) {
      output.setPrecipType(reader.nextString());
      } else if (name.equals("precipAccumulation")) {
      output.setPrecipAccumulation(reader.nextDouble());
      }*/ else if (name.equals("temperature")) {
        output.setTemperature(reader.nextDouble());
    } else if (name.equals("temperatureMin")) {
        output.setTemperatureMin(reader.nextDouble());
    } else if (name.equals("temperatureMinTime")) {
        output.setTemperatureMinTime(reader.nextLong());
    } else if (name.equals("temperatureMax")) {
        output.setTemperatureMax(reader.nextDouble());
    } else if (name.equals("temperatureMaxTime")) {
        output.setTemperatureMaxTime(reader.nextLong());
    } else if (name.equals("apparentTemperature")) {
        output.setApparentTemperature(reader.nextDouble());
    } else if (name.equals("apparentTemperatureMin")) {
        output.setApparentTemperatureMin(reader.nextDouble());
    } else if (name.equals("apparentTemperatureMax")) {
        output.setApparentTemperatureMax(reader.nextDouble());
    } else if (name.equals("apparentTemperatureMaxTime")) {
        output.setApparentTemperatureMaxTime(reader.nextLong());
    } else if (name.equals("windSpeed")) {
        output.setWindSpeed(reader.nextDouble());
    } else if (name.equals("windBearing")) {
        output.setWindBearing(reader.nextDouble());
    } /*else if (name.equals("cloudCover")) {
      output.setCloudCover(reader.nextDouble());
      }*/ else if (name.equals("humidity")) {
        output.setHumidity(reader.nextDouble());
    } else {//from   ww  w.  jav a2  s  .c  o m
        reader.skipValue();
    }
}