From ff301837249bc9165ee2b1f43b257f8efed2052d Mon Sep 17 00:00:00 2001 From: "gu.martinm@gmail.com" Date: Fri, 11 Apr 2014 18:08:13 +0200 Subject: [PATCH] Working with Jackson. Useful in WeatherInformatip Android app. --- Allgemeines/Jackson/build.gradle | 39 ++++++++++ .../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 ++++++ .../java/de/example/jackson/databinding/Coord.java | 24 +++++++ .../databinding/JacksonDataBindingTestMain.java | 76 ++++++++++++++++++++ .../de/example/jackson/databinding/Weather.java | 39 ++++++++++ .../jackson/databinding/WeatherDataPOJO.java | 35 +++++++++ .../java/de/example/jackson/databinding/Wind.java | 55 ++++++++++++++ 14 files changed, 604 insertions(+) create mode 100644 Allgemeines/Jackson/build.gradle create mode 100644 Allgemeines/Jackson/src/main/java/de/example/jackson/auto/Clouds.java create mode 100644 Allgemeines/Jackson/src/main/java/de/example/jackson/auto/Coord.java create mode 100644 Allgemeines/Jackson/src/main/java/de/example/jackson/auto/JacksonDataBindingTestMain.java create mode 100644 Allgemeines/Jackson/src/main/java/de/example/jackson/auto/Main.java create mode 100644 Allgemeines/Jackson/src/main/java/de/example/jackson/auto/Sys.java create mode 100644 Allgemeines/Jackson/src/main/java/de/example/jackson/auto/Weather.java create mode 100644 Allgemeines/Jackson/src/main/java/de/example/jackson/auto/WeatherData.java create mode 100644 Allgemeines/Jackson/src/main/java/de/example/jackson/auto/Wind.java create mode 100644 Allgemeines/Jackson/src/main/java/de/example/jackson/databinding/Coord.java create mode 100644 Allgemeines/Jackson/src/main/java/de/example/jackson/databinding/JacksonDataBindingTestMain.java create mode 100644 Allgemeines/Jackson/src/main/java/de/example/jackson/databinding/Weather.java create mode 100644 Allgemeines/Jackson/src/main/java/de/example/jackson/databinding/WeatherDataPOJO.java create mode 100644 Allgemeines/Jackson/src/main/java/de/example/jackson/databinding/Wind.java diff --git a/Allgemeines/Jackson/build.gradle b/Allgemeines/Jackson/build.gradle new file mode 100644 index 0000000..ee630e2 --- /dev/null +++ b/Allgemeines/Jackson/build.gradle @@ -0,0 +1,39 @@ +apply plugin: 'java' +apply plugin: 'eclipse' + +sourceCompatibility = 1.6 +version = '1.0' + +dependencies { + compile group: 'com.fasterxml.jackson.core', name: 'jackson-core', version: '2.3.3' + compile group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.3.3' + testCompile group: 'junit', name: 'junit', version: '4.+' +} + +jar { + manifest { + attributes 'Implementation-Title': 'Jackson Streaming', 'Implementation-Version': version + } +} + +repositories { + maven { + mavenLocal() + mavenCentral() + // Look for POMs and artifacts, such as JARs, here + url "http://gumartinm.name/fakerepository" + } +} + + +uploadArchives { + repositories { + maven { + credentials { + username "gustavo" + password "meinpasswd" + } + url "http://gumartinm.name/fakerepository" + } + } +} 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 new file mode 100644 index 0000000..c762e80 --- /dev/null +++ b/Allgemeines/Jackson/src/main/java/de/example/jackson/auto/Clouds.java @@ -0,0 +1,13 @@ +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 new file mode 100644 index 0000000..193b964 --- /dev/null +++ b/Allgemeines/Jackson/src/main/java/de/example/jackson/auto/Coord.java @@ -0,0 +1,20 @@ +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 new file mode 100644 index 0000000..6a6be41 --- /dev/null +++ b/Allgemeines/Jackson/src/main/java/de/example/jackson/auto/JacksonDataBindingTestMain.java @@ -0,0 +1,76 @@ +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 new file mode 100644 index 0000000..547e6e6 --- /dev/null +++ b/Allgemeines/Jackson/src/main/java/de/example/jackson/auto/Main.java @@ -0,0 +1,55 @@ +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 new file mode 100644 index 0000000..fe008cc --- /dev/null +++ b/Allgemeines/Jackson/src/main/java/de/example/jackson/auto/Sys.java @@ -0,0 +1,34 @@ +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 new file mode 100644 index 0000000..6193caa --- /dev/null +++ b/Allgemeines/Jackson/src/main/java/de/example/jackson/auto/Weather.java @@ -0,0 +1,34 @@ +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 new file mode 100644 index 0000000..79ddd9b --- /dev/null +++ b/Allgemeines/Jackson/src/main/java/de/example/jackson/auto/WeatherData.java @@ -0,0 +1,84 @@ +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 new file mode 100644 index 0000000..e821612 --- /dev/null +++ b/Allgemeines/Jackson/src/main/java/de/example/jackson/auto/Wind.java @@ -0,0 +1,20 @@ +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/databinding/Coord.java b/Allgemeines/Jackson/src/main/java/de/example/jackson/databinding/Coord.java new file mode 100644 index 0000000..97be147 --- /dev/null +++ b/Allgemeines/Jackson/src/main/java/de/example/jackson/databinding/Coord.java @@ -0,0 +1,24 @@ +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 new file mode 100644 index 0000000..8ed637e --- /dev/null +++ b/Allgemeines/Jackson/src/main/java/de/example/jackson/databinding/JacksonDataBindingTestMain.java @@ -0,0 +1,76 @@ +package de.example.jackson.databinding; + +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: + WeatherDataPOJO weatherDataPOJO = null; + try { + weatherDataPOJO = mapper.readValue(dataB.getBytes("UTF-8"), WeatherDataPOJO.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/databinding/Weather.java b/Allgemeines/Jackson/src/main/java/de/example/jackson/databinding/Weather.java new file mode 100644 index 0000000..0feb735 --- /dev/null +++ b/Allgemeines/Jackson/src/main/java/de/example/jackson/databinding/Weather.java @@ -0,0 +1,39 @@ +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 new file mode 100644 index 0000000..b75fcdc --- /dev/null +++ b/Allgemeines/Jackson/src/main/java/de/example/jackson/databinding/WeatherDataPOJO.java @@ -0,0 +1,35 @@ +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 new file mode 100644 index 0000000..11c80fa --- /dev/null +++ b/Allgemeines/Jackson/src/main/java/de/example/jackson/databinding/Wind.java @@ -0,0 +1,55 @@ +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; + } +} -- 2.1.4