From 20eb79c6aa0c0ec8ea28d7eb972eaac6e3a6b250 Mon Sep 17 00:00:00 2001 From: "gu.martinm@gmail.com" Date: Sun, 13 Apr 2014 22:15:27 +0200 Subject: [PATCH] Jackson: databinding and streaming for fun --- .../main/java/de/example/jackson/auto/Clouds.java | 13 - .../main/java/de/example/jackson/auto/Coord.java | 20 - .../jackson/auto/JacksonDataBindingTestMain.java | 76 --- .../main/java/de/example/jackson/auto/Main.java | 55 --- .../src/main/java/de/example/jackson/auto/Sys.java | 34 -- .../main/java/de/example/jackson/auto/Weather.java | 34 -- .../java/de/example/jackson/auto/WeatherData.java | 84 ---- .../main/java/de/example/jackson/auto/Wind.java | 20 - .../jackson/auto/currentweather/Clouds.java | 22 + .../example/jackson/auto/currentweather/Coord.java | 30 ++ .../auto/currentweather/CurrentWeatherData.java | 117 +++++ .../example/jackson/auto/currentweather/Main.java | 73 +++ .../example/jackson/auto/currentweather/Rain.java | 23 + .../example/jackson/auto/currentweather/Snow.java | 23 + .../example/jackson/auto/currentweather/Sys.java | 46 ++ .../jackson/auto/currentweather/Weather.java | 45 ++ .../example/jackson/auto/currentweather/Wind.java | 30 ++ .../de/example/jackson/auto/forecast/City.java | 53 +++ .../de/example/jackson/auto/forecast/Coord.java | 30 ++ .../jackson/auto/forecast/ForecastWeatherData.java | 61 +++ .../de/example/jackson/auto/forecast/List.java | 93 ++++ .../de/example/jackson/auto/forecast/Temp.java | 61 +++ .../de/example/jackson/auto/forecast/Weather.java | 45 ++ .../java/de/example/jackson/databinding/Coord.java | 24 - .../databinding/JacksonDataBindingTestMain.java | 162 ++++++- .../de/example/jackson/databinding/Weather.java | 39 -- .../jackson/databinding/WeatherDataPOJO.java | 35 -- .../java/de/example/jackson/databinding/Wind.java | 55 --- .../streaming/JacksonStreamingTestMain.java | 512 +++++++++++++++++++++ 29 files changed, 1410 insertions(+), 505 deletions(-) delete mode 100644 Allgemeines/Jackson/src/main/java/de/example/jackson/auto/Clouds.java delete mode 100644 Allgemeines/Jackson/src/main/java/de/example/jackson/auto/Coord.java delete mode 100644 Allgemeines/Jackson/src/main/java/de/example/jackson/auto/JacksonDataBindingTestMain.java delete mode 100644 Allgemeines/Jackson/src/main/java/de/example/jackson/auto/Main.java delete mode 100644 Allgemeines/Jackson/src/main/java/de/example/jackson/auto/Sys.java delete mode 100644 Allgemeines/Jackson/src/main/java/de/example/jackson/auto/Weather.java delete mode 100644 Allgemeines/Jackson/src/main/java/de/example/jackson/auto/WeatherData.java delete mode 100644 Allgemeines/Jackson/src/main/java/de/example/jackson/auto/Wind.java create mode 100644 Allgemeines/Jackson/src/main/java/de/example/jackson/auto/currentweather/Clouds.java create mode 100644 Allgemeines/Jackson/src/main/java/de/example/jackson/auto/currentweather/Coord.java create mode 100644 Allgemeines/Jackson/src/main/java/de/example/jackson/auto/currentweather/CurrentWeatherData.java create mode 100644 Allgemeines/Jackson/src/main/java/de/example/jackson/auto/currentweather/Main.java create mode 100644 Allgemeines/Jackson/src/main/java/de/example/jackson/auto/currentweather/Rain.java create mode 100644 Allgemeines/Jackson/src/main/java/de/example/jackson/auto/currentweather/Snow.java create mode 100644 Allgemeines/Jackson/src/main/java/de/example/jackson/auto/currentweather/Sys.java create mode 100644 Allgemeines/Jackson/src/main/java/de/example/jackson/auto/currentweather/Weather.java create mode 100644 Allgemeines/Jackson/src/main/java/de/example/jackson/auto/currentweather/Wind.java create mode 100644 Allgemeines/Jackson/src/main/java/de/example/jackson/auto/forecast/City.java create mode 100644 Allgemeines/Jackson/src/main/java/de/example/jackson/auto/forecast/Coord.java create mode 100644 Allgemeines/Jackson/src/main/java/de/example/jackson/auto/forecast/ForecastWeatherData.java create mode 100644 Allgemeines/Jackson/src/main/java/de/example/jackson/auto/forecast/List.java create mode 100644 Allgemeines/Jackson/src/main/java/de/example/jackson/auto/forecast/Temp.java create mode 100644 Allgemeines/Jackson/src/main/java/de/example/jackson/auto/forecast/Weather.java delete mode 100644 Allgemeines/Jackson/src/main/java/de/example/jackson/databinding/Coord.java delete mode 100644 Allgemeines/Jackson/src/main/java/de/example/jackson/databinding/Weather.java delete mode 100644 Allgemeines/Jackson/src/main/java/de/example/jackson/databinding/WeatherDataPOJO.java delete mode 100644 Allgemeines/Jackson/src/main/java/de/example/jackson/databinding/Wind.java create mode 100644 Allgemeines/Jackson/src/main/java/de/example/jackson/streaming/JacksonStreamingTestMain.java diff --git a/Allgemeines/Jackson/src/main/java/de/example/jackson/auto/Clouds.java b/Allgemeines/Jackson/src/main/java/de/example/jackson/auto/Clouds.java deleted file mode 100644 index c762e80..0000000 --- a/Allgemeines/Jackson/src/main/java/de/example/jackson/auto/Clouds.java +++ /dev/null @@ -1,13 +0,0 @@ -package de.example.jackson.auto; - - -public class Clouds{ - private Number all; - - public Number getAll(){ - return this.all; - } - public void setAll(final Number all){ - this.all = all; - } -} diff --git a/Allgemeines/Jackson/src/main/java/de/example/jackson/auto/Coord.java b/Allgemeines/Jackson/src/main/java/de/example/jackson/auto/Coord.java deleted file mode 100644 index 193b964..0000000 --- a/Allgemeines/Jackson/src/main/java/de/example/jackson/auto/Coord.java +++ /dev/null @@ -1,20 +0,0 @@ -package de.example.jackson.auto; - - -public class Coord{ - private Number lat; - private Number lon; - - public Number getLat(){ - return this.lat; - } - public void setLat(final Number lat){ - this.lat = lat; - } - public Number getLon(){ - return this.lon; - } - public void setLon(final Number lon){ - this.lon = lon; - } -} diff --git a/Allgemeines/Jackson/src/main/java/de/example/jackson/auto/JacksonDataBindingTestMain.java b/Allgemeines/Jackson/src/main/java/de/example/jackson/auto/JacksonDataBindingTestMain.java deleted file mode 100644 index 6a6be41..0000000 --- a/Allgemeines/Jackson/src/main/java/de/example/jackson/auto/JacksonDataBindingTestMain.java +++ /dev/null @@ -1,76 +0,0 @@ -package de.example.jackson.auto; - -import java.io.IOException; -import java.io.UnsupportedEncodingException; - -import com.fasterxml.jackson.core.JsonGenerationException; -import com.fasterxml.jackson.core.JsonParseException; -import com.fasterxml.jackson.databind.JsonMappingException; -import com.fasterxml.jackson.databind.ObjectMapper; - -public class JacksonDataBindingTestMain { - - public static void main(final String[] args) { - - final String dataA = - "{" - + "\"coord\":{\"lon\":139,\"lat\":35}," - + "\"sys\":{\"message\":5.2147,\"country\":\"JP\",\"sunrise\":1397161018,\"sunset\":1397207585}," - + "\"weather\":[{\"id\":800,\"main\":\"Clear\",\"description\":\"Sky is Clear\",\"icon\":\"01n\"}]," - + "\"base\":\"cmc stations\"," - + "\"main\":{\"temp\":273.275,\"temp_min\":273.275,\"temp_max\":273.275,\"pressure\":988.56,\"sea_level\":1033.79,\"grnd_level\":988.56,\"humidity\":95}," - + "\"wind\":{\"speed\":1.11,\"deg\":64.5043}," - + "\"clouds\":{\"all\":0}," - + "\"dt\":1397227133," - + "\"id\":1851632," - + "\"name\":\"Shuzenji\"," - + "\"cod\":200" - + "}"; - - final String dataB = - "{" - + "\"coord\":{\"lon\":139,\"lat\":35}," - + "\"wind\":{\"speed\":1.11,\"deg\":64.5043}," - + "\"name\":\"Shuzenji\"," - + "\"cod\":200" - + "}"; - final ObjectMapper mapper = new ObjectMapper(); - - // Unmarshalling: - WeatherData weatherDataPOJO = null; - try { - weatherDataPOJO = mapper.readValue(dataB.getBytes("UTF-8"), WeatherData.class); - } catch (final JsonParseException e) { - e.printStackTrace(); - } catch (final JsonMappingException e) { - e.printStackTrace(); - } catch (final UnsupportedEncodingException e) { - e.printStackTrace(); - } catch (final IOException e) { - e.printStackTrace(); - } - - if (weatherDataPOJO != null) { - System.out.println("coord: " + weatherDataPOJO.getCoord().getLat() + " " + weatherDataPOJO.getCoord().getLon()); - System.out.println("wind: " + weatherDataPOJO.getWind().getSpeed() + " " + weatherDataPOJO.getWind().getDeg()); - System.out.println("name: " + weatherDataPOJO.getName()); - System.out.println("cod: " + weatherDataPOJO.getCod()); - } - - // Marshalling: - try { - final String dataBMarshalling = mapper.writeValueAsString(weatherDataPOJO); - System.out.println(dataBMarshalling); - } catch (final JsonGenerationException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } catch (final JsonMappingException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } catch (final IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - } - -} diff --git a/Allgemeines/Jackson/src/main/java/de/example/jackson/auto/Main.java b/Allgemeines/Jackson/src/main/java/de/example/jackson/auto/Main.java deleted file mode 100644 index 547e6e6..0000000 --- a/Allgemeines/Jackson/src/main/java/de/example/jackson/auto/Main.java +++ /dev/null @@ -1,55 +0,0 @@ -package de.example.jackson.auto; - - -public class Main{ - private Number grnd_level; - private Number humidity; - private Number pressure; - private Number sea_level; - private Number temp; - private Number temp_max; - private Number temp_min; - - public Number getGrnd_level(){ - return this.grnd_level; - } - public void setGrnd_level(final Number grnd_level){ - this.grnd_level = grnd_level; - } - public Number getHumidity(){ - return this.humidity; - } - public void setHumidity(final Number humidity){ - this.humidity = humidity; - } - public Number getPressure(){ - return this.pressure; - } - public void setPressure(final Number pressure){ - this.pressure = pressure; - } - public Number getSea_level(){ - return this.sea_level; - } - public void setSea_level(final Number sea_level){ - this.sea_level = sea_level; - } - public Number getTemp(){ - return this.temp; - } - public void setTemp(final Number temp){ - this.temp = temp; - } - public Number getTemp_max(){ - return this.temp_max; - } - public void setTemp_max(final Number temp_max){ - this.temp_max = temp_max; - } - public Number getTemp_min(){ - return this.temp_min; - } - public void setTemp_min(final Number temp_min){ - this.temp_min = temp_min; - } -} diff --git a/Allgemeines/Jackson/src/main/java/de/example/jackson/auto/Sys.java b/Allgemeines/Jackson/src/main/java/de/example/jackson/auto/Sys.java deleted file mode 100644 index fe008cc..0000000 --- a/Allgemeines/Jackson/src/main/java/de/example/jackson/auto/Sys.java +++ /dev/null @@ -1,34 +0,0 @@ -package de.example.jackson.auto; - - -public class Sys{ - private String country; - private Number message; - private Number sunrise; - private Number sunset; - - public String getCountry(){ - return this.country; - } - public void setCountry(final String country){ - this.country = country; - } - public Number getMessage(){ - return this.message; - } - public void setMessage(final Number message){ - this.message = message; - } - public Number getSunrise(){ - return this.sunrise; - } - public void setSunrise(final Number sunrise){ - this.sunrise = sunrise; - } - public Number getSunset(){ - return this.sunset; - } - public void setSunset(final Number sunset){ - this.sunset = sunset; - } -} diff --git a/Allgemeines/Jackson/src/main/java/de/example/jackson/auto/Weather.java b/Allgemeines/Jackson/src/main/java/de/example/jackson/auto/Weather.java deleted file mode 100644 index 6193caa..0000000 --- a/Allgemeines/Jackson/src/main/java/de/example/jackson/auto/Weather.java +++ /dev/null @@ -1,34 +0,0 @@ -package de.example.jackson.auto; - - -public class Weather{ - private String description; - private String icon; - private Number id; - private String main; - - public String getDescription(){ - return this.description; - } - public void setDescription(final String description){ - this.description = description; - } - public String getIcon(){ - return this.icon; - } - public void setIcon(final String icon){ - this.icon = icon; - } - public Number getId(){ - return this.id; - } - public void setId(final Number id){ - this.id = id; - } - public String getMain(){ - return this.main; - } - public void setMain(final String main){ - this.main = main; - } -} diff --git a/Allgemeines/Jackson/src/main/java/de/example/jackson/auto/WeatherData.java b/Allgemeines/Jackson/src/main/java/de/example/jackson/auto/WeatherData.java deleted file mode 100644 index 79ddd9b..0000000 --- a/Allgemeines/Jackson/src/main/java/de/example/jackson/auto/WeatherData.java +++ /dev/null @@ -1,84 +0,0 @@ -package de.example.jackson.auto; - -import java.util.List; - -public class WeatherData{ - private String base; - private Clouds clouds; - private Number cod; - private Coord coord; - private Number dt; - private Number id; - private Main main; - private String name; - private Sys sys; - private List weather; - private Wind wind; - - public String getBase(){ - return this.base; - } - public void setBase(final String base){ - this.base = base; - } - public Clouds getClouds(){ - return this.clouds; - } - public void setClouds(final Clouds clouds){ - this.clouds = clouds; - } - public Number getCod(){ - return this.cod; - } - public void setCod(final Number cod){ - this.cod = cod; - } - public Coord getCoord(){ - return this.coord; - } - public void setCoord(final Coord coord){ - this.coord = coord; - } - public Number getDt(){ - return this.dt; - } - public void setDt(final Number dt){ - this.dt = dt; - } - public Number getId(){ - return this.id; - } - public void setId(final Number id){ - this.id = id; - } - public Main getMain(){ - return this.main; - } - public void setMain(final Main main){ - this.main = main; - } - public String getName(){ - return this.name; - } - public void setName(final String name){ - this.name = name; - } - public Sys getSys(){ - return this.sys; - } - public void setSys(final Sys sys){ - this.sys = sys; - } - public List getWeather(){ - return this.weather; - } - public void setWeather(final List weather){ - this.weather = weather; - } - public Wind getWind(){ - return this.wind; - } - public void setWind(final Wind wind){ - this.wind = wind; - } -} diff --git a/Allgemeines/Jackson/src/main/java/de/example/jackson/auto/Wind.java b/Allgemeines/Jackson/src/main/java/de/example/jackson/auto/Wind.java deleted file mode 100644 index e821612..0000000 --- a/Allgemeines/Jackson/src/main/java/de/example/jackson/auto/Wind.java +++ /dev/null @@ -1,20 +0,0 @@ -package de.example.jackson.auto; - - -public class Wind{ - private Number deg; - private Number speed; - - public Number getDeg(){ - return this.deg; - } - public void setDeg(final Number deg){ - this.deg = deg; - } - public Number getSpeed(){ - return this.speed; - } - public void setSpeed(final Number speed){ - this.speed = speed; - } -} diff --git a/Allgemeines/Jackson/src/main/java/de/example/jackson/auto/currentweather/Clouds.java b/Allgemeines/Jackson/src/main/java/de/example/jackson/auto/currentweather/Clouds.java new file mode 100644 index 0000000..fbe8bec --- /dev/null +++ b/Allgemeines/Jackson/src/main/java/de/example/jackson/auto/currentweather/Clouds.java @@ -0,0 +1,22 @@ +package de.example.jackson.auto.currentweather; + +import java.io.Serializable; + +public class Clouds implements Serializable { + private static final long serialVersionUID = 3034435739326030899L; + private Number all; + + public Number getAll(){ + return this.all; + } + public void setAll(final Number all){ + this.all = all; + } + + @Override + public String toString() { + final StringBuilder builder = new StringBuilder(); + builder.append("Clouds [all=").append(this.all).append("]"); + return builder.toString(); + } +} diff --git a/Allgemeines/Jackson/src/main/java/de/example/jackson/auto/currentweather/Coord.java b/Allgemeines/Jackson/src/main/java/de/example/jackson/auto/currentweather/Coord.java new file mode 100644 index 0000000..2ac9843 --- /dev/null +++ b/Allgemeines/Jackson/src/main/java/de/example/jackson/auto/currentweather/Coord.java @@ -0,0 +1,30 @@ +package de.example.jackson.auto.currentweather; + +import java.io.Serializable; + +public class Coord implements Serializable { + private static final long serialVersionUID = 7151637605146377486L; + private Number lat; + private Number lon; + + public Number getLat(){ + return this.lat; + } + public void setLat(final Number lat){ + this.lat = lat; + } + public Number getLon(){ + return this.lon; + } + public void setLon(final Number lon){ + this.lon = lon; + } + + @Override + public String toString() { + final StringBuilder builder = new StringBuilder(); + builder.append("Coord [lat=").append(this.lat).append(", lon=").append(this.lon) + .append("]"); + return builder.toString(); + } +} diff --git a/Allgemeines/Jackson/src/main/java/de/example/jackson/auto/currentweather/CurrentWeatherData.java b/Allgemeines/Jackson/src/main/java/de/example/jackson/auto/currentweather/CurrentWeatherData.java new file mode 100644 index 0000000..f815fa4 --- /dev/null +++ b/Allgemeines/Jackson/src/main/java/de/example/jackson/auto/currentweather/CurrentWeatherData.java @@ -0,0 +1,117 @@ +package de.example.jackson.auto.currentweather; + +import java.io.Serializable; +import java.util.List; + +/** + * Auto generated by: http://jsongen.byingtondesign.com/ + * + */ +public class CurrentWeatherData implements Serializable { + private static final long serialVersionUID = -5325322670137670148L; + private String base; + private Clouds clouds; + private Number cod; + private Coord coord; + private Number dt; + private Number id; + private Main main; + private String name; + private Rain rain; + private Snow snow; + private Sys sys; + private List weather; + private Wind wind; + + public String getBase(){ + return this.base; + } + public void setBase(final String base){ + this.base = base; + } + public Clouds getClouds(){ + return this.clouds; + } + public void setClouds(final Clouds clouds){ + this.clouds = clouds; + } + public Number getCod(){ + return this.cod; + } + public void setCod(final Number cod){ + this.cod = cod; + } + public Coord getCoord(){ + return this.coord; + } + public void setCoord(final Coord coord){ + this.coord = coord; + } + public Number getDt(){ + return this.dt; + } + public void setDt(final Number dt){ + this.dt = dt; + } + public Number getId(){ + return this.id; + } + public void setId(final Number id){ + this.id = id; + } + public Main getMain(){ + return this.main; + } + public void setMain(final Main main){ + this.main = main; + } + public String getName(){ + return this.name; + } + public void setName(final String name){ + this.name = name; + } + public Rain getRain(){ + return this.rain; + } + public void setRain(final Rain rain){ + this.rain = rain; + } + public Snow getSnow() { + return this.snow; + } + public void setSnow(final Snow snow) { + this.snow = snow; + } + public Sys getSys(){ + return this.sys; + } + public void setSys(final Sys sys){ + this.sys = sys; + } + public List getWeather(){ + return this.weather; + } + public void setWeather(final List weather){ + this.weather = weather; + } + public Wind getWind(){ + return this.wind; + } + public void setWind(final Wind wind){ + this.wind = wind; + } + + @Override + public String toString() { + final StringBuilder builder = new StringBuilder(); + builder.append("CurrentWeatherData [base=").append(this.base).append(", clouds=") + .append(this.clouds).append(", cod=").append(this.cod).append(", coord=") + .append(this.coord).append(", dt=").append(this.dt).append(", id=").append(this.id) + .append(", main=").append(this.main).append(", name=").append(this.name) + .append(", rain=").append(this.rain).append(", snow=").append(this.snow) + .append(", sys=").append(this.sys).append(", weather=").append(this.weather) + .append(", wind=").append(this.wind).append("]"); + return builder.toString(); + } +} diff --git a/Allgemeines/Jackson/src/main/java/de/example/jackson/auto/currentweather/Main.java b/Allgemeines/Jackson/src/main/java/de/example/jackson/auto/currentweather/Main.java new file mode 100644 index 0000000..9a0dc86 --- /dev/null +++ b/Allgemeines/Jackson/src/main/java/de/example/jackson/auto/currentweather/Main.java @@ -0,0 +1,73 @@ +package de.example.jackson.auto.currentweather; + +import java.io.Serializable; + +public class Main implements Serializable { + private static final long serialVersionUID = -6000879164436289447L; + private Number grnd_level; + private Number humidity; + private Number pressure; + private Number sea_level; + private Number temp; + private Number temp_max; + private Number temp_min; + + public Number getGrnd_level() { + return this.grnd_level; + } + + public void setGrnd_level(final Number grnd_level) { + this.grnd_level = grnd_level; + } + + public Number getHumidity(){ + return this.humidity; + } + public void setHumidity(final Number humidity){ + this.humidity = humidity; + } + public Number getPressure(){ + return this.pressure; + } + public void setPressure(final Number pressure){ + this.pressure = pressure; + } + + public Number getSea_level() { + return this.sea_level; + } + + public void setSea_level(final Number sea_level) { + this.sea_level = sea_level; + } + + public Number getTemp(){ + return this.temp; + } + public void setTemp(final Number temp){ + this.temp = temp; + } + public Number getTemp_max(){ + return this.temp_max; + } + public void setTemp_max(final Number temp_max){ + this.temp_max = temp_max; + } + public Number getTemp_min(){ + return this.temp_min; + } + public void setTemp_min(final Number temp_min){ + this.temp_min = temp_min; + } + + @Override + public String toString() { + final StringBuilder builder = new StringBuilder(); + builder.append("Main [grnd_level=").append(this.grnd_level).append(", humidity=") + .append(this.humidity).append(", pressure=").append(this.pressure) + .append(", sea_level=").append(this.sea_level).append(", temp=").append(this.temp) + .append(", temp_max=").append(this.temp_max).append(", temp_min=") + .append(this.temp_min).append("]"); + return builder.toString(); + } +} diff --git a/Allgemeines/Jackson/src/main/java/de/example/jackson/auto/currentweather/Rain.java b/Allgemeines/Jackson/src/main/java/de/example/jackson/auto/currentweather/Rain.java new file mode 100644 index 0000000..1bca184 --- /dev/null +++ b/Allgemeines/Jackson/src/main/java/de/example/jackson/auto/currentweather/Rain.java @@ -0,0 +1,23 @@ +package de.example.jackson.auto.currentweather; + +import java.io.Serializable; + +public class Rain implements Serializable { + private static final long serialVersionUID = 1318464783605029435L; + private Number three; + + public Number get3h(){ + return this.three; + } + + public void set3h(final Number threeh) { + this.three = threeh; + } + + @Override + public String toString() { + final StringBuilder builder = new StringBuilder(); + builder.append("Rain [three=").append(this.three).append("]"); + return builder.toString(); + } +} diff --git a/Allgemeines/Jackson/src/main/java/de/example/jackson/auto/currentweather/Snow.java b/Allgemeines/Jackson/src/main/java/de/example/jackson/auto/currentweather/Snow.java new file mode 100644 index 0000000..412a842 --- /dev/null +++ b/Allgemeines/Jackson/src/main/java/de/example/jackson/auto/currentweather/Snow.java @@ -0,0 +1,23 @@ +package de.example.jackson.auto.currentweather; + +import java.io.Serializable; + +public class Snow implements Serializable { + private static final long serialVersionUID = 6769716772818311879L; + private Number three; + + public Number get3h() { + return this.three; + } + + public void set3h(final Number threeh) { + this.three = threeh; + } + + @Override + public String toString() { + final StringBuilder builder = new StringBuilder(); + builder.append("Snow [three=").append(this.three).append("]"); + return builder.toString(); + } +} diff --git a/Allgemeines/Jackson/src/main/java/de/example/jackson/auto/currentweather/Sys.java b/Allgemeines/Jackson/src/main/java/de/example/jackson/auto/currentweather/Sys.java new file mode 100644 index 0000000..5101b2a --- /dev/null +++ b/Allgemeines/Jackson/src/main/java/de/example/jackson/auto/currentweather/Sys.java @@ -0,0 +1,46 @@ +package de.example.jackson.auto.currentweather; + +import java.io.Serializable; + + +public class Sys implements Serializable { + private static final long serialVersionUID = 5333083785731053139L; + private String country; + private Number message; + private Number sunrise; + private Number sunset; + + public String getCountry(){ + return this.country; + } + public void setCountry(final String country){ + this.country = country; + } + public Number getMessage(){ + return this.message; + } + public void setMessage(final Number message){ + this.message = message; + } + public Number getSunrise(){ + return this.sunrise; + } + public void setSunrise(final Number sunrise){ + this.sunrise = sunrise; + } + public Number getSunset(){ + return this.sunset; + } + public void setSunset(final Number sunset){ + this.sunset = sunset; + } + + @Override + public String toString() { + final StringBuilder builder = new StringBuilder(); + builder.append("Sys [country=").append(this.country).append(", message=") + .append(this.message).append(", sunrise=").append(this.sunrise).append(", sunset=") + .append(this.sunset).append("]"); + return builder.toString(); + } +} diff --git a/Allgemeines/Jackson/src/main/java/de/example/jackson/auto/currentweather/Weather.java b/Allgemeines/Jackson/src/main/java/de/example/jackson/auto/currentweather/Weather.java new file mode 100644 index 0000000..00eb828 --- /dev/null +++ b/Allgemeines/Jackson/src/main/java/de/example/jackson/auto/currentweather/Weather.java @@ -0,0 +1,45 @@ +package de.example.jackson.auto.currentweather; + +import java.io.Serializable; + +public class Weather implements Serializable { + private static final long serialVersionUID = -34336548786316655L; + private String description; + private String icon; + private Number id; + private String main; + + public String getDescription(){ + return this.description; + } + public void setDescription(final String description){ + this.description = description; + } + public String getIcon(){ + return this.icon; + } + public void setIcon(final String icon){ + this.icon = icon; + } + public Number getId(){ + return this.id; + } + public void setId(final Number id){ + this.id = id; + } + public String getMain(){ + return this.main; + } + public void setMain(final String main){ + this.main = main; + } + + @Override + public String toString() { + final StringBuilder builder = new StringBuilder(); + builder.append("Weather [description=").append(this.description).append(", icon=") + .append(this.icon).append(", id=").append(this.id).append(", main=") + .append(this.main).append("]"); + return builder.toString(); + } +} diff --git a/Allgemeines/Jackson/src/main/java/de/example/jackson/auto/currentweather/Wind.java b/Allgemeines/Jackson/src/main/java/de/example/jackson/auto/currentweather/Wind.java new file mode 100644 index 0000000..593d5ff --- /dev/null +++ b/Allgemeines/Jackson/src/main/java/de/example/jackson/auto/currentweather/Wind.java @@ -0,0 +1,30 @@ +package de.example.jackson.auto.currentweather; + +import java.io.Serializable; + +public class Wind implements Serializable { + private static final long serialVersionUID = 5495842422633674631L; + private Number deg; + private Number speed; + + public Number getDeg(){ + return this.deg; + } + public void setDeg(final Number deg){ + this.deg = deg; + } + public Number getSpeed(){ + return this.speed; + } + public void setSpeed(final Number speed){ + this.speed = speed; + } + + @Override + public String toString() { + final StringBuilder builder = new StringBuilder(); + builder.append("Wind [deg=").append(this.deg).append(", speed=").append(this.speed) + .append("]"); + return builder.toString(); + } +} diff --git a/Allgemeines/Jackson/src/main/java/de/example/jackson/auto/forecast/City.java b/Allgemeines/Jackson/src/main/java/de/example/jackson/auto/forecast/City.java new file mode 100644 index 0000000..e0f8186 --- /dev/null +++ b/Allgemeines/Jackson/src/main/java/de/example/jackson/auto/forecast/City.java @@ -0,0 +1,53 @@ +package de.example.jackson.auto.forecast; + +import java.io.Serializable; + + +public class City implements Serializable { + private static final long serialVersionUID = 3079687975077030704L; + private Coord coord; + private String country; + private Number id; + private String name; + private Number population; + + public Coord getCoord(){ + return this.coord; + } + public void setCoord(final Coord coord){ + this.coord = coord; + } + public String getCountry(){ + return this.country; + } + public void setCountry(final String country){ + this.country = country; + } + public Number getId(){ + return this.id; + } + public void setId(final Number id){ + this.id = id; + } + public String getName(){ + return this.name; + } + public void setName(final String name){ + this.name = name; + } + public Number getPopulation(){ + return this.population; + } + public void setPopulation(final Number population){ + this.population = population; + } + + @Override + public String toString() { + final StringBuilder builder = new StringBuilder(); + builder.append("City [coord=").append(this.coord).append(", country=").append(this.country) + .append(", id=").append(this.id).append(", name=").append(this.name) + .append(", population=").append(this.population).append("]"); + return builder.toString(); + } +} diff --git a/Allgemeines/Jackson/src/main/java/de/example/jackson/auto/forecast/Coord.java b/Allgemeines/Jackson/src/main/java/de/example/jackson/auto/forecast/Coord.java new file mode 100644 index 0000000..6f084a7 --- /dev/null +++ b/Allgemeines/Jackson/src/main/java/de/example/jackson/auto/forecast/Coord.java @@ -0,0 +1,30 @@ +package de.example.jackson.auto.forecast; + +import java.io.Serializable; + +public class Coord implements Serializable { + private static final long serialVersionUID = 8069257976701986700L; + private Number lat; + private Number lon; + + public Number getLat(){ + return this.lat; + } + public void setLat(final Number lat){ + this.lat = lat; + } + public Number getLon(){ + return this.lon; + } + public void setLon(final Number lon){ + this.lon = lon; + } + + @Override + public String toString() { + final StringBuilder builder = new StringBuilder(); + builder.append("Coord [lat=").append(this.lat).append(", lon=").append(this.lon) + .append("]"); + return builder.toString(); + } +} diff --git a/Allgemeines/Jackson/src/main/java/de/example/jackson/auto/forecast/ForecastWeatherData.java b/Allgemeines/Jackson/src/main/java/de/example/jackson/auto/forecast/ForecastWeatherData.java new file mode 100644 index 0000000..0d1829e --- /dev/null +++ b/Allgemeines/Jackson/src/main/java/de/example/jackson/auto/forecast/ForecastWeatherData.java @@ -0,0 +1,61 @@ +package de.example.jackson.auto.forecast; + +import java.io.Serializable; +import java.util.List; + +/** + * Auto generated by: http://jsongen.byingtondesign.com/ + * + */ +public class ForecastWeatherData implements Serializable { + private static final long serialVersionUID = 5095443678019686190L; + private City city; + private Number cnt; + private Number cod; + private List list; + private Number message; + + public City getCity(){ + return this.city; + } + public void setCity(final City city){ + this.city = city; + } + public Number getCnt(){ + return this.cnt; + } + public void setCnt(final Number cnt){ + this.cnt = cnt; + } + + public Number getCod() { + return this.cod; + } + + public void setCod(final Number cod) { + this.cod = cod; + } + + public List getList() { + return this.list; + } + + public void setList(final List list) { + this.list = list; + } + public Number getMessage(){ + return this.message; + } + public void setMessage(final Number message){ + this.message = message; + } + + @Override + public String toString() { + final StringBuilder builder = new StringBuilder(); + builder.append("ForecastWeatherData [city=").append(this.city).append(", cnt=") + .append(this.cnt).append(", cod=").append(this.cod).append(", list=") + .append(this.list).append(", message=").append(this.message).append("]"); + return builder.toString(); + } +} diff --git a/Allgemeines/Jackson/src/main/java/de/example/jackson/auto/forecast/List.java b/Allgemeines/Jackson/src/main/java/de/example/jackson/auto/forecast/List.java new file mode 100644 index 0000000..0541f0a --- /dev/null +++ b/Allgemeines/Jackson/src/main/java/de/example/jackson/auto/forecast/List.java @@ -0,0 +1,93 @@ +package de.example.jackson.auto.forecast; + +import java.io.Serializable; + + +public class List implements Serializable { + private static final long serialVersionUID = 838468273188666785L; + private Number clouds; + private Number deg; + private Number dt; + private Number humidity; + private Number pressure; + private Number rain; + private Number snow; + private Number speed; + private Temp temp; + private java.util.List weather; + + public Number getClouds(){ + return this.clouds; + } + public void setClouds(final Number clouds){ + this.clouds = clouds; + } + public Number getDeg(){ + return this.deg; + } + public void setDeg(final Number deg){ + this.deg = deg; + } + public Number getDt(){ + return this.dt; + } + public void setDt(final Number dt){ + this.dt = dt; + } + public Number getHumidity(){ + return this.humidity; + } + public void setHumidity(final Number humidity){ + this.humidity = humidity; + } + public Number getPressure(){ + return this.pressure; + } + public void setPressure(final Number pressure){ + this.pressure = pressure; + } + public Number getRain(){ + return this.rain; + } + public void setRain(final Number rain){ + this.rain = rain; + } + public Number getSnow() { + return this.snow; + } + public void setSnow(final Number snow) { + this.snow = snow; + } + public Number getSpeed(){ + return this.speed; + } + public void setSpeed(final Number speed){ + this.speed = speed; + } + public Temp getTemp(){ + return this.temp; + } + public void setTemp(final Temp temp){ + this.temp = temp; + } + + public java.util.List getWeather() { + return this.weather; + } + + public void setWeather(final java.util.List weather) { + this.weather = weather; + } + + @Override + public String toString() { + final StringBuilder builder = new StringBuilder(); + builder.append("List [clouds=").append(this.clouds).append(", deg=").append(this.deg) + .append(", dt=").append(this.dt).append(", humidity=").append(this.humidity) + .append(", pressure=").append(this.pressure).append(", rain=").append(this.rain) + .append(", snow=").append(this.snow).append(", speed=").append(this.speed) + .append(", temp=").append(this.temp).append(", weather=").append(this.weather) + .append("]"); + return builder.toString(); + } +} diff --git a/Allgemeines/Jackson/src/main/java/de/example/jackson/auto/forecast/Temp.java b/Allgemeines/Jackson/src/main/java/de/example/jackson/auto/forecast/Temp.java new file mode 100644 index 0000000..adc126e --- /dev/null +++ b/Allgemeines/Jackson/src/main/java/de/example/jackson/auto/forecast/Temp.java @@ -0,0 +1,61 @@ + +package de.example.jackson.auto.forecast; + +import java.io.Serializable; + +public class Temp implements Serializable { + private static final long serialVersionUID = -7614799035018271127L; + private Number day; + private Number eve; + private Number max; + private Number min; + private Number morn; + private Number night; + + public Number getDay(){ + return this.day; + } + public void setDay(final Number day){ + this.day = day; + } + public Number getEve(){ + return this.eve; + } + public void setEve(final Number eve){ + this.eve = eve; + } + public Number getMax(){ + return this.max; + } + public void setMax(final Number max){ + this.max = max; + } + public Number getMin(){ + return this.min; + } + public void setMin(final Number min){ + this.min = min; + } + public Number getMorn(){ + return this.morn; + } + public void setMorn(final Number morn){ + this.morn = morn; + } + public Number getNight(){ + return this.night; + } + public void setNight(final Number night){ + this.night = night; + } + + @Override + public String toString() { + final StringBuilder builder = new StringBuilder(); + builder.append("Temp [day=").append(this.day).append(", eve=").append(this.eve) + .append(", max=").append(this.max).append(", min=").append(this.min) + .append(", morn=").append(this.morn).append(", night=").append(this.night) + .append("]"); + return builder.toString(); + } +} diff --git a/Allgemeines/Jackson/src/main/java/de/example/jackson/auto/forecast/Weather.java b/Allgemeines/Jackson/src/main/java/de/example/jackson/auto/forecast/Weather.java new file mode 100644 index 0000000..9e6e51e --- /dev/null +++ b/Allgemeines/Jackson/src/main/java/de/example/jackson/auto/forecast/Weather.java @@ -0,0 +1,45 @@ +package de.example.jackson.auto.forecast; + +import java.io.Serializable; + +public class Weather implements Serializable { + private static final long serialVersionUID = -5066357704517363241L; + private String description; + private String icon; + private Number id; + private String main; + + public String getDescription(){ + return this.description; + } + public void setDescription(final String description){ + this.description = description; + } + public String getIcon(){ + return this.icon; + } + public void setIcon(final String icon){ + this.icon = icon; + } + public Number getId(){ + return this.id; + } + public void setId(final Number id){ + this.id = id; + } + public String getMain(){ + return this.main; + } + public void setMain(final String main){ + this.main = main; + } + + @Override + public String toString() { + final StringBuilder builder = new StringBuilder(); + builder.append("Weather [description=").append(this.description).append(", icon=") + .append(this.icon).append(", id=").append(this.id).append(", main=") + .append(this.main).append("]"); + return builder.toString(); + } +} diff --git a/Allgemeines/Jackson/src/main/java/de/example/jackson/databinding/Coord.java b/Allgemeines/Jackson/src/main/java/de/example/jackson/databinding/Coord.java deleted file mode 100644 index 97be147..0000000 --- a/Allgemeines/Jackson/src/main/java/de/example/jackson/databinding/Coord.java +++ /dev/null @@ -1,24 +0,0 @@ -package de.example.jackson.databinding; - -public class Coord { - // City location - private Double lon; - private Double lat; - - - public Double getLon() { - return this.lon; - } - - public Double getLat() { - return this.lat; - } - - public void setLon(final Double lon) { - this.lon = lon; - } - - public void setLat(final Double lat) { - this.lat = lat; - } -} diff --git a/Allgemeines/Jackson/src/main/java/de/example/jackson/databinding/JacksonDataBindingTestMain.java b/Allgemeines/Jackson/src/main/java/de/example/jackson/databinding/JacksonDataBindingTestMain.java index 8ed637e..5f2dd35 100644 --- a/Allgemeines/Jackson/src/main/java/de/example/jackson/databinding/JacksonDataBindingTestMain.java +++ b/Allgemeines/Jackson/src/main/java/de/example/jackson/databinding/JacksonDataBindingTestMain.java @@ -8,10 +8,15 @@ import com.fasterxml.jackson.core.JsonParseException; import com.fasterxml.jackson.databind.JsonMappingException; import com.fasterxml.jackson.databind.ObjectMapper; +import de.example.jackson.auto.currentweather.CurrentWeatherData; +import de.example.jackson.auto.forecast.ForecastWeatherData; + public class JacksonDataBindingTestMain { public static void main(final String[] args) { + // Searching by geographic coordinates: + // http://api.openweathermap.org/data/2.5/weather?lat=57&lon=-2.15&cnt=1 final String dataA = "{" + "\"coord\":{\"lon\":139,\"lat\":35}," @@ -22,6 +27,7 @@ public class JacksonDataBindingTestMain { + "\"wind\":{\"speed\":1.11,\"deg\":64.5043}," + "\"clouds\":{\"all\":0}," + "\"dt\":1397227133," + + "\"rain\":{\"3h\":0}," + "\"id\":1851632," + "\"name\":\"Shuzenji\"," + "\"cod\":200" @@ -34,12 +40,134 @@ public class JacksonDataBindingTestMain { + "\"name\":\"Shuzenji\"," + "\"cod\":200" + "}"; + + // Getting daily forecast weather data: Searching 15 days forecast by geographic coordinates at JSON format + // http://api.openweathermap.org/data/2.5/forecast/daily?lat=57&lon=-2.15&cnt=15&mode=json + final String dataForeCast = "{\"cod\":\"200\"," + + "\"message\":0.0048," + + "\"city\":{\"id\":2641549,\"name\":\"Newtonhill\",\"coord\":{\"lon\":-2.15,\"lat\":57.033329},\"country\":\"GB\",\"population\":0}," + + "\"cnt\":15," + + "\"list\":[" + + "{\"dt\":1397304000,\"temp\":{\"day\":286.15,\"min\":284.62,\"max\":286.15,\"night\":284.62,\"eve\":285.7,\"morn\":286.15},\"pressure\":1016.67,\"humidity\":84,\"weather\":[{\"id\":500,\"main\":\"Rain\",\"description\":\"light rain\",\"icon\":\"10d\"}],\"speed\":7.68,\"deg\":252,\"clouds\":0,\"rain\":0.25}," + + "{\"dt\":1397390400,\"temp\":{\"day\":284.92,\"min\":282.3,\"max\":284.92,\"night\":282.3,\"eve\":283.79,\"morn\":284.24},\"pressure\":1021.62,\"humidity\":84,\"weather\":[{\"id\":804,\"main\":\"Clouds\",\"description\":\"overcast clouds\",\"icon\":\"04d\"}],\"speed\":7.91,\"deg\":259,\"clouds\":92}," + + "{\"dt\":1397476800,\"temp\":{\"day\":282.1,\"min\":280.32,\"max\":282.1,\"night\":280.32,\"eve\":281.51,\"morn\":281.65},\"pressure\":1033.84,\"humidity\":92,\"weather\":[{\"id\":801,\"main\":\"Clouds\",\"description\":\"few clouds\",\"icon\":\"02d\"}],\"speed\":8.37,\"deg\":324,\"clouds\":20}," + + "{\"dt\":1397563200,\"temp\":{\"day\":280.73,\"min\":280.11,\"max\":281.4,\"night\":281.4,\"eve\":280.75,\"morn\":280.11},\"pressure\":1039.27,\"humidity\":97,\"weather\":[{\"id\":801,\"main\":\"Clouds\",\"description\":\"few clouds\",\"icon\":\"02d\"}],\"speed\":7.31,\"deg\":184,\"clouds\":12}," + + "{\"dt\":1397649600,\"temp\":{\"day\":281.73,\"min\":281.03,\"max\":282.22,\"night\":281.69,\"eve\":282.22,\"morn\":281.03},\"pressure\":1036.05,\"humidity\":90,\"weather\":[{\"id\":803,\"main\":\"Clouds\",\"description\":\"broken clouds\",\"icon\":\"04d\"}],\"speed\":7.61,\"deg\":205,\"clouds\":68}," + + "{\"dt\":1397736000,\"temp\":{\"day\":282.9,\"min\":281.45,\"max\":283.21,\"night\":282.71,\"eve\":283.06,\"morn\":281.49},\"pressure\":1029.39,\"humidity\":83,\"weather\":[{\"id\":803,\"main\":\"Clouds\",\"description\":\"broken clouds\",\"icon\":\"04d\"}],\"speed\":6.17,\"deg\":268,\"clouds\":56}," + + "{\"dt\":1397822400,\"temp\":{\"day\":285.26,\"min\":281.55,\"max\":285.26,\"night\":282.48,\"eve\":285.09,\"morn\":281.55},\"pressure\":1025.83,\"humidity\":0,\"weather\":[{\"id\":800,\"main\":\"Clear\",\"description\":\"sky is clear\",\"icon\":\"01d\"}],\"speed\":5.31,\"deg\":221,\"clouds\":10}," + + "{\"dt\":1397908800,\"temp\":{\"day\":284.29,\"min\":281.5,\"max\":284.29,\"night\":282.53,\"eve\":283.58,\"morn\":281.5},\"pressure\":1024.55,\"humidity\":0,\"weather\":[{\"id\":500,\"main\":\"Rain\",\"description\":\"light rain\",\"icon\":\"10d\"}],\"speed\":5.51,\"deg\":192,\"clouds\":6}," + + "{\"dt\":1397995200,\"temp\":{\"day\":283.36,\"min\":281.62,\"max\":284.34,\"night\":284.04,\"eve\":284.34,\"morn\":281.62},\"pressure\":1019.58,\"humidity\":0,\"weather\":[{\"id\":500,\"main\":\"Rain\",\"description\":\"light rain\",\"icon\":\"10d\"}],\"speed\":7.66,\"deg\":149,\"clouds\":0,\"rain\":0.48}," + + "{\"dt\":1398081600,\"temp\":{\"day\":282.24,\"min\":280.51,\"max\":282.41,\"night\":280.51,\"eve\":282.41,\"morn\":280.9},\"pressure\":1027.35,\"humidity\":0,\"weather\":[{\"id\":500,\"main\":\"Rain\",\"description\":\"light rain\",\"icon\":\"10d\"}],\"speed\":8.17,\"deg\":221,\"clouds\":10,\"rain\":0.94}," + + "{\"dt\":1398168000,\"temp\":{\"day\":282.28,\"min\":279.76,\"max\":282.28,\"night\":280.69,\"eve\":281.13,\"morn\":279.76},\"pressure\":1038.31,\"humidity\":0,\"weather\":[{\"id\":800,\"main\":\"Clear\",\"description\":\"sky is clear\",\"icon\":\"01d\"}],\"speed\":6.33,\"deg\":172,\"clouds\":1}," + + "{\"dt\":1398254400,\"temp\":{\"day\":281.54,\"min\":280.52,\"max\":281.54,\"night\":281.44,\"eve\":281.23,\"morn\":280.52},\"pressure\":1022.4,\"humidity\":0,\"weather\":[{\"id\":500,\"main\":\"Rain\",\"description\":\"light rain\",\"icon\":\"10d\"}],\"speed\":7.84,\"deg\":140,\"clouds\":91,\"rain\":1.24}," + + "{\"dt\":1398340800,\"temp\":{\"day\":282.1,\"min\":280.66,\"max\":282.78,\"night\":280.97,\"eve\":282.78,\"morn\":280.66},\"pressure\":1013.39,\"humidity\":0,\"weather\":[{\"id\":500,\"main\":\"Rain\",\"description\":\"light rain\",\"icon\":\"10d\"}],\"speed\":9.43,\"deg\":164,\"clouds\":98,\"rain\":1.03}," + + "{\"dt\":1398427200,\"temp\":{\"day\":282.11,\"min\":280.72,\"max\":282.32,\"night\":282.32,\"eve\":280.99,\"morn\":280.72},\"pressure\":1018.65,\"humidity\":0,\"weather\":[{\"id\":502,\"main\":\"Rain\",\"description\":\"heavy intensity rain\",\"icon\":\"10d\"}],\"speed\":5.26,\"deg\":158,\"clouds\":83,\"rain\":14.4}," + + "{\"dt\":1398513600,\"temp\":{\"day\":282.75,\"min\":280.61,\"max\":282.75,\"night\":280.61,\"eve\":281.75,\"morn\":281.96},\"pressure\":1007.4,\"humidity\":0,\"weather\":[{\"id\":500,\"main\":\"Rain\",\"description\":\"light rain\",\"icon\":\"10d\"}],\"speed\":9.18,\"deg\":198,\"clouds\":35,\"rain\":0.55}" + + "]}"; + final ObjectMapper mapper = new ObjectMapper(); - // Unmarshalling: - WeatherDataPOJO weatherDataPOJO = null; + CurrentWeatherData currentWeatherData = unmarshalling(dataA, mapper, + CurrentWeatherData.class); + if (currentWeatherData != null) { + System.out.println("DATAA UNMARSHALLING (JSON TO JAVA)"); + System.out.println("coord: " + currentWeatherData.getCoord().getLat() + " " + + currentWeatherData.getCoord().getLon()); + System.out.println("wind: " + currentWeatherData.getWind().getSpeed() + " " + + currentWeatherData.getWind().getDeg()); + System.out.println("name: " + currentWeatherData.getName()); + System.out.println("cod: " + currentWeatherData.getCod()); + } + + String jsonMarshalling = marshalling(currentWeatherData, mapper); + if (jsonMarshalling != null) { + System.out.println("DATAA MARSHALLING (JAVA TO JSON)"); + System.out.println(jsonMarshalling); + } + + System.out.println(); + System.out.println(); + + currentWeatherData = unmarshalling(dataB, mapper, + CurrentWeatherData.class); + if (currentWeatherData != null) { + System.out.println("DATAB UNMARSHALLING (JSON TO JAVA)"); + System.out.println("coord: " + currentWeatherData.getCoord().getLat() + " " + + currentWeatherData.getCoord().getLon()); + System.out.println("wind: " + currentWeatherData.getWind().getSpeed() + " " + + currentWeatherData.getWind().getDeg()); + System.out.println("name: " + currentWeatherData.getName()); + System.out.println("cod: " + currentWeatherData.getCod()); + } + + jsonMarshalling = marshalling(currentWeatherData, mapper); + if (jsonMarshalling != null) { + System.out.println("DATAB MARSHALLING (JAVA TO JSON)"); + System.out.println(jsonMarshalling); + } + + System.out.println(); + System.out.println(); + + + final ForecastWeatherData forecastWeatherData = unmarshalling( + dataForeCast, mapper, ForecastWeatherData.class); + if (forecastWeatherData != null) { + System.out.println("FORECASTWEATHER UNMARSHALLING (JSON TO JAVA)"); + System.out.println("cnt " + forecastWeatherData.getCnt()); + System.out.println("cod " + forecastWeatherData.getCod()); + System.out.println("city country " + forecastWeatherData.getCity().getCountry()); + System.out.println("city name " + forecastWeatherData.getCity().getName()); + System.out.println("city coord lat " + forecastWeatherData.getCity().getCoord().getLon()); + System.out.println("city population " + forecastWeatherData.getCity().getCoord().getLat()); + System.out.println("city id " + forecastWeatherData.getCity().getPopulation()); + System.out.println("cnt " + forecastWeatherData.getCity().getId()); + final int cnt = (Integer) forecastWeatherData.getCnt(); + for (int i = 0; i < cnt; i++) { + System.out.println("FORECAST LIST NUMBER: " + i); + if (forecastWeatherData.getList().get(i).getWeather().size() > 0) { + System.out.println("list weather description " + forecastWeatherData.getList().get(i).getWeather().get(0).getDescription()); + System.out.println("list weather id " + forecastWeatherData.getList().get(i).getWeather().get(0).getId()); + System.out.println("list weather main " + forecastWeatherData.getList().get(i).getWeather().get(0).getMain()); + System.out.println("list weather icon " + forecastWeatherData.getList().get(i).getWeather().get(0).getIcon()); + } + System.out.println("list clouds " + forecastWeatherData.getList().get(i).getClouds()); + System.out.println("list deg " + forecastWeatherData.getList().get(i).getDeg()); + System.out.println("list dt " + forecastWeatherData.getList().get(i).getDt()); + System.out.println("list humidity " + forecastWeatherData.getList().get(i).getHumidity()); + System.out.println("list pressure " + forecastWeatherData.getList().get(i).getPressure()); + System.out.println("list rain " + forecastWeatherData.getList().get(i).getRain()); + System.out.println("list snow " + forecastWeatherData.getList().get(i).getSnow()); + System.out.println("list speed " + forecastWeatherData.getList().get(i).getSpeed()); + System.out.println("list temp day " + forecastWeatherData.getList().get(i).getTemp().getDay()); + System.out.println("list temp eve " + forecastWeatherData.getList().get(i).getTemp().getEve()); + System.out.println("list temp max " + forecastWeatherData.getList().get(i).getTemp().getMax()); + System.out.println("list temp min " + forecastWeatherData.getList().get(i).getTemp().getMin()); + System.out.println("list temp morn " + forecastWeatherData.getList().get(i).getTemp().getMorn()); + System.out.println("list temp night " + forecastWeatherData.getList().get(i).getTemp().getNight()); + } + } + + jsonMarshalling = marshalling(forecastWeatherData, + mapper); + if (jsonMarshalling != null) { + System.out.println("FORECASTWEATHER MARSHALLING (JAVA TO JSON)"); + System.out.println(jsonMarshalling); + } + } + + /** + * Unmarshalling: JSON -> JAVA + * + * @param jsonData + * @param mapper + * @return WeatherData + */ + public static E unmarshalling(final String jsonData, final ObjectMapper mapper, final Class typeClass) { + E weatherData = null; + try { - weatherDataPOJO = mapper.readValue(dataB.getBytes("UTF-8"), WeatherDataPOJO.class); + weatherData = mapper.readValue(jsonData.getBytes("UTF-8"), typeClass); } catch (final JsonParseException e) { e.printStackTrace(); } catch (final JsonMappingException e) { @@ -50,27 +178,29 @@ public class JacksonDataBindingTestMain { e.printStackTrace(); } - if (weatherDataPOJO != null) { - System.out.println("coord: " + weatherDataPOJO.getCoord().getLat() + " " + weatherDataPOJO.getCoord().getLon()); - System.out.println("wind: " + weatherDataPOJO.getWind().getSpeed() + " " + weatherDataPOJO.getWind().getDeg()); - System.out.println("name: " + weatherDataPOJO.getName()); - System.out.println("cod: " + weatherDataPOJO.getCod()); - } + return weatherData; + } + + /** + * Marshalling: JAVA -> JSON + * + * @param weatherData + * @param mapper + * @return String + */ + public static String marshalling(final Object weatherData, final ObjectMapper mapper) { + String marshalling = null; - // Marshalling: try { - final String dataBMarshalling = mapper.writeValueAsString(weatherDataPOJO); - System.out.println(dataBMarshalling); + marshalling = mapper.writeValueAsString(weatherData); } catch (final JsonGenerationException e) { - // TODO Auto-generated catch block e.printStackTrace(); } catch (final JsonMappingException e) { - // TODO Auto-generated catch block e.printStackTrace(); } catch (final IOException e) { - // TODO Auto-generated catch block e.printStackTrace(); } - } + return marshalling; + } } diff --git a/Allgemeines/Jackson/src/main/java/de/example/jackson/databinding/Weather.java b/Allgemeines/Jackson/src/main/java/de/example/jackson/databinding/Weather.java deleted file mode 100644 index 0feb735..0000000 --- a/Allgemeines/Jackson/src/main/java/de/example/jackson/databinding/Weather.java +++ /dev/null @@ -1,39 +0,0 @@ -package de.example.jackson.databinding; - -public class Weather { - private Integer id; - private String main; - private String description; - private String icon; - - public Integer getId() { - return id; - } - - public String getMain() { - return main; - } - - public String getDescription() { - return description; - } - public String getIcon() { - return icon; - } - - public void setId(final Integer id) { - this.id = id; - } - - public void setMain(final String main) { - this.main = main; - } - - public void setDescription(final String description) { - this.description = description; - } - - public void setIcon(final String icon) { - this.icon = icon; - } -} diff --git a/Allgemeines/Jackson/src/main/java/de/example/jackson/databinding/WeatherDataPOJO.java b/Allgemeines/Jackson/src/main/java/de/example/jackson/databinding/WeatherDataPOJO.java deleted file mode 100644 index b75fcdc..0000000 --- a/Allgemeines/Jackson/src/main/java/de/example/jackson/databinding/WeatherDataPOJO.java +++ /dev/null @@ -1,35 +0,0 @@ -package de.example.jackson.databinding; - - - -public class WeatherDataPOJO { - private String name; - private Integer cod; - private Coord coord; - private Wind wind; - - - public String getName() { - return name; - } - - public Integer getCod() { - return cod; - } - - public void setName(final String name) { - this.name = name; - } - - public void setCod(final Integer cod) { - this.cod = cod; - } - - public Coord getCoord() { - return coord; - } - - public Wind getWind() { - return wind; - } -} diff --git a/Allgemeines/Jackson/src/main/java/de/example/jackson/databinding/Wind.java b/Allgemeines/Jackson/src/main/java/de/example/jackson/databinding/Wind.java deleted file mode 100644 index 11c80fa..0000000 --- a/Allgemeines/Jackson/src/main/java/de/example/jackson/databinding/Wind.java +++ /dev/null @@ -1,55 +0,0 @@ -package de.example.jackson.databinding; - -public class Wind { - // Wind speed in mps - private Double speed; - // Wind direction in degrees (meteorological) - private Double deg; - // speed of wind gust - private Double gust; - // Wind direction - private Double var_beg; - // Wind direction - private Double var_end; - - - public Double getSpeed() { - return this.speed; - } - - public Double getDeg() { - return this.deg; - } - - public Double getGust() { - return this.gust; - } - - public Double getVar_beg() { - return this.var_beg; - } - - public Double getVar_end() { - return this.var_end; - } - - public void setSpeed(final Double speed) { - this.speed = speed; - } - - public void setDeg(final Double deg) { - this.deg = deg; - } - - public void setGust(final Double gust) { - this.gust = gust; - } - - public void setVar_beg(final Double var_beg) { - this.var_beg = var_beg; - } - - public void setVar_end(final Double var_end) { - this.var_end = var_end; - } -} diff --git a/Allgemeines/Jackson/src/main/java/de/example/jackson/streaming/JacksonStreamingTestMain.java b/Allgemeines/Jackson/src/main/java/de/example/jackson/streaming/JacksonStreamingTestMain.java new file mode 100644 index 0000000..dc850a5 --- /dev/null +++ b/Allgemeines/Jackson/src/main/java/de/example/jackson/streaming/JacksonStreamingTestMain.java @@ -0,0 +1,512 @@ +package de.example.jackson.streaming; + +import java.io.IOException; +import java.util.ArrayList; + +import com.fasterxml.jackson.core.JsonFactory; +import com.fasterxml.jackson.core.JsonParseException; +import com.fasterxml.jackson.core.JsonParser; +import com.fasterxml.jackson.core.JsonToken; + +import de.example.jackson.auto.currentweather.Clouds; +import de.example.jackson.auto.currentweather.Coord; +import de.example.jackson.auto.currentweather.CurrentWeatherData; +import de.example.jackson.auto.currentweather.Main; +import de.example.jackson.auto.currentweather.Rain; +import de.example.jackson.auto.currentweather.Sys; +import de.example.jackson.auto.currentweather.Wind; +import de.example.jackson.auto.forecast.City; +import de.example.jackson.auto.forecast.ForecastWeatherData; +import de.example.jackson.auto.forecast.List; +import de.example.jackson.auto.forecast.Temp; + +public class JacksonStreamingTestMain { + + public static void main(final String[] args) throws JsonParseException, + IOException { + // Searching by geographic coordinates: + // http://api.openweathermap.org/data/2.5/weather?lat=57&lon=-2.15&cnt=1 + final String dataA = "{" + + "\"coord\":{\"lon\":139,\"lat\":35}," + + "\"sys\":{\"message\":5.2147,\"country\":\"JP\",\"sunrise\":1397161018,\"sunset\":1397207585}," + + "\"weather\":[{\"id\":800,\"main\":\"Clear\",\"description\":\"Sky is Clear\",\"icon\":\"01n\"}]," + + "\"base\":\"cmc stations\"," + + "\"main\":{\"temp\":273.275,\"temp_min\":273.275,\"temp_max\":273.275,\"pressure\":988.56,\"sea_level\":1033.79,\"grnd_level\":988.56,\"humidity\":95}," + + "\"wind\":{\"speed\":1.11,\"deg\":64.5043}," + + "\"clouds\":{\"all\":0}," + + "\"dt\":1397227133," + + "\"rain\":{\"3h\":0}," + + "\"id\":1851632," + + "\"name\":\"Shuzenji\"," + + "\"cod\":200" + "}"; + + final String dataB = "{" + "\"coord\":{\"lon\":139,\"lat\":35}," + + "\"wind\":{\"speed\":1.11,\"deg\":64.5043}," + + "\"name\":\"Shuzenji\"," + "\"cod\":200" + "}"; + + // Getting daily forecast weather data: Searching 15 days forecast by + // geographic coordinates at JSON format + // http://api.openweathermap.org/data/2.5/forecast/daily?lat=57&lon=-2.15&cnt=15&mode=json + final String dataForeCast = "{" + + "\"cod\":\"200\"," + + "\"message\":0.0048," + + "\"city\":{\"id\":2641549,\"name\":\"Newtonhill\",\"coord\":{\"lon\":-2.15,\"lat\":57.033329},\"country\":\"GB\",\"population\":0}," + + "\"cnt\":15," + + "\"list\":[" + + "{\"dt\":1397304000,\"temp\":{\"day\":286.15,\"min\":284.62,\"max\":286.15,\"night\":284.62,\"eve\":285.7,\"morn\":286.15},\"pressure\":1016.67,\"humidity\":84,\"weather\":[{\"id\":500,\"main\":\"Rain\",\"description\":\"light rain\",\"icon\":\"10d\"}],\"speed\":7.68,\"deg\":252,\"clouds\":0,\"rain\":0.25}," + + "{\"dt\":1397390400,\"temp\":{\"day\":284.92,\"min\":282.3,\"max\":284.92,\"night\":282.3,\"eve\":283.79,\"morn\":284.24},\"pressure\":1021.62,\"humidity\":84,\"weather\":[{\"id\":804,\"main\":\"Clouds\",\"description\":\"overcast clouds\",\"icon\":\"04d\"}],\"speed\":7.91,\"deg\":259,\"clouds\":92}," + + "{\"dt\":1397476800,\"temp\":{\"day\":282.1,\"min\":280.32,\"max\":282.1,\"night\":280.32,\"eve\":281.51,\"morn\":281.65},\"pressure\":1033.84,\"humidity\":92,\"weather\":[{\"id\":801,\"main\":\"Clouds\",\"description\":\"few clouds\",\"icon\":\"02d\"}],\"speed\":8.37,\"deg\":324,\"clouds\":20}," + + "{\"dt\":1397563200,\"temp\":{\"day\":280.73,\"min\":280.11,\"max\":281.4,\"night\":281.4,\"eve\":280.75,\"morn\":280.11},\"pressure\":1039.27,\"humidity\":97,\"weather\":[{\"id\":801,\"main\":\"Clouds\",\"description\":\"few clouds\",\"icon\":\"02d\"}],\"speed\":7.31,\"deg\":184,\"clouds\":12}," + + "{\"dt\":1397649600,\"temp\":{\"day\":281.73,\"min\":281.03,\"max\":282.22,\"night\":281.69,\"eve\":282.22,\"morn\":281.03},\"pressure\":1036.05,\"humidity\":90,\"weather\":[{\"id\":803,\"main\":\"Clouds\",\"description\":\"broken clouds\",\"icon\":\"04d\"}],\"speed\":7.61,\"deg\":205,\"clouds\":68}," + + "{\"dt\":1397736000,\"temp\":{\"day\":282.9,\"min\":281.45,\"max\":283.21,\"night\":282.71,\"eve\":283.06,\"morn\":281.49},\"pressure\":1029.39,\"humidity\":83,\"weather\":[{\"id\":803,\"main\":\"Clouds\",\"description\":\"broken clouds\",\"icon\":\"04d\"}],\"speed\":6.17,\"deg\":268,\"clouds\":56}," + + "{\"dt\":1397822400,\"temp\":{\"day\":285.26,\"min\":281.55,\"max\":285.26,\"night\":282.48,\"eve\":285.09,\"morn\":281.55},\"pressure\":1025.83,\"humidity\":0,\"weather\":[{\"id\":800,\"main\":\"Clear\",\"description\":\"sky is clear\",\"icon\":\"01d\"}],\"speed\":5.31,\"deg\":221,\"clouds\":10}," + + "{\"dt\":1397908800,\"temp\":{\"day\":284.29,\"min\":281.5,\"max\":284.29,\"night\":282.53,\"eve\":283.58,\"morn\":281.5},\"pressure\":1024.55,\"humidity\":0,\"weather\":[{\"id\":500,\"main\":\"Rain\",\"description\":\"light rain\",\"icon\":\"10d\"}],\"speed\":5.51,\"deg\":192,\"clouds\":6}," + + "{\"dt\":1397995200,\"temp\":{\"day\":283.36,\"min\":281.62,\"max\":284.34,\"night\":284.04,\"eve\":284.34,\"morn\":281.62},\"pressure\":1019.58,\"humidity\":0,\"weather\":[{\"id\":500,\"main\":\"Rain\",\"description\":\"light rain\",\"icon\":\"10d\"}],\"speed\":7.66,\"deg\":149,\"clouds\":0,\"rain\":0.48}," + + "{\"dt\":1398081600,\"temp\":{\"day\":282.24,\"min\":280.51,\"max\":282.41,\"night\":280.51,\"eve\":282.41,\"morn\":280.9},\"pressure\":1027.35,\"humidity\":0,\"weather\":[{\"id\":500,\"main\":\"Rain\",\"description\":\"light rain\",\"icon\":\"10d\"}],\"speed\":8.17,\"deg\":221,\"clouds\":10,\"rain\":0.94}," + + "{\"dt\":1398168000,\"temp\":{\"day\":282.28,\"min\":279.76,\"max\":282.28,\"night\":280.69,\"eve\":281.13,\"morn\":279.76},\"pressure\":1038.31,\"humidity\":0,\"weather\":[{\"id\":800,\"main\":\"Clear\",\"description\":\"sky is clear\",\"icon\":\"01d\"}],\"speed\":6.33,\"deg\":172,\"clouds\":1}," + + "{\"dt\":1398254400,\"temp\":{\"day\":281.54,\"min\":280.52,\"max\":281.54,\"night\":281.44,\"eve\":281.23,\"morn\":280.52},\"pressure\":1022.4,\"humidity\":0,\"weather\":[{\"id\":500,\"main\":\"Rain\",\"description\":\"light rain\",\"icon\":\"10d\"}],\"speed\":7.84,\"deg\":140,\"clouds\":91,\"rain\":1.24}," + + "{\"dt\":1398340800,\"temp\":{\"day\":282.1,\"min\":280.66,\"max\":282.78,\"night\":280.97,\"eve\":282.78,\"morn\":280.66},\"pressure\":1013.39,\"humidity\":0,\"weather\":[{\"id\":500,\"main\":\"Rain\",\"description\":\"light rain\",\"icon\":\"10d\"}],\"speed\":9.43,\"deg\":164,\"clouds\":98,\"rain\":1.03}," + + "{\"dt\":1398427200,\"temp\":{\"day\":282.11,\"min\":280.72,\"max\":282.32,\"night\":282.32,\"eve\":280.99,\"morn\":280.72},\"pressure\":1018.65,\"humidity\":0,\"weather\":[{\"id\":502,\"main\":\"Rain\",\"description\":\"heavy intensity rain\",\"icon\":\"10d\"}],\"speed\":5.26,\"deg\":158,\"clouds\":83,\"rain\":14.4}," + + "{\"dt\":1398513600,\"temp\":{\"day\":282.75,\"min\":280.61,\"max\":282.75,\"night\":280.61,\"eve\":281.75,\"morn\":281.96},\"pressure\":1007.4,\"humidity\":0,\"weather\":[{\"id\":500,\"main\":\"Rain\",\"description\":\"light rain\",\"icon\":\"10d\"}],\"speed\":9.18,\"deg\":198,\"clouds\":35,\"rain\":0.55}" + + "]}"; + + final JsonFactory f = new JsonFactory(); + + CurrentWeatherData currentWeatherData = new CurrentWeatherData(); + currentWeatherData.setClouds(new Clouds()); + currentWeatherData.setCoord(new Coord()); + currentWeatherData.setMain(new Main()); + currentWeatherData.setRain(new Rain()); + currentWeatherData.setSys(new Sys()); + currentWeatherData.setWeather(new ArrayList()); + currentWeatherData.setWind(new Wind()); + JsonParser jParser = f.createParser(dataA); + + System.out.println("DATAA UNMARSHALLING (JSON TO JAVA)"); + getCurrentWeatherData(currentWeatherData, jParser); + + printCurrentWeatherData(currentWeatherData); + + currentWeatherData = new CurrentWeatherData(); + currentWeatherData.setClouds(new Clouds()); + currentWeatherData.setCoord(new Coord()); + currentWeatherData.setMain(new Main()); + currentWeatherData.setRain(new Rain()); + currentWeatherData.setSys(new Sys()); + currentWeatherData + .setWeather(new ArrayList()); + currentWeatherData.setWind(new Wind()); + jParser = f.createParser(dataB); + + System.out.println(); + System.out.println(); + + System.out.println("DATAB UNMARSHALLING (JSON TO JAVA)"); + getCurrentWeatherData(currentWeatherData, jParser); + + printCurrentWeatherData(currentWeatherData); + + System.out.println(); + System.out.println(); + + final ForecastWeatherData forecastWeatherData = new ForecastWeatherData(); + forecastWeatherData.setList(new ArrayList(15)); + final City city = new City(); + city.setCoord(new de.example.jackson.auto.forecast.Coord()); + forecastWeatherData.setCity(city); + jParser = f.createParser(dataForeCast); + + System.out.println("FORECASTWEATHER UNMARSHALLING (JSON TO JAVA)"); + getForecastWeatherData(forecastWeatherData, jParser); + + printForecastWeatherData(forecastWeatherData); + } + + public static void getCurrentWeatherData(final CurrentWeatherData currentWeatherData, + final JsonParser jParser) throws JsonParseException, IOException { + if (jParser.nextToken() == JsonToken.START_OBJECT) { + + while (jParser.nextToken() != JsonToken.END_OBJECT) { + final String fieldname = jParser.getCurrentName(); + final JsonToken nextToken = jParser.nextToken(); + if (nextToken == JsonToken.START_OBJECT) { + getCurrentWeatherDataObjects(currentWeatherData, jParser, fieldname); + } + if (nextToken == JsonToken.START_ARRAY) { + JsonToken tokenNext = jParser.nextToken(); + while (tokenNext != JsonToken.END_ARRAY) { + if (tokenNext == JsonToken.START_OBJECT) { + getCurrentWeatherDataObjects(currentWeatherData, jParser, fieldname); + } + tokenNext = jParser.nextToken(); + } + } + if ((nextToken == JsonToken.VALUE_NUMBER_INT) || (nextToken == JsonToken.VALUE_STRING)) { + getCurrentWeatherDataObjects(currentWeatherData, jParser, fieldname); + } + } + } + } + + public static void getCurrentWeatherDataObjects(final CurrentWeatherData currentWeatherData, + final JsonParser jParser, final String fieldname) throws JsonParseException, IOException { + if (fieldname == "coord") { + while (jParser.nextToken() != JsonToken.END_OBJECT) { + final String namefield = jParser.getCurrentName(); + jParser.nextToken(); // move to value + if ("lon".equals(namefield)) { + currentWeatherData.getCoord().setLon(jParser.getDoubleValue()); + } + if ("lat".equals(namefield)) { + currentWeatherData.getCoord().setLat(jParser.getDoubleValue()); + } + } + } + if (fieldname == "sys") { + while (jParser.nextToken() != JsonToken.END_OBJECT) { + final String namefield = jParser.getCurrentName(); + jParser.nextToken(); // move to value + if ("message".equals(namefield)) { + currentWeatherData.getSys().setMessage(jParser.getDoubleValue()); + } + if ("country".equals(namefield)) { + currentWeatherData.getSys().setCountry(jParser.getValueAsString()); + } + if ("sunrise".equals(namefield)) { + currentWeatherData.getSys().setSunrise(jParser.getValueAsLong()); + } + if ("sunset".equals(namefield)) { + currentWeatherData.getSys().setSunset(jParser.getValueAsLong()); + } + } + } + if (fieldname == "weather") { + final de.example.jackson.auto.currentweather.Weather weather = new de.example.jackson.auto.currentweather.Weather(); + currentWeatherData.getWeather().add(weather); + while (jParser.nextToken() != JsonToken.END_OBJECT) { + final String namefield = jParser.getCurrentName(); + jParser.nextToken(); // move to value + 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()); + } + + } + } + if (fieldname == "base") { + currentWeatherData.setBase(jParser.getText()); + } + if (fieldname == "main") { + while (jParser.nextToken() != JsonToken.END_OBJECT) { + final String namefield = jParser.getCurrentName(); + jParser.nextToken(); // move to value + if ("temp".equals(namefield)) { + currentWeatherData.getMain().setTemp(jParser.getDoubleValue()); + } + if ("temp_min".equals(namefield)) { + currentWeatherData.getMain().setTemp_min(jParser.getDoubleValue()); + } + if ("temp_max".equals(namefield)) { + currentWeatherData.getMain().setTemp_max(jParser.getDoubleValue()); + } + if ("pressure".equals(namefield)) { + currentWeatherData.getMain().setPressure(jParser.getDoubleValue()); + } + if ("sea_level".equals(namefield)) { + currentWeatherData.getMain().setSea_level(jParser.getDoubleValue()); + } + if ("grnd_level".equals(namefield)) { + currentWeatherData.getMain().setGrnd_level(jParser.getDoubleValue()); + } + if ("humidity".equals(namefield)) { + currentWeatherData.getMain().setHumidity(jParser.getDoubleValue()); + } + } + } + if (fieldname == "wind") { + while (jParser.nextToken() != JsonToken.END_OBJECT) { + final String namefield = jParser.getCurrentName(); + jParser.nextToken(); // move to value + if ("speed".equals(namefield)) { + currentWeatherData.getWind().setSpeed(jParser.getDoubleValue()); + } + if ("deg".equals(namefield)) { + currentWeatherData.getWind().setDeg(jParser.getDoubleValue()); + } + } + } + if (fieldname == "clouds") { + while (jParser.nextToken() != JsonToken.END_OBJECT) { + final String namefield = jParser.getCurrentName(); + jParser.nextToken(); // move to value + if ("all".equals(namefield)) { + currentWeatherData.getClouds().setAll(jParser.getDoubleValue()); + } + } + } + if (fieldname == "dt") { + currentWeatherData.setDt(jParser.getLongValue()); + } + if (fieldname == "rain") { + while (jParser.nextToken() != JsonToken.END_OBJECT) { + final String namefield = jParser.getCurrentName(); + jParser.nextToken(); // move to value + if ("3h".equals(namefield)) { + currentWeatherData.getRain().set3h(jParser.getDoubleValue()); + } + } + } + if (fieldname == "snow") { + while (jParser.nextToken() != JsonToken.END_OBJECT) { + final String namefield = jParser.getCurrentName(); + jParser.nextToken(); // move to value + if ("3h".equals(namefield)) { + currentWeatherData.getSnow().set3h(jParser.getDoubleValue()); + } + } + } + if (fieldname == "id") { + currentWeatherData.setId(jParser.getLongValue()); + } + if (fieldname == "name") { + currentWeatherData.setName(jParser.getText()); + } + if (fieldname == "cod") { + currentWeatherData.setCod(jParser.getLongValue()); + } + } + + public static void printCurrentWeatherData(final CurrentWeatherData currentWeatherData) { + System.out.println(currentWeatherData.getCoord().getLat()); + System.out.println(currentWeatherData.getCoord().getLon()); + System.out.println(currentWeatherData.getSys().getCountry()); + if (currentWeatherData.getWeather().size() > 0) { + System.out.println(currentWeatherData.getWeather().get(0).getDescription()); + System.out.println(currentWeatherData.getWeather().get(0).getMain()); + } + System.out.println(currentWeatherData.getMain().getTemp()); + System.out.println(currentWeatherData.getMain().getTemp_max()); + System.out.println(currentWeatherData.getMain().getTemp_min()); + System.out.println(currentWeatherData.getBase()); + System.out.println(currentWeatherData.getCod()); + System.out.println(currentWeatherData.getName()); + System.out.println(currentWeatherData.getId()); + System.out.println(currentWeatherData.getDt()); + } + + public static void getForecastWeatherData(final ForecastWeatherData forecastWeatherData, + final JsonParser jParser) throws JsonParseException, IOException { + if (jParser.nextToken() == JsonToken.START_OBJECT) { + + while (jParser.nextToken() != JsonToken.END_OBJECT) { + final String fieldname = jParser.getCurrentName(); + final JsonToken nextToken = jParser.nextToken(); + if (nextToken == JsonToken.START_OBJECT) { + getForecastWeatherDataObjects(forecastWeatherData, jParser, fieldname); + } + if (nextToken == JsonToken.START_ARRAY) { + JsonToken tokenNext = jParser.nextToken(); + while (tokenNext != JsonToken.END_ARRAY) { + if (tokenNext == JsonToken.START_OBJECT) { + getForecastWeatherDataObjects(forecastWeatherData, jParser, fieldname); + } + tokenNext = jParser.nextToken(); + } + } + if ((nextToken == JsonToken.VALUE_NUMBER_INT) + || (nextToken == JsonToken.VALUE_STRING)) { + getForecastWeatherDataObjects(forecastWeatherData, jParser, fieldname); + } + } + } + } + + 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(); + final JsonToken nextToken = jParser.nextToken(); // move to value + 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(); + jParser.nextToken(); // move to value + 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()); + forecastWeatherData.getList().add(list); + while (jParser.nextToken() != JsonToken.END_OBJECT) { + final String namefield = jParser.getCurrentName(); + final JsonToken nextToken = jParser.nextToken(); // move to value + 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(); + jParser.nextToken(); // move to value + 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(); + jParser.nextToken(); // move to value + + 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); + } + } + + public static void printForecastWeatherData(final ForecastWeatherData forecastWeatherData) { + System.out.println("cnt " + forecastWeatherData.getCnt()); + System.out.println("cod " + forecastWeatherData.getCod()); + System.out.println("city country " + forecastWeatherData.getCity().getCountry()); + System.out.println("city name " + forecastWeatherData.getCity().getName()); + System.out.println("city coord lat " + forecastWeatherData.getCity().getCoord().getLon()); + System.out.println("city population " + forecastWeatherData.getCity().getCoord().getLat()); + System.out.println("city id " + forecastWeatherData.getCity().getPopulation()); + System.out.println("city id " + forecastWeatherData.getCity().getId()); + System.out.println("cnt " + forecastWeatherData.getCnt()); + final int cnt = (Integer) forecastWeatherData.getCnt(); + for (int i = 0; i < cnt; i++) { + System.out.println("FORECAST LIST NUMBER: " + i); + if (forecastWeatherData.getList().get(i).getWeather().size() > 0) { + System.out.println("list weather description " + forecastWeatherData.getList().get(i).getWeather().get(0).getDescription()); + System.out.println("list weather id " + forecastWeatherData.getList().get(i).getWeather().get(0).getId()); + System.out.println("list weather main " + forecastWeatherData.getList().get(i).getWeather().get(0).getMain()); + System.out.println("list weather icon " + forecastWeatherData.getList().get(i).getWeather().get(0).getIcon()); + } + System.out.println("list clouds " + forecastWeatherData.getList().get(i).getClouds()); + System.out.println("list deg " + forecastWeatherData.getList().get(i).getDeg()); + System.out.println("list dt " + forecastWeatherData.getList().get(i).getDt()); + System.out.println("list humidity " + forecastWeatherData.getList().get(i).getHumidity()); + System.out.println("list pressure " + forecastWeatherData.getList().get(i).getPressure()); + System.out.println("list rain " + forecastWeatherData.getList().get(i).getRain()); + System.out.println("list snow " + forecastWeatherData.getList().get(i).getSnow()); + System.out.println("list speed " + forecastWeatherData.getList().get(i).getSpeed()); + System.out.println("list temp day " + forecastWeatherData.getList().get(i).getTemp().getDay()); + System.out.println("list temp eve " + forecastWeatherData.getList().get(i).getTemp().getEve()); + System.out.println("list temp max " + forecastWeatherData.getList().get(i).getTemp().getMax()); + System.out.println("list temp min " + forecastWeatherData.getList().get(i).getTemp().getMin()); + System.out.println("list temp morn " + forecastWeatherData.getList().get(i).getTemp().getMorn()); + System.out.println("list temp night " + forecastWeatherData.getList().get(i).getTemp().getNight()); + } + } +} -- 2.1.4