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.currentweather.Clouds;
27 import name.gumartinm.weather.information.model.currentweather.Coord;
28 import name.gumartinm.weather.information.model.currentweather.Current;
29 import name.gumartinm.weather.information.model.currentweather.Main;
30 import name.gumartinm.weather.information.model.currentweather.Rain;
31 import name.gumartinm.weather.information.model.currentweather.Snow;
32 import name.gumartinm.weather.information.model.currentweather.Sys;
33 import name.gumartinm.weather.information.model.currentweather.Weather;
34 import name.gumartinm.weather.information.model.currentweather.Wind;
36 public class JPOSCurrentParser {
38 public Current retrieveCurrenFromJPOS(final String jsonData)
39 throws JsonParseException, IOException {
40 final JsonFactory f = new JsonFactory();
42 final Current currentWeatherData = new Current();
43 currentWeatherData.setClouds(new Clouds());
44 currentWeatherData.setCoord(new Coord());
45 currentWeatherData.setMain(new Main());
46 currentWeatherData.setRain(new Rain());
47 currentWeatherData.setSys(new Sys());
48 currentWeatherData.setSnow(new Snow());
50 .setWeather(new ArrayList<Weather>());
51 currentWeatherData.setWind(new Wind());
52 final JsonParser jParser = f.createParser(jsonData);
54 this.getCurrentWeatherData(currentWeatherData, jParser);
56 return currentWeatherData;
59 private void getCurrentWeatherData(final Current currentWeatherData,
60 final JsonParser jParser) throws JsonParseException, IOException {
61 if (jParser.nextToken() == JsonToken.START_OBJECT) {
63 while (jParser.nextToken() != JsonToken.END_OBJECT) {
64 final String fieldname = jParser.getCurrentName();
65 final JsonToken nextToken = jParser.nextToken();
66 if (nextToken == JsonToken.START_OBJECT) {
67 this.getCurrentWeatherDataObjects(currentWeatherData, jParser, fieldname);
69 if (nextToken == JsonToken.START_ARRAY) {
70 JsonToken tokenNext = jParser.nextToken();
71 while (tokenNext != JsonToken.END_ARRAY) {
72 if (tokenNext == JsonToken.START_OBJECT) {
73 this.getCurrentWeatherDataObjects(currentWeatherData, jParser, fieldname);
75 tokenNext = jParser.nextToken();
78 if ((nextToken == JsonToken.VALUE_NUMBER_INT)
79 || (nextToken == JsonToken.VALUE_STRING)) {
80 this.getCurrentWeatherDataObjects(currentWeatherData, jParser, fieldname);
86 private void getCurrentWeatherDataObjects(final Current currentWeatherData,
87 final JsonParser jParser, final String fieldname) throws JsonParseException,
89 if ("coord".equals(fieldname)) {
90 while (jParser.nextToken() != JsonToken.END_OBJECT) {
91 final String namefield = jParser.getCurrentName();
92 jParser.nextToken(); // move to value
93 if ("lon".equals(namefield)) {
94 currentWeatherData.getCoord().setLon(jParser.getDoubleValue());
96 if ("lat".equals(namefield)) {
97 currentWeatherData.getCoord().setLat(jParser.getDoubleValue());
101 if ("sys".equals(fieldname)) {
102 while (jParser.nextToken() != JsonToken.END_OBJECT) {
103 final String namefield = jParser.getCurrentName();
104 jParser.nextToken(); // move to value
105 if ("message".equals(namefield)) {
106 currentWeatherData.getSys().setMessage(jParser.getDoubleValue());
108 if ("country".equals(namefield)) {
109 currentWeatherData.getSys().setCountry(jParser.getValueAsString());
111 if ("sunrise".equals(namefield)) {
112 currentWeatherData.getSys().setSunrise(jParser.getValueAsLong());
114 if ("sunset".equals(namefield)) {
115 currentWeatherData.getSys().setSunset(jParser.getValueAsLong());
119 if ("weather".equals(fieldname)) {
120 final Weather weather = new Weather();
121 currentWeatherData.getWeather().add(weather);
122 while (jParser.nextToken() != JsonToken.END_OBJECT) {
123 final String namefield = jParser.getCurrentName();
124 jParser.nextToken(); // move to value
125 if ("id".equals(namefield)) {
126 weather.setId(jParser.getIntValue());
128 if ("main".equals(namefield)) {
129 weather.setMain(jParser.getText());
131 if ("description".equals(namefield)) {
132 weather.setDescription(jParser.getText());
134 if ("icon".equals(namefield)) {
135 weather.setIcon(jParser.getText());
140 if ("base".equals(fieldname)) {
141 currentWeatherData.setBase(jParser.getText());
143 if ("main".equals(fieldname)) {
144 while (jParser.nextToken() != JsonToken.END_OBJECT) {
145 final String namefield = jParser.getCurrentName();
146 jParser.nextToken(); // move to value
147 if ("temp".equals(namefield)) {
148 currentWeatherData.getMain().setTemp(jParser.getDoubleValue());
150 if ("temp_min".equals(namefield)) {
151 currentWeatherData.getMain().setTemp_min(jParser.getDoubleValue());
153 if ("temp_max".equals(namefield)) {
154 currentWeatherData.getMain().setTemp_max(jParser.getDoubleValue());
156 if ("pressure".equals(namefield)) {
157 currentWeatherData.getMain().setPressure(jParser.getDoubleValue());
159 if ("sea_level".equals(namefield)) {
160 currentWeatherData.getMain().setSea_level(jParser.getDoubleValue());
162 if ("grnd_level".equals(namefield)) {
163 currentWeatherData.getMain().setGrnd_level(jParser.getDoubleValue());
165 if ("humidity".equals(namefield)) {
166 currentWeatherData.getMain().setHumidity(jParser.getDoubleValue());
170 if ("wind".equals(fieldname)) {
171 while (jParser.nextToken() != JsonToken.END_OBJECT) {
172 final String namefield = jParser.getCurrentName();
173 jParser.nextToken(); // move to value
174 if ("speed".equals(namefield)) {
175 currentWeatherData.getWind().setSpeed(jParser.getDoubleValue());
177 if ("deg".equals(namefield)) {
178 currentWeatherData.getWind().setDeg(jParser.getDoubleValue());
182 if ("clouds".equals(fieldname)) {
183 while (jParser.nextToken() != JsonToken.END_OBJECT) {
184 final String namefield = jParser.getCurrentName();
185 jParser.nextToken(); // move to value
186 if ("all".equals(namefield)) {
187 currentWeatherData.getClouds().setAll(jParser.getDoubleValue());
191 if ("dt".equals(fieldname)) {
192 currentWeatherData.setDt(jParser.getLongValue());
194 if ("rain".equals(fieldname)) {
195 while (jParser.nextToken() != JsonToken.END_OBJECT) {
196 final String namefield = jParser.getCurrentName();
197 jParser.nextToken(); // move to value
198 if ("3h".equals(namefield)) {
199 currentWeatherData.getRain().set3h(jParser.getDoubleValue());
203 if ("snow".equals(fieldname)) {
204 while (jParser.nextToken() != JsonToken.END_OBJECT) {
205 final String namefield = jParser.getCurrentName();
206 jParser.nextToken(); // move to value
207 if ("3h".equals(namefield)) {
208 currentWeatherData.getSnow().set3h(jParser.getDoubleValue());
212 if ("id".equals(fieldname)) {
213 currentWeatherData.setId(jParser.getLongValue());
215 if ("name".equals(fieldname)) {
216 currentWeatherData.setName(jParser.getText());
218 if ("cod".equals(fieldname)) {
219 currentWeatherData.setCod(jParser.getIntValue());