Skip to content

Commit

Permalink
add duration to JSON auto-marshalling
Browse files Browse the repository at this point in the history
  • Loading branch information
daviddenton committed Aug 12, 2018
1 parent 5a9c683 commit d03d95d
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 3 deletions.
Expand Up @@ -6,6 +6,7 @@ import com.natpryce.hamkrest.throws
import org.http4k.core.Uri
import org.junit.jupiter.api.Test
import java.net.URL
import java.time.Duration
import java.time.Instant
import java.time.LocalDate
import java.time.LocalDateTime
Expand All @@ -14,7 +15,17 @@ import java.time.ZoneId
import java.time.ZonedDateTime
import java.util.UUID

data class CommonJdkPrimitives(val localDate: LocalDate, val localTime: LocalTime, val localDateTime: LocalDateTime, val zonedDateTime: ZonedDateTime, val instant: Instant, val uuid: UUID, val uri: Uri, val url: URL)
data class CommonJdkPrimitives(
val duration: Duration,
val localDate: LocalDate,
val localTime: LocalTime,
val localDateTime: LocalDateTime,
val zonedDateTime: ZonedDateTime,
val instant: Instant,
val uuid: UUID,
val uri: Uri,
val url: URL
)

data class ArbObject(val string: String, val child: ArbObject?, val numbers: List<Int>, val bool: Boolean)

Expand All @@ -23,7 +34,7 @@ data class RegexHolder(val regex: Regex)
abstract class AutoMarshallingContract(private val j: AutoMarshallingJson) {

protected open val expectedAutoMarshallingResult = """{"string":"hello","child":{"string":"world","child":null,"numbers":[1],"bool":true},"numbers":[],"bool":false}"""
protected open val expectedAutoMarshallingResultPrimitives = """{"localDate":"2000-01-01","localTime":"01:01:01","localDateTime":"2000-01-01T01:01:01","zonedDateTime":"2000-01-01T01:01:01Z[UTC]","instant":"1970-01-01T00:00:00Z","uuid":"1a448854-1687-4f90-9562-7d527d64383c","uri":"http://uri:8000","url":"http://url:9000"}"""
protected open val expectedAutoMarshallingResultPrimitives = """{"duration":"PT1S","localDate":"2000-01-01","localTime":"01:01:01","localDateTime":"2000-01-01T01:01:01","zonedDateTime":"2000-01-01T01:01:01Z[UTC]","instant":"1970-01-01T00:00:00Z","uuid":"1a448854-1687-4f90-9562-7d527d64383c","uri":"http://uri:8000","url":"http://url:9000"}"""

val obj = ArbObject("hello", ArbObject("world", null, listOf(1), true), emptyList(), false)

Expand All @@ -43,7 +54,7 @@ abstract class AutoMarshallingContract(private val j: AutoMarshallingJson) {
fun `roundtrip object with common java primitive types`() {
val localDate = LocalDate.of(2000, 1, 1)
val localTime = LocalTime.of(1, 1, 1)
val obj = CommonJdkPrimitives(localDate, localTime, LocalDateTime.of(localDate, localTime), ZonedDateTime.of(localDate, localTime, ZoneId.of("UTC")), Instant.EPOCH, UUID.fromString("1a448854-1687-4f90-9562-7d527d64383c"), Uri.of("http://uri:8000"), URL("http://url:9000"))
val obj = CommonJdkPrimitives(Duration.ofMillis(1000), localDate, localTime, LocalDateTime.of(localDate, localTime), ZonedDateTime.of(localDate, localTime, ZoneId.of("UTC")), Instant.EPOCH, UUID.fromString("1a448854-1687-4f90-9562-7d527d64383c"), Uri.of("http://uri:8000"), URL("http://url:9000"))
val out = j.asJsonString(obj)
assertThat(out, equalTo(expectedAutoMarshallingResultPrimitives))
assertThat(j.asA(out, CommonJdkPrimitives::class), equalTo(obj))
Expand Down
2 changes: 2 additions & 0 deletions http4k-format-gson/src/main/kotlin/org/http4k/format/Gson.kt
Expand Up @@ -22,6 +22,7 @@ import java.lang.reflect.Type
import java.math.BigDecimal
import java.math.BigInteger
import java.net.URL
import java.time.Duration
import java.time.Instant
import java.time.LocalDate
import java.time.LocalDateTime
Expand Down Expand Up @@ -101,6 +102,7 @@ open class ConfigurableGson(builder: GsonBuilder) : JsonLibAutoMarshallingJson<J
}

object Gson : ConfigurableGson(GsonBuilder()
.registerTypeAdapter(Duration::class.java, custom(Duration::parse))
.registerTypeAdapter(LocalTime::class.java, custom({ LocalTime.parse(it, DateTimeFormatter.ISO_LOCAL_TIME) }, DateTimeFormatter.ISO_LOCAL_TIME::format))
.registerTypeAdapter(LocalDate::class.java, custom({ LocalDate.parse(it, DateTimeFormatter.ISO_DATE) }, DateTimeFormatter.ISO_DATE::format))
.registerTypeAdapter(LocalDateTime::class.java, custom({ LocalDateTime.parse(it, DateTimeFormatter.ISO_LOCAL_DATE_TIME) }, DateTimeFormatter.ISO_LOCAL_DATE_TIME::format))
Expand Down
Expand Up @@ -30,6 +30,7 @@ import org.http4k.websocket.WsMessage
import java.math.BigDecimal
import java.math.BigInteger
import java.net.URL
import java.time.Duration
import java.time.Instant
import java.time.LocalDate
import java.time.LocalDateTime
Expand Down Expand Up @@ -97,6 +98,7 @@ open class ConfigurableJackson(private val mapper: ObjectMapper) : JsonLibAutoMa
}

val defaultKotlinModuleWithHttp4kSerialisers = KotlinModule()
.custom(Duration::parse)
.custom({ LocalTime.parse(it, DateTimeFormatter.ISO_LOCAL_TIME) }, DateTimeFormatter.ISO_LOCAL_TIME::format)
.custom({ LocalDate.parse(it, DateTimeFormatter.ISO_DATE) }, DateTimeFormatter.ISO_DATE::format)
.custom({ LocalDateTime.parse(it, DateTimeFormatter.ISO_LOCAL_DATE_TIME) }, DateTimeFormatter.ISO_LOCAL_DATE_TIME::format)
Expand Down
Expand Up @@ -15,6 +15,7 @@ import org.http4k.lens.ContentNegotiation.Companion.None
import org.http4k.lens.string
import org.http4k.websocket.WsMessage
import java.net.URL
import java.time.Duration
import java.time.Instant
import java.time.LocalDate
import java.time.LocalDateTime
Expand Down Expand Up @@ -46,6 +47,7 @@ open class ConfigurableMoshi(builder: Moshi.Builder) : AutoMarshallingJson() {
}

object Moshi : ConfigurableMoshi(Moshi.Builder()
.add(custom(Duration::parse))
.add(custom({ LocalTime.parse(it, DateTimeFormatter.ISO_LOCAL_TIME) }, DateTimeFormatter.ISO_LOCAL_TIME::format))
.add(custom({ LocalDate.parse(it, DateTimeFormatter.ISO_DATE) }, DateTimeFormatter.ISO_DATE::format))
.add(custom({ LocalDateTime.parse(it, DateTimeFormatter.ISO_LOCAL_DATE_TIME) }, DateTimeFormatter.ISO_LOCAL_DATE_TIME::format))
Expand Down

0 comments on commit d03d95d

Please sign in to comment.