Skip to content

Add Kotlin extension functions support #25

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .idea/codeStyles/codeStyleConfig.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 29 additions & 1 deletion gson-javatime-serialisers.iml
Original file line number Diff line number Diff line change
@@ -1,6 +1,27 @@
<?xml version="1.0" encoding="UTF-8"?>
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_8" inherit-compiler-output="false">
<component name="FacetManager">
<facet type="kotlin-language" name="Kotlin">
<configuration version="3" platform="JVM 1.8" useProjectSettings="false">
<compilerSettings />
<compilerArguments>
<option name="jvmTarget" value="1.8" />
<option name="languageVersion" value="1.3" />
<option name="apiVersion" value="1.3" />
<option name="pluginOptions">
<array />
</option>
<option name="pluginClasspaths">
<array />
</option>
<option name="errors">
<ArgumentParseErrors />
</option>
</compilerArguments>
</configuration>
</facet>
</component>
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_8">
<output url="file://$MODULE_DIR$/target/classes" />
<output-test url="file://$MODULE_DIR$/target/test-classes" />
<content url="file://$MODULE_DIR$">
Expand All @@ -14,5 +35,12 @@
<orderEntry type="library" scope="TEST" name="Maven: org.hamcrest:hamcrest-core:1.3" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.hamcrest:hamcrest-all:1.3" level="project" />
<orderEntry type="library" name="Maven: com.google.code.gson:gson:2.3.1" level="project" />
<orderEntry type="library" name="Maven: org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.3.21" level="project" />
<orderEntry type="library" name="Maven: org.jetbrains.kotlin:kotlin-stdlib:1.3.21" level="project" />
<orderEntry type="library" name="Maven: org.jetbrains.kotlin:kotlin-stdlib-common:1.3.21" level="project" />
<orderEntry type="library" name="Maven: org.jetbrains:annotations:13.0" level="project" />
<orderEntry type="library" name="Maven: org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.3.21" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.jetbrains.kotlin:kotlin-test:1.3.21" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.jetbrains.kotlin:kotlin-test-common:1.3.21" level="project" />
</component>
</module>
72 changes: 62 additions & 10 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -59,20 +59,12 @@

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<kotlin.version>1.3.21</kotlin.version>
</properties>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-release-plugin</artifactId>
<version>2.2.2</version>
Expand Down Expand Up @@ -128,6 +120,55 @@
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
<version>${kotlin.version}</version>
<executions>
<execution>
<id>compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>test-compile</id>
<phase>test-compile</phase>
<goals>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
<configuration>
<jvmTarget>1.8</jvmTarget>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.0</version>
<executions>
<execution>
<id>compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>testCompile</id>
<phase>test-compile</phase>
<goals>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>

Expand Down Expand Up @@ -195,6 +236,17 @@
<artifactId>gson</artifactId>
<version>2.3.1</version>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib-jdk8</artifactId>
<version>${kotlin.version}</version>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-test</artifactId>
<version>${kotlin.version}</version>
<scope>test</scope>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -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)
Original file line number Diff line number Diff line change
@@ -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<String>(json.get("ld").asString, `is`<String>("1969-07-21"))
assertThat<String>(json.get("lt").asString, `is`<String>("12:56:00"))
assertThat<String>(json.get("ldt").asString, `is`<String>("1969-07-21T12:56:00"))
assertThat<String>(json.get("odt").asString, `is`<String>("1969-07-21T12:56:00+10:00"))
assertThat<String>(json.get("ot").asString, `is`<String>("12:56:00+10:00"))
assertThat<String>(json.get("zdt").asString, `is`<String>("1969-07-21T12:56:00+10:00[Australia/Brisbane]"))
assertThat<String>(json.get("i").asString, `is`<String>("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<Container>(jsonString, Container::class.java)

assertThat<LocalDate>(deserialized.ld, `is`<LocalDate>(container.ld))
assertThat<LocalDateTime>(deserialized.ldt, `is`<LocalDateTime>(container.ldt))
assertThat<LocalTime>(deserialized.lt, `is`<LocalTime>(container.lt))
assertThat<OffsetDateTime>(deserialized.odt, `is`<OffsetDateTime>(container.odt))
assertThat<OffsetTime>(deserialized.ot, `is`<OffsetTime>(container.ot))
assertThat<ZonedDateTime>(deserialized.zdt, `is`<ZonedDateTime>(container.zdt))
assertThat<Instant>(deserialized.i, `is`<Instant>(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
}
}