Working with Jackson. Useful in WeatherInformatip Android app.
authorgu.martinm@gmail.com <gu.martinm@gmail.com>
Fri, 11 Apr 2014 16:08:13 +0000 (18:08 +0200)
committergu.martinm@gmail.com <gu.martinm@gmail.com>
Fri, 11 Apr 2014 16:08:13 +0000 (18:08 +0200)
14 files changed:
Allgemeines/Jackson/build.gradle [new file with mode: 0644]
Allgemeines/Jackson/src/main/java/de/example/jackson/auto/Clouds.java [new file with mode: 0644]
Allgemeines/Jackson/src/main/java/de/example/jackson/auto/Coord.java [new file with mode: 0644]
Allgemeines/Jackson/src/main/java/de/example/jackson/auto/JacksonDataBindingTestMain.java [new file with mode: 0644]
Allgemeines/Jackson/src/main/java/de/example/jackson/auto/Main.java [new file with mode: 0644]
Allgemeines/Jackson/src/main/java/de/example/jackson/auto/Sys.java [new file with mode: 0644]
Allgemeines/Jackson/src/main/java/de/example/jackson/auto/Weather.java [new file with mode: 0644]
Allgemeines/Jackson/src/main/java/de/example/jackson/auto/WeatherData.java [new file with mode: 0644]
Allgemeines/Jackson/src/main/java/de/example/jackson/auto/Wind.java [new file with mode: 0644]
Allgemeines/Jackson/src/main/java/de/example/jackson/databinding/Coord.java [new file with mode: 0644]
Allgemeines/Jackson/src/main/java/de/example/jackson/databinding/JacksonDataBindingTestMain.java [new file with mode: 0644]
Allgemeines/Jackson/src/main/java/de/example/jackson/databinding/Weather.java [new file with mode: 0644]
Allgemeines/Jackson/src/main/java/de/example/jackson/databinding/WeatherDataPOJO.java [new file with mode: 0644]
Allgemeines/Jackson/src/main/java/de/example/jackson/databinding/Wind.java [new file with mode: 0644]

diff --git a/Allgemeines/Jackson/build.gradle b/Allgemeines/Jackson/build.gradle
new file mode 100644 (file)
index 0000000..ee630e2
--- /dev/null
@@ -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 (file)
index 0000000..c762e80
--- /dev/null
@@ -0,0 +1,13 @@
+package de.example.jackson.auto;\r
+\r
+\r
+public class Clouds{\r
+    private Number all;\r
+\r
+    public Number getAll(){\r
+        return this.all;\r
+    }\r
+    public void setAll(final Number all){\r
+        this.all = all;\r
+    }\r
+}\r
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 (file)
index 0000000..193b964
--- /dev/null
@@ -0,0 +1,20 @@
+package de.example.jackson.auto;\r
+\r
+\r
+public class Coord{\r
+    private Number lat;\r
+    private Number lon;\r
+\r
+    public Number getLat(){\r
+        return this.lat;\r
+    }\r
+    public void setLat(final Number lat){\r
+        this.lat = lat;\r
+    }\r
+    public Number getLon(){\r
+        return this.lon;\r
+    }\r
+    public void setLon(final Number lon){\r
+        this.lon = lon;\r
+    }\r
+}\r
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 (file)
index 0000000..6a6be41
--- /dev/null
@@ -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 (file)
index 0000000..547e6e6
--- /dev/null
@@ -0,0 +1,55 @@
+package de.example.jackson.auto;\r
+\r
+\r
+public class Main{\r
+    private Number grnd_level;\r
+    private Number humidity;\r
+    private Number pressure;\r
+    private Number sea_level;\r
+    private Number temp;\r
+    private Number temp_max;\r
+    private Number temp_min;\r
+\r
+    public Number getGrnd_level(){\r
+        return this.grnd_level;\r
+    }\r
+    public void setGrnd_level(final Number grnd_level){\r
+        this.grnd_level = grnd_level;\r
+    }\r
+    public Number getHumidity(){\r
+        return this.humidity;\r
+    }\r
+    public void setHumidity(final Number humidity){\r
+        this.humidity = humidity;\r
+    }\r
+    public Number getPressure(){\r
+        return this.pressure;\r
+    }\r
+    public void setPressure(final Number pressure){\r
+        this.pressure = pressure;\r
+    }\r
+    public Number getSea_level(){\r
+        return this.sea_level;\r
+    }\r
+    public void setSea_level(final Number sea_level){\r
+        this.sea_level = sea_level;\r
+    }\r
+    public Number getTemp(){\r
+        return this.temp;\r
+    }\r
+    public void setTemp(final Number temp){\r
+        this.temp = temp;\r
+    }\r
+    public Number getTemp_max(){\r
+        return this.temp_max;\r
+    }\r
+    public void setTemp_max(final Number temp_max){\r
+        this.temp_max = temp_max;\r
+    }\r
+    public Number getTemp_min(){\r
+        return this.temp_min;\r
+    }\r
+    public void setTemp_min(final Number temp_min){\r
+        this.temp_min = temp_min;\r
+    }\r
+}\r
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 (file)
index 0000000..fe008cc
--- /dev/null
@@ -0,0 +1,34 @@
+package de.example.jackson.auto;\r
+\r
+\r
+public class Sys{\r
+    private String country;\r
+    private Number message;\r
+    private Number sunrise;\r
+    private Number sunset;\r
+\r
+    public String getCountry(){\r
+        return this.country;\r
+    }\r
+    public void setCountry(final String country){\r
+        this.country = country;\r
+    }\r
+    public Number getMessage(){\r
+        return this.message;\r
+    }\r
+    public void setMessage(final Number message){\r
+        this.message = message;\r
+    }\r
+    public Number getSunrise(){\r
+        return this.sunrise;\r
+    }\r
+    public void setSunrise(final Number sunrise){\r
+        this.sunrise = sunrise;\r
+    }\r
+    public Number getSunset(){\r
+        return this.sunset;\r
+    }\r
+    public void setSunset(final Number sunset){\r
+        this.sunset = sunset;\r
+    }\r
+}\r
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 (file)
index 0000000..6193caa
--- /dev/null
@@ -0,0 +1,34 @@
+package de.example.jackson.auto;\r
+\r
+\r
+public class Weather{\r
+    private String description;\r
+    private String icon;\r
+    private Number id;\r
+    private String main;\r
+\r
+    public String getDescription(){\r
+        return this.description;\r
+    }\r
+    public void setDescription(final String description){\r
+        this.description = description;\r
+    }\r
+    public String getIcon(){\r
+        return this.icon;\r
+    }\r
+    public void setIcon(final String icon){\r
+        this.icon = icon;\r
+    }\r
+    public Number getId(){\r
+        return this.id;\r
+    }\r
+    public void setId(final Number id){\r
+        this.id = id;\r
+    }\r
+    public String getMain(){\r
+        return this.main;\r
+    }\r
+    public void setMain(final String main){\r
+        this.main = main;\r
+    }\r
+}\r
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 (file)
index 0000000..79ddd9b
--- /dev/null
@@ -0,0 +1,84 @@
+package de.example.jackson.auto;\r
+\r
+import java.util.List;\r
+\r
+public class WeatherData{\r
+    private String base;\r
+    private Clouds clouds;\r
+    private Number cod;\r
+    private Coord coord;\r
+    private Number dt;\r
+    private Number id;\r
+    private Main main;\r
+    private String name;\r
+    private Sys sys;\r
+    private List<Weather> weather;\r
+    private Wind wind;\r
+\r
+    public String getBase(){\r
+        return this.base;\r
+    }\r
+    public void setBase(final String base){\r
+        this.base = base;\r
+    }\r
+    public Clouds getClouds(){\r
+        return this.clouds;\r
+    }\r
+    public void setClouds(final Clouds clouds){\r
+        this.clouds = clouds;\r
+    }\r
+    public Number getCod(){\r
+        return this.cod;\r
+    }\r
+    public void setCod(final Number cod){\r
+        this.cod = cod;\r
+    }\r
+    public Coord getCoord(){\r
+        return this.coord;\r
+    }\r
+    public void setCoord(final Coord coord){\r
+        this.coord = coord;\r
+    }\r
+    public Number getDt(){\r
+        return this.dt;\r
+    }\r
+    public void setDt(final Number dt){\r
+        this.dt = dt;\r
+    }\r
+    public Number getId(){\r
+        return this.id;\r
+    }\r
+    public void setId(final Number id){\r
+        this.id = id;\r
+    }\r
+    public Main getMain(){\r
+        return this.main;\r
+    }\r
+    public void setMain(final Main main){\r
+        this.main = main;\r
+    }\r
+    public String getName(){\r
+        return this.name;\r
+    }\r
+    public void setName(final String name){\r
+        this.name = name;\r
+    }\r
+    public Sys getSys(){\r
+        return this.sys;\r
+    }\r
+    public void setSys(final Sys sys){\r
+        this.sys = sys;\r
+    }\r
+    public List<Weather> getWeather(){\r
+        return this.weather;\r
+    }\r
+    public void setWeather(final List<Weather> weather){\r
+        this.weather = weather;\r
+    }\r
+    public Wind getWind(){\r
+        return this.wind;\r
+    }\r
+    public void setWind(final Wind wind){\r
+        this.wind = wind;\r
+    }\r
+}\r
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 (file)
index 0000000..e821612
--- /dev/null
@@ -0,0 +1,20 @@
+package de.example.jackson.auto;\r
+\r
+\r
+public class Wind{\r
+    private Number deg;\r
+    private Number speed;\r
+\r
+    public Number getDeg(){\r
+        return this.deg;\r
+    }\r
+    public void setDeg(final Number deg){\r
+        this.deg = deg;\r
+    }\r
+    public Number getSpeed(){\r
+        return this.speed;\r
+    }\r
+    public void setSpeed(final Number speed){\r
+        this.speed = speed;\r
+    }\r
+}\r
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 (file)
index 0000000..97be147
--- /dev/null
@@ -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 (file)
index 0000000..8ed637e
--- /dev/null
@@ -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 (file)
index 0000000..0feb735
--- /dev/null
@@ -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 (file)
index 0000000..b75fcdc
--- /dev/null
@@ -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 (file)
index 0000000..11c80fa
--- /dev/null
@@ -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;
+    }
+}