Make wetter.com state JSON-serializable
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Fri, 27 May 2016 22:11:41 +0000 (00:11 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Fri, 27 May 2016 22:11:41 +0000 (00:11 +0200)
src/main/kotlin/net/pterodactylus/rhynodge/webpages/weather/wettercom/HourState.kt
src/main/kotlin/net/pterodactylus/rhynodge/webpages/weather/wettercom/WetterComState.kt
src/test/kotlin/net/pterodactylus/rhynodge/webpages/weather/wettercom/WetterComStateTest.kt

index 8ef39cb..f5ddfc2 100644 (file)
@@ -1,11 +1,21 @@
 package net.pterodactylus.rhynodge.webpages.weather.wettercom
 
+import com.fasterxml.jackson.annotation.JsonProperty
+
 /**
  * Container for weather conditions of a single hour.
  *
  * @author [David ‘Bombe’ Roden](mailto:bombe@pterodactylus.net)
  */
-data class HourState(val hourIndex: Int, val temperature: Double, val rainProbability: Double, val rainAmount: Double, val windDirection: WindDirection, val windSpeed: Double, val description: String, val image: String) {
+data class HourState(
+        @JsonProperty("hourIndex") val hourIndex: Int,
+        @JsonProperty("temperature") val temperature: Double,
+        @JsonProperty("rainProbability") val rainProbability: Double,
+        @JsonProperty("rainAmount") val rainAmount: Double,
+        @JsonProperty("windDirection") val windDirection: WindDirection,
+        @JsonProperty("windSpeed") val windSpeed: Double,
+        @JsonProperty("description") val description: String,
+        @JsonProperty("image") val image: String) {
 
     class Builder(private val hourIndex: Int) {
 
index 59147eb..be18847 100644 (file)
@@ -1,6 +1,10 @@
 package net.pterodactylus.rhynodge.webpages.weather.wettercom
 
+import com.fasterxml.jackson.annotation.JsonGetter
+import com.fasterxml.jackson.annotation.JsonProperty
 import net.pterodactylus.rhynodge.states.AbstractState
+import java.time.Instant
+import java.time.ZoneId
 import java.time.ZonedDateTime
 
 /**
@@ -10,8 +14,16 @@ import java.time.ZonedDateTime
  */
 class WetterComState(val dateTime: ZonedDateTime) : AbstractState(true), Iterable<HourState> {
 
+    constructor(@JsonProperty("dateTime") time: Long) :
+    this(Instant.ofEpochMilli(time).atZone(ZoneId.of("Europe/Berlin")))
+
+    @JsonProperty("hours")
     val hours: List<HourState> = mutableListOf()
 
+    val timeMillis: Long
+        @JsonGetter("dateTime")
+        get() = dateTime.toInstant().toEpochMilli()
+
     fun addHour(hourState: HourState) {
         (hours as MutableList<HourState>).add(hourState)
     }
index 149b9a2..cdab7b6 100644 (file)
@@ -1,5 +1,6 @@
 package net.pterodactylus.rhynodge.webpages.weather.wettercom
 
+import com.fasterxml.jackson.databind.ObjectMapper
 import org.hamcrest.MatcherAssert.assertThat
 import org.hamcrest.Matchers.`is`
 import org.junit.Test
@@ -43,4 +44,17 @@ class WetterComStateTest {
         assertThat(firstState.iterator().asSequence().toList(), `is`(firstState.hours as Iterable<HourState>))
     }
 
+    @Test
+    fun stateIsSerializableAsJson() {
+        val objectMapper = ObjectMapper()
+        val now = Instant.now().atZone(ZoneId.of("Europe/Berlin"))
+        val originalState = WetterComState(ZonedDateTime.from(now))
+        originalState.addHour(HourState(0, 10.0, 0.05, 0.0, WindDirection.NORTH, 5.0, "Fine", "http://1"))
+        originalState.addHour(HourState(1, 12.0, 0.1, 2.0, WindDirection.WEST, 8.0, "Superb", "http://2"))
+        val json = objectMapper.writeValueAsString(originalState)
+        println(json)
+        val parsedState = objectMapper.readValue(json, WetterComState::class.java)
+        assertThat(parsedState, `is`(originalState))
+    }
+
 }