2 * Copyright 2014 Gustavo Martin Morcuende
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package name.gumartinm.weather.information.parser;
18 import com.fasterxml.jackson.core.JsonFactory;
19 import com.fasterxml.jackson.core.JsonParseException;
20 import com.fasterxml.jackson.core.JsonParser;
21 import com.fasterxml.jackson.core.JsonToken;
23 import java.io.IOException;
24 import java.util.ArrayList;
26 import name.gumartinm.weather.information.model.forecastweather.City;
27 import name.gumartinm.weather.information.model.forecastweather.Coord;
28 import name.gumartinm.weather.information.model.forecastweather.Forecast;
29 import name.gumartinm.weather.information.model.forecastweather.List;
30 import name.gumartinm.weather.information.model.forecastweather.Temp;
31 import name.gumartinm.weather.information.model.forecastweather.Weather;
33 public class JPOSForecastParser {
35 public Forecast retrieveForecastFromJPOS(final String jsonData)
36 throws JsonParseException, IOException {
37 final JsonFactory f = new JsonFactory();
39 final Forecast forecastWeatherData = new Forecast();
41 .setList(new ArrayList<List>(15));
42 final City city = new City();
43 city.setCoord(new Coord());
44 forecastWeatherData.setCity(city);
45 final JsonParser jParser = f.createParser(jsonData);
47 this.getForecastWeatherData(forecastWeatherData, jParser);
49 return forecastWeatherData;
52 private void getForecastWeatherData(final Forecast forecastWeatherData,
53 final JsonParser jParser) throws JsonParseException, IOException {
54 if (jParser.nextToken() == JsonToken.START_OBJECT) {
56 while (jParser.nextToken() != JsonToken.END_OBJECT) {
57 final String fieldname = jParser.getCurrentName();
58 final JsonToken nextToken = jParser.nextToken();
59 if (nextToken == JsonToken.START_OBJECT) {
60 this.getForecastWeatherDataObjects(forecastWeatherData, jParser, fieldname);
62 if (nextToken == JsonToken.START_ARRAY) {
63 JsonToken tokenNext = jParser.nextToken();
64 while (tokenNext != JsonToken.END_ARRAY) {
65 if (tokenNext == JsonToken.START_OBJECT) {
66 this.getForecastWeatherDataObjects(forecastWeatherData, jParser, fieldname);
68 tokenNext = jParser.nextToken();
71 if ((nextToken == JsonToken.VALUE_NUMBER_INT)
72 || (nextToken == JsonToken.VALUE_STRING)) {
73 this.getForecastWeatherDataObjects(forecastWeatherData, jParser, fieldname);
79 private void getForecastWeatherDataObjects(final Forecast forecastWeatherData,
80 final JsonParser jParser, final String fieldname) throws JsonParseException,
83 if ("cod".equals(fieldname)) {
84 final String stringCod = jParser.getText();
85 forecastWeatherData.setCod(Long.valueOf(stringCod));
87 if ("message".equals(fieldname)) {
88 forecastWeatherData.setMessage(jParser.getDoubleValue());
90 if ("city".equals(fieldname)) {
91 while (jParser.nextToken() != JsonToken.END_OBJECT) {
92 final String namefield = jParser.getCurrentName();
93 final JsonToken nextToken = jParser.nextToken(); // move to
95 if ("id".equals(namefield)) {
96 forecastWeatherData.getCity().setId(jParser.getLongValue());
98 if ("name".equals(namefield)) {
99 forecastWeatherData.getCity().setName(jParser.getText());
101 if ("coord".equals(namefield)) {
102 if (nextToken == JsonToken.START_OBJECT) {
103 this.getForecastWeatherDataObjects(forecastWeatherData, jParser, namefield);
106 if ("country".equals(namefield)) {
107 forecastWeatherData.getCity().setCountry(jParser.getText());
109 if ("population".equals(namefield)) {
110 forecastWeatherData.getCity().setPopulation(jParser.getLongValue());
114 if ("cnt".equals(fieldname)) {
115 forecastWeatherData.setCnt(jParser.getIntValue());
117 if ("coord".equals(fieldname)) {
118 while (jParser.nextToken() != JsonToken.END_OBJECT) {
119 final String namefield = jParser.getCurrentName();
120 jParser.nextToken(); // move to value
121 if ("lon".equals(namefield)) {
122 forecastWeatherData.getCity().getCoord().setLon(jParser.getDoubleValue());
124 if ("lat".equals(namefield)) {
125 forecastWeatherData.getCity().getCoord().setLat(jParser.getDoubleValue());
129 if ("list".equals(fieldname)) {
130 final name.gumartinm.weather.information.model.forecastweather.List list = new name.gumartinm.weather.information.model.forecastweather.List();
131 list.setTemp(new Temp());
132 list.setWeather(new ArrayList<Weather>());
133 forecastWeatherData.getList().add(list);
134 while (jParser.nextToken() != JsonToken.END_OBJECT) {
135 final String namefield = jParser.getCurrentName();
136 final JsonToken nextToken = jParser.nextToken(); // move to
138 if ("dt".equals(namefield)) {
139 list.setDt(jParser.getLongValue());
141 if ("temp".equals(namefield)) {
142 if (nextToken == JsonToken.START_OBJECT) {
143 this.getForecastWeatherDataObjects(forecastWeatherData, jParser, namefield);
146 if ("pressure".equals(namefield)) {
147 list.setPressure(jParser.getDoubleValue());
149 if ("humidity".equals(namefield)) {
150 list.setHumidity(jParser.getDoubleValue());
152 if ("weather".equals(namefield)) {
153 if (nextToken == JsonToken.START_ARRAY) {
154 JsonToken tokenNext = jParser.nextToken();
155 while (tokenNext != JsonToken.END_ARRAY) {
156 if (tokenNext == JsonToken.START_OBJECT) {
157 this.getForecastWeatherDataObjects(forecastWeatherData, jParser,
160 tokenNext = jParser.nextToken();
164 if ("speed".equals(namefield)) {
165 list.setSpeed(jParser.getDoubleValue());
167 if ("deg".equals(namefield)) {
168 list.setDeg(jParser.getDoubleValue());
170 if ("clouds".equals(namefield)) {
171 list.setClouds(jParser.getDoubleValue());
173 if ("rain".equals(namefield)) {
174 list.setRain(jParser.getDoubleValue());
178 if ("temp".equals(fieldname)) {
179 final name.gumartinm.weather.information.model.forecastweather.List list =
180 forecastWeatherData.getList().get((forecastWeatherData.getList().size() - 1));
181 while (jParser.nextToken() != JsonToken.END_OBJECT) {
182 final String namefield = jParser.getCurrentName();
183 jParser.nextToken(); // move to value
184 if ("day".equals(namefield)) {
185 list.getTemp().setDay(jParser.getDoubleValue());
187 if ("min".equals(namefield)) {
188 list.getTemp().setMin(jParser.getDoubleValue());
190 if ("max".equals(namefield)) {
191 list.getTemp().setMax(jParser.getDoubleValue());
193 if ("night".equals(namefield)) {
194 list.getTemp().setNight(jParser.getDoubleValue());
196 if ("eve".equals(namefield)) {
197 list.getTemp().setEve(jParser.getDoubleValue());
199 if ("morn".equals(namefield)) {
200 list.getTemp().setMorn(jParser.getDoubleValue());
204 if ("weather".equals(fieldname)) {
205 final name.gumartinm.weather.information.model.forecastweather.List list =
206 forecastWeatherData.getList().get((forecastWeatherData.getList().size() - 1));
207 final Weather weather = new Weather();
208 while (jParser.nextToken() != JsonToken.END_OBJECT) {
209 final String namefield = jParser.getCurrentName();
210 jParser.nextToken(); // move to value
212 if ("id".equals(namefield)) {
213 weather.setId(jParser.getIntValue());
215 if ("main".equals(namefield)) {
216 weather.setMain(jParser.getText());
218 if ("description".equals(namefield)) {
219 weather.setDescription(jParser.getText());
221 if ("icon".equals(namefield)) {
222 weather.setIcon(jParser.getText());
225 list.getWeather().add(weather);