From: David ‘Bombe’ Roden Date: Fri, 27 May 2016 22:11:41 +0000 (+0200) Subject: Make wetter.com state JSON-serializable X-Git-Tag: v2~134 X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=6a0f7045a1d164428679b04c87103ba861297fc1;p=rhynodge.git Make wetter.com state JSON-serializable --- diff --git a/src/main/kotlin/net/pterodactylus/rhynodge/webpages/weather/wettercom/HourState.kt b/src/main/kotlin/net/pterodactylus/rhynodge/webpages/weather/wettercom/HourState.kt index 8ef39cb..f5ddfc2 100644 --- a/src/main/kotlin/net/pterodactylus/rhynodge/webpages/weather/wettercom/HourState.kt +++ b/src/main/kotlin/net/pterodactylus/rhynodge/webpages/weather/wettercom/HourState.kt @@ -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) { diff --git a/src/main/kotlin/net/pterodactylus/rhynodge/webpages/weather/wettercom/WetterComState.kt b/src/main/kotlin/net/pterodactylus/rhynodge/webpages/weather/wettercom/WetterComState.kt index 59147eb..be18847 100644 --- a/src/main/kotlin/net/pterodactylus/rhynodge/webpages/weather/wettercom/WetterComState.kt +++ b/src/main/kotlin/net/pterodactylus/rhynodge/webpages/weather/wettercom/WetterComState.kt @@ -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 { + constructor(@JsonProperty("dateTime") time: Long) : + this(Instant.ofEpochMilli(time).atZone(ZoneId.of("Europe/Berlin"))) + + @JsonProperty("hours") val hours: List = mutableListOf() + val timeMillis: Long + @JsonGetter("dateTime") + get() = dateTime.toInstant().toEpochMilli() + fun addHour(hourState: HourState) { (hours as MutableList).add(hourState) } diff --git a/src/test/kotlin/net/pterodactylus/rhynodge/webpages/weather/wettercom/WetterComStateTest.kt b/src/test/kotlin/net/pterodactylus/rhynodge/webpages/weather/wettercom/WetterComStateTest.kt index 149b9a2..cdab7b6 100644 --- a/src/test/kotlin/net/pterodactylus/rhynodge/webpages/weather/wettercom/WetterComStateTest.kt +++ b/src/test/kotlin/net/pterodactylus/rhynodge/webpages/weather/wettercom/WetterComStateTest.kt @@ -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)) } + @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)) + } + }