1 package name.gumartinm.weather.information.parser;
3 import com.fasterxml.jackson.core.JsonFactory;
4 import com.fasterxml.jackson.core.JsonParseException;
5 import com.fasterxml.jackson.core.JsonParser;
6 import com.fasterxml.jackson.core.JsonToken;
8 import java.io.IOException;
9 import java.util.ArrayList;
11 import name.gumartinm.weather.information.model.forecastweather.City;
12 import name.gumartinm.weather.information.model.forecastweather.Coord;
13 import name.gumartinm.weather.information.model.forecastweather.Forecast;
14 import name.gumartinm.weather.information.model.forecastweather.List;
15 import name.gumartinm.weather.information.model.forecastweather.Temp;
16 import name.gumartinm.weather.information.model.forecastweather.Weather;
18 public class JPOSForecastParser {
20 public Forecast retrieveForecastFromJPOS(final String jsonData)
21 throws JsonParseException, IOException {
22 final JsonFactory f = new JsonFactory();
24 final Forecast forecastWeatherData = new Forecast();
26 .setList(new ArrayList<List>(15));
27 final City city = new City();
28 city.setCoord(new Coord());
29 forecastWeatherData.setCity(city);
30 final JsonParser jParser = f.createParser(jsonData);
32 this.getForecastWeatherData(forecastWeatherData, jParser);
34 return forecastWeatherData;
37 private void getForecastWeatherData(final Forecast forecastWeatherData,
38 final JsonParser jParser) throws JsonParseException, IOException {
39 if (jParser.nextToken() == JsonToken.START_OBJECT) {
41 while (jParser.nextToken() != JsonToken.END_OBJECT) {
42 final String fieldname = jParser.getCurrentName();
43 final JsonToken nextToken = jParser.nextToken();
44 if (nextToken == JsonToken.START_OBJECT) {
45 this.getForecastWeatherDataObjects(forecastWeatherData, jParser, fieldname);
47 if (nextToken == JsonToken.START_ARRAY) {
48 JsonToken tokenNext = jParser.nextToken();
49 while (tokenNext != JsonToken.END_ARRAY) {
50 if (tokenNext == JsonToken.START_OBJECT) {
51 this.getForecastWeatherDataObjects(forecastWeatherData, jParser, fieldname);
53 tokenNext = jParser.nextToken();
56 if ((nextToken == JsonToken.VALUE_NUMBER_INT)
57 || (nextToken == JsonToken.VALUE_STRING)) {
58 this.getForecastWeatherDataObjects(forecastWeatherData, jParser, fieldname);
64 private void getForecastWeatherDataObjects(final Forecast forecastWeatherData,
65 final JsonParser jParser, final String fieldname) throws JsonParseException,
68 if ("cod".equals(fieldname)) {
69 final String stringCod = jParser.getText();
70 forecastWeatherData.setCod(Long.valueOf(stringCod));
72 if ("message".equals(fieldname)) {
73 forecastWeatherData.setMessage(jParser.getDoubleValue());
75 if ("city".equals(fieldname)) {
76 while (jParser.nextToken() != JsonToken.END_OBJECT) {
77 final String namefield = jParser.getCurrentName();
78 final JsonToken nextToken = jParser.nextToken(); // move to
80 if ("id".equals(namefield)) {
81 forecastWeatherData.getCity().setId(jParser.getLongValue());
83 if ("name".equals(namefield)) {
84 forecastWeatherData.getCity().setName(jParser.getText());
86 if ("coord".equals(namefield)) {
87 if (nextToken == JsonToken.START_OBJECT) {
88 this.getForecastWeatherDataObjects(forecastWeatherData, jParser, namefield);
91 if ("country".equals(namefield)) {
92 forecastWeatherData.getCity().setCountry(jParser.getText());
94 if ("population".equals(namefield)) {
95 forecastWeatherData.getCity().setPopulation(jParser.getLongValue());
99 if ("cnt".equals(fieldname)) {
100 forecastWeatherData.setCnt(jParser.getIntValue());
102 if ("coord".equals(fieldname)) {
103 while (jParser.nextToken() != JsonToken.END_OBJECT) {
104 final String namefield = jParser.getCurrentName();
105 jParser.nextToken(); // move to value
106 if ("lon".equals(namefield)) {
107 forecastWeatherData.getCity().getCoord().setLon(jParser.getDoubleValue());
109 if ("lat".equals(namefield)) {
110 forecastWeatherData.getCity().getCoord().setLat(jParser.getDoubleValue());
114 if ("list".equals(fieldname)) {
115 final name.gumartinm.weather.information.model.forecastweather.List list = new name.gumartinm.weather.information.model.forecastweather.List();
116 list.setTemp(new Temp());
117 list.setWeather(new ArrayList<Weather>());
118 forecastWeatherData.getList().add(list);
119 while (jParser.nextToken() != JsonToken.END_OBJECT) {
120 final String namefield = jParser.getCurrentName();
121 final JsonToken nextToken = jParser.nextToken(); // move to
123 if ("dt".equals(namefield)) {
124 list.setDt(jParser.getLongValue());
126 if ("temp".equals(namefield)) {
127 if (nextToken == JsonToken.START_OBJECT) {
128 this.getForecastWeatherDataObjects(forecastWeatherData, jParser, namefield);
131 if ("pressure".equals(namefield)) {
132 list.setPressure(jParser.getDoubleValue());
134 if ("humidity".equals(namefield)) {
135 list.setHumidity(jParser.getDoubleValue());
137 if ("weather".equals(namefield)) {
138 if (nextToken == JsonToken.START_ARRAY) {
139 JsonToken tokenNext = jParser.nextToken();
140 while (tokenNext != JsonToken.END_ARRAY) {
141 if (tokenNext == JsonToken.START_OBJECT) {
142 this.getForecastWeatherDataObjects(forecastWeatherData, jParser,
145 tokenNext = jParser.nextToken();
149 if ("speed".equals(namefield)) {
150 list.setSpeed(jParser.getDoubleValue());
152 if ("deg".equals(namefield)) {
153 list.setDeg(jParser.getDoubleValue());
155 if ("clouds".equals(namefield)) {
156 list.setClouds(jParser.getDoubleValue());
158 if ("rain".equals(namefield)) {
159 list.setRain(jParser.getDoubleValue());
163 if ("temp".equals(fieldname)) {
164 final name.gumartinm.weather.information.model.forecastweather.List list =
165 forecastWeatherData.getList().get((forecastWeatherData.getList().size() - 1));
166 while (jParser.nextToken() != JsonToken.END_OBJECT) {
167 final String namefield = jParser.getCurrentName();
168 jParser.nextToken(); // move to value
169 if ("day".equals(namefield)) {
170 list.getTemp().setDay(jParser.getDoubleValue());
172 if ("min".equals(namefield)) {
173 list.getTemp().setMin(jParser.getDoubleValue());
175 if ("max".equals(namefield)) {
176 list.getTemp().setMax(jParser.getDoubleValue());
178 if ("night".equals(namefield)) {
179 list.getTemp().setNight(jParser.getDoubleValue());
181 if ("eve".equals(namefield)) {
182 list.getTemp().setEve(jParser.getDoubleValue());
184 if ("morn".equals(namefield)) {
185 list.getTemp().setMorn(jParser.getDoubleValue());
189 if ("weather".equals(fieldname)) {
190 final name.gumartinm.weather.information.model.forecastweather.List list =
191 forecastWeatherData.getList().get((forecastWeatherData.getList().size() - 1));
192 final Weather weather = new Weather();
193 while (jParser.nextToken() != JsonToken.END_OBJECT) {
194 final String namefield = jParser.getCurrentName();
195 jParser.nextToken(); // move to value
197 if ("id".equals(namefield)) {
198 weather.setId(jParser.getIntValue());
200 if ("main".equals(namefield)) {
201 weather.setMain(jParser.getText());
203 if ("description".equals(namefield)) {
204 weather.setDescription(jParser.getText());
206 if ("icon".equals(namefield)) {
207 weather.setIcon(jParser.getText());
210 list.getWeather().add(weather);