Search in sources :

Example 1 with Temp

use of de.example.jackson.auto.forecast.Temp in project JavaForFun by gumartinm.

the class JacksonStreamingTestMain method getForecastWeatherDataObjects.

public static void getForecastWeatherDataObjects(final ForecastWeatherData forecastWeatherData, final JsonParser jParser, final String fieldname) throws JsonParseException, IOException {
    if (fieldname == "cod") {
        final String stringCod = jParser.getText();
        forecastWeatherData.setCod(Long.valueOf(stringCod));
    }
    if (fieldname == "message") {
        forecastWeatherData.setMessage(jParser.getDoubleValue());
    }
    if (fieldname == "city") {
        while (jParser.nextToken() != JsonToken.END_OBJECT) {
            final String namefield = jParser.getCurrentName();
            // move to value
            final JsonToken nextToken = jParser.nextToken();
            if ("id".equals(namefield)) {
                forecastWeatherData.getCity().setId(jParser.getLongValue());
            }
            if ("name".equals(namefield)) {
                forecastWeatherData.getCity().setName(jParser.getText());
            }
            if ("coord".equals(namefield)) {
                if (nextToken == JsonToken.START_OBJECT) {
                    getForecastWeatherDataObjects(forecastWeatherData, jParser, namefield);
                }
            }
            if ("country".equals(namefield)) {
                forecastWeatherData.getCity().setCountry(jParser.getText());
            }
            if ("population".equals(namefield)) {
                forecastWeatherData.getCity().setPopulation(jParser.getLongValue());
            }
        }
    }
    if (fieldname == "cnt") {
        forecastWeatherData.setCnt(jParser.getIntValue());
    }
    if (fieldname == "coord") {
        while (jParser.nextToken() != JsonToken.END_OBJECT) {
            final String namefield = jParser.getCurrentName();
            // move to value
            jParser.nextToken();
            if ("lon".equals(namefield)) {
                forecastWeatherData.getCity().getCoord().setLon(jParser.getDoubleValue());
            }
            if ("lat".equals(namefield)) {
                forecastWeatherData.getCity().getCoord().setLat(jParser.getDoubleValue());
            }
        }
    }
    if (fieldname == "list") {
        final List list = new List();
        list.setTemp(new Temp());
        list.setWeather(new ArrayList<de.example.jackson.auto.forecast.Weather>());
        forecastWeatherData.getList().add(list);
        while (jParser.nextToken() != JsonToken.END_OBJECT) {
            final String namefield = jParser.getCurrentName();
            // move to value
            final JsonToken nextToken = jParser.nextToken();
            if ("dt".equals(namefield)) {
                list.setDt(jParser.getLongValue());
            }
            if ("temp".equals(namefield)) {
                if (nextToken == JsonToken.START_OBJECT) {
                    getForecastWeatherDataObjects(forecastWeatherData, jParser, namefield);
                }
            }
            if ("pressure".equals(namefield)) {
                list.setPressure(jParser.getDoubleValue());
            }
            if ("humidity".equals(namefield)) {
                list.setHumidity(jParser.getDoubleValue());
            }
            if ("weather".equals(namefield)) {
                if (nextToken == JsonToken.START_ARRAY) {
                    JsonToken tokenNext = jParser.nextToken();
                    while (tokenNext != JsonToken.END_ARRAY) {
                        if (tokenNext == JsonToken.START_OBJECT) {
                            getForecastWeatherDataObjects(forecastWeatherData, jParser, namefield);
                        }
                        tokenNext = jParser.nextToken();
                    }
                }
            }
            if ("speed".equals(namefield)) {
                list.setSpeed(jParser.getDoubleValue());
            }
            if ("deg".equals(namefield)) {
                list.setDeg(jParser.getDoubleValue());
            }
            if ("clouds".equals(namefield)) {
                list.setClouds(jParser.getDoubleValue());
            }
            if ("rain".equals(namefield)) {
                list.setRain(jParser.getDoubleValue());
            }
        }
    }
    if (fieldname == "temp") {
        final List list = forecastWeatherData.getList().get((forecastWeatherData.getList().size() - 1));
        while (jParser.nextToken() != JsonToken.END_OBJECT) {
            final String namefield = jParser.getCurrentName();
            // move to value
            jParser.nextToken();
            if ("day".equals(namefield)) {
                list.getTemp().setDay(jParser.getDoubleValue());
            }
            if ("min".equals(namefield)) {
                list.getTemp().setMin(jParser.getDoubleValue());
            }
            if ("max".equals(namefield)) {
                list.getTemp().setMax(jParser.getDoubleValue());
            }
            if ("night".equals(namefield)) {
                list.getTemp().setNight(jParser.getDoubleValue());
            }
            if ("eve".equals(namefield)) {
                list.getTemp().setEve(jParser.getDoubleValue());
            }
            if ("morn".equals(namefield)) {
                list.getTemp().setMorn(jParser.getDoubleValue());
            }
        }
    }
    if (fieldname == "weather") {
        final List list = forecastWeatherData.getList().get((forecastWeatherData.getList().size() - 1));
        final de.example.jackson.auto.forecast.Weather weather = new de.example.jackson.auto.forecast.Weather();
        while (jParser.nextToken() != JsonToken.END_OBJECT) {
            final String namefield = jParser.getCurrentName();
            // move to value
            jParser.nextToken();
            if ("id".equals(namefield)) {
                weather.setId(jParser.getIntValue());
            }
            if ("main".equals(namefield)) {
                weather.setMain(jParser.getText());
            }
            if ("description".equals(namefield)) {
                weather.setDescription(jParser.getText());
            }
            if ("icon".equals(namefield)) {
                weather.setIcon(jParser.getText());
            }
        }
        list.getWeather().add(weather);
    }
}
Also used : Temp(de.example.jackson.auto.forecast.Temp) JsonToken(com.fasterxml.jackson.core.JsonToken) List(de.example.jackson.auto.forecast.List) ArrayList(java.util.ArrayList)

Aggregations

JsonToken (com.fasterxml.jackson.core.JsonToken)1 List (de.example.jackson.auto.forecast.List)1 Temp (de.example.jackson.auto.forecast.Temp)1 ArrayList (java.util.ArrayList)1