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.currentweather.Clouds;
12 import name.gumartinm.weather.information.model.currentweather.Coord;
13 import name.gumartinm.weather.information.model.currentweather.Current;
14 import name.gumartinm.weather.information.model.currentweather.Main;
15 import name.gumartinm.weather.information.model.currentweather.Rain;
16 import name.gumartinm.weather.information.model.currentweather.Snow;
17 import name.gumartinm.weather.information.model.currentweather.Sys;
18 import name.gumartinm.weather.information.model.currentweather.Weather;
19 import name.gumartinm.weather.information.model.currentweather.Wind;
21 public class JPOSCurrentParser {
23 public Current retrieveCurrenFromJPOS(final String jsonData)
24 throws JsonParseException, IOException {
25 final JsonFactory f = new JsonFactory();
27 final Current currentWeatherData = new Current();
28 currentWeatherData.setClouds(new Clouds());
29 currentWeatherData.setCoord(new Coord());
30 currentWeatherData.setMain(new Main());
31 currentWeatherData.setRain(new Rain());
32 currentWeatherData.setSys(new Sys());
33 currentWeatherData.setSnow(new Snow());
35 .setWeather(new ArrayList<Weather>());
36 currentWeatherData.setWind(new Wind());
37 final JsonParser jParser = f.createParser(jsonData);
39 this.getCurrentWeatherData(currentWeatherData, jParser);
41 return currentWeatherData;
44 private void getCurrentWeatherData(final Current currentWeatherData,
45 final JsonParser jParser) throws JsonParseException, IOException {
46 if (jParser.nextToken() == JsonToken.START_OBJECT) {
48 while (jParser.nextToken() != JsonToken.END_OBJECT) {
49 final String fieldname = jParser.getCurrentName();
50 final JsonToken nextToken = jParser.nextToken();
51 if (nextToken == JsonToken.START_OBJECT) {
52 this.getCurrentWeatherDataObjects(currentWeatherData, jParser, fieldname);
54 if (nextToken == JsonToken.START_ARRAY) {
55 JsonToken tokenNext = jParser.nextToken();
56 while (tokenNext != JsonToken.END_ARRAY) {
57 if (tokenNext == JsonToken.START_OBJECT) {
58 this.getCurrentWeatherDataObjects(currentWeatherData, jParser, fieldname);
60 tokenNext = jParser.nextToken();
63 if ((nextToken == JsonToken.VALUE_NUMBER_INT)
64 || (nextToken == JsonToken.VALUE_STRING)) {
65 this.getCurrentWeatherDataObjects(currentWeatherData, jParser, fieldname);
71 private void getCurrentWeatherDataObjects(final Current currentWeatherData,
72 final JsonParser jParser, final String fieldname) throws JsonParseException,
74 if ("coord".equals(fieldname)) {
75 while (jParser.nextToken() != JsonToken.END_OBJECT) {
76 final String namefield = jParser.getCurrentName();
77 jParser.nextToken(); // move to value
78 if ("lon".equals(namefield)) {
79 currentWeatherData.getCoord().setLon(jParser.getDoubleValue());
81 if ("lat".equals(namefield)) {
82 currentWeatherData.getCoord().setLat(jParser.getDoubleValue());
86 if ("sys".equals(fieldname)) {
87 while (jParser.nextToken() != JsonToken.END_OBJECT) {
88 final String namefield = jParser.getCurrentName();
89 jParser.nextToken(); // move to value
90 if ("message".equals(namefield)) {
91 currentWeatherData.getSys().setMessage(jParser.getDoubleValue());
93 if ("country".equals(namefield)) {
94 currentWeatherData.getSys().setCountry(jParser.getValueAsString());
96 if ("sunrise".equals(namefield)) {
97 currentWeatherData.getSys().setSunrise(jParser.getValueAsLong());
99 if ("sunset".equals(namefield)) {
100 currentWeatherData.getSys().setSunset(jParser.getValueAsLong());
104 if ("weather".equals(fieldname)) {
105 final Weather weather = new Weather();
106 currentWeatherData.getWeather().add(weather);
107 while (jParser.nextToken() != JsonToken.END_OBJECT) {
108 final String namefield = jParser.getCurrentName();
109 jParser.nextToken(); // move to value
110 if ("id".equals(namefield)) {
111 weather.setId(jParser.getIntValue());
113 if ("main".equals(namefield)) {
114 weather.setMain(jParser.getText());
116 if ("description".equals(namefield)) {
117 weather.setDescription(jParser.getText());
119 if ("icon".equals(namefield)) {
120 weather.setIcon(jParser.getText());
125 if ("base".equals(fieldname)) {
126 currentWeatherData.setBase(jParser.getText());
128 if ("main".equals(fieldname)) {
129 while (jParser.nextToken() != JsonToken.END_OBJECT) {
130 final String namefield = jParser.getCurrentName();
131 jParser.nextToken(); // move to value
132 if ("temp".equals(namefield)) {
133 currentWeatherData.getMain().setTemp(jParser.getDoubleValue());
135 if ("temp_min".equals(namefield)) {
136 currentWeatherData.getMain().setTemp_min(jParser.getDoubleValue());
138 if ("temp_max".equals(namefield)) {
139 currentWeatherData.getMain().setTemp_max(jParser.getDoubleValue());
141 if ("pressure".equals(namefield)) {
142 currentWeatherData.getMain().setPressure(jParser.getDoubleValue());
144 if ("sea_level".equals(namefield)) {
145 currentWeatherData.getMain().setSea_level(jParser.getDoubleValue());
147 if ("grnd_level".equals(namefield)) {
148 currentWeatherData.getMain().setGrnd_level(jParser.getDoubleValue());
150 if ("humidity".equals(namefield)) {
151 currentWeatherData.getMain().setHumidity(jParser.getDoubleValue());
155 if ("wind".equals(fieldname)) {
156 while (jParser.nextToken() != JsonToken.END_OBJECT) {
157 final String namefield = jParser.getCurrentName();
158 jParser.nextToken(); // move to value
159 if ("speed".equals(namefield)) {
160 currentWeatherData.getWind().setSpeed(jParser.getDoubleValue());
162 if ("deg".equals(namefield)) {
163 currentWeatherData.getWind().setDeg(jParser.getDoubleValue());
167 if ("clouds".equals(fieldname)) {
168 while (jParser.nextToken() != JsonToken.END_OBJECT) {
169 final String namefield = jParser.getCurrentName();
170 jParser.nextToken(); // move to value
171 if ("all".equals(namefield)) {
172 currentWeatherData.getClouds().setAll(jParser.getDoubleValue());
176 if ("dt".equals(fieldname)) {
177 currentWeatherData.setDt(jParser.getLongValue());
179 if ("rain".equals(fieldname)) {
180 while (jParser.nextToken() != JsonToken.END_OBJECT) {
181 final String namefield = jParser.getCurrentName();
182 jParser.nextToken(); // move to value
183 if ("3h".equals(namefield)) {
184 currentWeatherData.getRain().set3h(jParser.getDoubleValue());
188 if ("snow".equals(fieldname)) {
189 while (jParser.nextToken() != JsonToken.END_OBJECT) {
190 final String namefield = jParser.getCurrentName();
191 jParser.nextToken(); // move to value
192 if ("3h".equals(namefield)) {
193 currentWeatherData.getSnow().set3h(jParser.getDoubleValue());
197 if ("id".equals(fieldname)) {
198 currentWeatherData.setId(jParser.getLongValue());
200 if ("name".equals(fieldname)) {
201 currentWeatherData.setName(jParser.getText());
203 if ("cod".equals(fieldname)) {
204 currentWeatherData.setCod(jParser.getIntValue());