From 3df918ca17d8cc528d42185001f337aef94c4330 Mon Sep 17 00:00:00 2001 From: Miha Krajnc Date: Sat, 23 Mar 2019 12:16:28 +0100 Subject: [PATCH] Add Kotlin extension functions support Added support for extension functions for the Converters class logic. --- .idea/codeStyles/codeStyleConfig.xml | 5 ++ gson-javatime-serialisers.iml | 30 ++++++- pom.xml | 72 ++++++++++++--- .../gsonjavatime/KotlinConverters.kt | 90 +++++++++++++++++++ .../gsonjavatime/KotlinConvertersTest.kt | 90 +++++++++++++++++++ 5 files changed, 276 insertions(+), 11 deletions(-) create mode 100644 .idea/codeStyles/codeStyleConfig.xml create mode 100644 src/main/java/com/fatboyindustrial/gsonjavatime/KotlinConverters.kt create mode 100644 src/test/java/com/fatboyindustrial/gsonjavatime/KotlinConvertersTest.kt diff --git a/.idea/codeStyles/codeStyleConfig.xml b/.idea/codeStyles/codeStyleConfig.xml new file mode 100644 index 0000000..a55e7a1 --- /dev/null +++ b/.idea/codeStyles/codeStyleConfig.xml @@ -0,0 +1,5 @@ + + + + \ No newline at end of file diff --git a/gson-javatime-serialisers.iml b/gson-javatime-serialisers.iml index f9c958e..1e0929b 100644 --- a/gson-javatime-serialisers.iml +++ b/gson-javatime-serialisers.iml @@ -1,6 +1,27 @@ - + + + + + + + + + + + + + @@ -14,5 +35,12 @@ + + + + + + + \ No newline at end of file diff --git a/pom.xml b/pom.xml index dcaf688..ab3c4a5 100644 --- a/pom.xml +++ b/pom.xml @@ -59,20 +59,12 @@ UTF-8 + 1.3.21 - - org.apache.maven.plugins - maven-compiler-plugin - 3.0 - - 1.8 - 1.8 - - - + org.apache.maven.plugins maven-release-plugin 2.2.2 @@ -128,6 +120,55 @@ + + org.jetbrains.kotlin + kotlin-maven-plugin + ${kotlin.version} + + + compile + compile + + compile + + + + test-compile + test-compile + + test-compile + + + + + 1.8 + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.0 + + + compile + compile + + compile + + + + testCompile + test-compile + + testCompile + + + + + 1.8 + 1.8 + + @@ -195,6 +236,17 @@ gson 2.3.1 + + org.jetbrains.kotlin + kotlin-stdlib-jdk8 + ${kotlin.version} + + + org.jetbrains.kotlin + kotlin-test + ${kotlin.version} + test + diff --git a/src/main/java/com/fatboyindustrial/gsonjavatime/KotlinConverters.kt b/src/main/java/com/fatboyindustrial/gsonjavatime/KotlinConverters.kt new file mode 100644 index 0000000..138b3c6 --- /dev/null +++ b/src/main/java/com/fatboyindustrial/gsonjavatime/KotlinConverters.kt @@ -0,0 +1,90 @@ +/* + * Copyright 2014 Greg Kopff + * All rights reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package com.fatboyindustrial.gsonjavatime + +import com.google.gson.GsonBuilder +import java.time.* + +/** + * Registers all Java Time converters onto the receiver. + * + * @see Converters.registerAll + */ +fun GsonBuilder.registerAllJavaConverters(): GsonBuilder = this + .registerLocalDate() + .registerLocalDateTime() + .registerLocalTime() + .registerOffsetDateTime() + .registerOffsetTime() + .registerZonedDateTime() + .registerInstant() + +/** + * Registers the [LocalDate] converter onto the receiver. + * + * @see Converters.registerLocalDate + */ +fun GsonBuilder.registerLocalDate(): GsonBuilder = Converters.registerLocalDate(this) + +/** + * Registers the [LocalDateTime] converter onto the receiver. + * + * @see Converters.registerLocalDateTime + */ +fun GsonBuilder.registerLocalDateTime(): GsonBuilder = Converters.registerLocalDateTime(this) + +/** + * Registers the [LocalTime] converter onto the receiver. + * + * @see Converters.registerLocalTime + */ +fun GsonBuilder.registerLocalTime(): GsonBuilder = Converters.registerLocalTime(this) + +/** + * Registers the [OffsetDateTime\] converter onto the receiver. + * + * @see Converters.registerOffsetDateTime + */ +fun GsonBuilder.registerOffsetDateTime(): GsonBuilder = Converters.registerOffsetDateTime(this) + +/** + * Registers the [OffsetTime] converter onto the receiver. + * + * @see Converters.registerOffsetTime + */ +fun GsonBuilder.registerOffsetTime(): GsonBuilder = Converters.registerOffsetTime(this) + +/** + * Registers the [ZonedDateTime] converter onto the receiver. + * + * @see Converters.registerZonedDateTime + */ +fun GsonBuilder.registerZonedDateTime(): GsonBuilder = Converters.registerZonedDateTime(this) + +/** + * Registers the [Instant] converter onto the receiver. + * + * @see Converters.registerInstant + */ +fun GsonBuilder.registerInstant(): GsonBuilder = Converters.registerInstant(this) \ No newline at end of file diff --git a/src/test/java/com/fatboyindustrial/gsonjavatime/KotlinConvertersTest.kt b/src/test/java/com/fatboyindustrial/gsonjavatime/KotlinConvertersTest.kt new file mode 100644 index 0000000..fb9a253 --- /dev/null +++ b/src/test/java/com/fatboyindustrial/gsonjavatime/KotlinConvertersTest.kt @@ -0,0 +1,90 @@ +package com.fatboyindustrial.gsonjavatime + +import com.google.gson.GsonBuilder +import com.google.gson.JsonObject +import com.google.gson.JsonPrimitive +import org.hamcrest.Matchers.`is` +import org.junit.Assert.assertThat +import org.junit.Test +import java.time.* + +class KotlinConvertersTest { + + /** + * Tests that serializing to JSON works. + */ + @Test + fun testSerialization() { + val gson = GsonBuilder().registerAllJavaConverters().create() + + val container = Container() + container.ld = LocalDate.of(1969, 7, 21) + container.lt = LocalTime.of(12, 56, 0) + container.ldt = LocalDateTime.of(container.ld, container.lt) + container.odt = OffsetDateTime.of(container.ld, container.lt, ZoneOffset.ofHours(10)) + container.ot = OffsetTime.of(container.lt, ZoneOffset.ofHours(10)) + container.zdt = ZonedDateTime.of(container.ld, container.lt, ZoneId.of("Australia/Brisbane")) + container.i = container.odt?.toInstant() + + val jsonString = gson.toJson(container) + val json = gson.fromJson(jsonString, JsonObject::class.java).asJsonObject + + assertThat(json.get("ld").asString, `is`("1969-07-21")) + assertThat(json.get("lt").asString, `is`("12:56:00")) + assertThat(json.get("ldt").asString, `is`("1969-07-21T12:56:00")) + assertThat(json.get("odt").asString, `is`("1969-07-21T12:56:00+10:00")) + assertThat(json.get("ot").asString, `is`("12:56:00+10:00")) + assertThat(json.get("zdt").asString, `is`("1969-07-21T12:56:00+10:00[Australia/Brisbane]")) + assertThat(json.get("i").asString, `is`("1969-07-21T02:56:00Z")) + } + + /** + * Tests that deserializing from JSON works. + */ + @Test + fun testDeserialization() { + val gson = Converters.registerAll(GsonBuilder()).create() + + val container = Container() + container.ld = LocalDate.of(1969, 7, 21) + container.lt = LocalTime.of(12, 56, 0) + container.ldt = LocalDateTime.of(container.ld, container.lt) + container.odt = OffsetDateTime.of(container.ld, container.lt, ZoneOffset.ofHours(10)) + container.ot = OffsetTime.of(container.lt, ZoneOffset.ofHours(10)) + container.zdt = ZonedDateTime.of(container.ld, container.lt, ZoneId.of("Australia/Brisbane")) + container.i = container.odt?.toInstant() + + val serialized = JsonObject() + serialized.add("ld", JsonPrimitive("1969-07-21")) + serialized.add("lt", JsonPrimitive("12:56:00")) + serialized.add("ldt", JsonPrimitive("1969-07-21T12:56:00")) + serialized.add("odt", JsonPrimitive("1969-07-21T12:56:00+10:00")) + serialized.add("ot", JsonPrimitive("12:56:00+10:00")) + serialized.add("zdt", JsonPrimitive("1969-07-21T12:56:00+10:00[Australia/Brisbane]")) + serialized.add("i", JsonPrimitive("1969-07-21T02:56:00Z")) + + val jsonString = gson.toJson(serialized) + val deserialized = gson.fromJson(jsonString, Container::class.java) + + assertThat(deserialized.ld, `is`(container.ld)) + assertThat(deserialized.ldt, `is`(container.ldt)) + assertThat(deserialized.lt, `is`(container.lt)) + assertThat(deserialized.odt, `is`(container.odt)) + assertThat(deserialized.ot, `is`(container.ot)) + assertThat(deserialized.zdt, `is`(container.zdt)) + assertThat(deserialized.i, `is`(container.i)) + } + + /** + * Container for serialising many fields. + */ + private class Container { + var ld: LocalDate? = null + var ldt: LocalDateTime? = null + var lt: LocalTime? = null + var odt: OffsetDateTime? = null + var ot: OffsetTime? = null + var zdt: ZonedDateTime? = null + var i: Instant? = null + } +} \ No newline at end of file