-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding serializers for java time package and finishing tests.
- Loading branch information
Showing
9 changed files
with
149 additions
and
7 deletions.
There are no files selected for viewing
21 changes: 21 additions & 0 deletions
21
src/main/kotlin/com/urosjarc/dbmessiah/extra/kotlinx/InstantSerializer.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package com.urosjarc.dbmessiah.extra.kotlinx | ||
|
||
import kotlinx.serialization.KSerializer | ||
import kotlinx.serialization.descriptors.PrimitiveKind | ||
import kotlinx.serialization.descriptors.PrimitiveSerialDescriptor | ||
import kotlinx.serialization.descriptors.SerialDescriptor | ||
import kotlinx.serialization.encoding.Decoder | ||
import kotlinx.serialization.encoding.Encoder | ||
import java.time.Instant | ||
|
||
public object InstantSerializer : KSerializer<Instant> { | ||
override val descriptor: SerialDescriptor = PrimitiveSerialDescriptor("java.time.Instant", PrimitiveKind.STRING) | ||
|
||
override fun deserialize(decoder: Decoder): Instant { | ||
return Instant.parse(decoder.decodeString()) | ||
} | ||
|
||
override fun serialize(encoder: Encoder, value: Instant) { | ||
encoder.encodeString(value.toString()) | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
src/main/kotlin/com/urosjarc/dbmessiah/extra/kotlinx/LocalDateSerializer.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package com.urosjarc.dbmessiah.extra.kotlinx | ||
|
||
import kotlinx.serialization.KSerializer | ||
import kotlinx.serialization.descriptors.PrimitiveKind | ||
import kotlinx.serialization.descriptors.PrimitiveSerialDescriptor | ||
import kotlinx.serialization.descriptors.SerialDescriptor | ||
import kotlinx.serialization.encoding.Decoder | ||
import kotlinx.serialization.encoding.Encoder | ||
import java.time.LocalDate | ||
|
||
public object LocalDateSerializer : KSerializer<LocalDate> { | ||
override val descriptor: SerialDescriptor = PrimitiveSerialDescriptor("java.time.LocalDate", PrimitiveKind.STRING) | ||
|
||
override fun deserialize(decoder: Decoder): LocalDate { | ||
return LocalDate.parse(decoder.decodeString()) | ||
} | ||
|
||
override fun serialize(encoder: Encoder, value: LocalDate) { | ||
encoder.encodeString(value.toString()) | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
src/main/kotlin/com/urosjarc/dbmessiah/extra/kotlinx/LocalTimeSerializer.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package com.urosjarc.dbmessiah.extra.kotlinx | ||
|
||
import kotlinx.serialization.KSerializer | ||
import kotlinx.serialization.descriptors.PrimitiveKind | ||
import kotlinx.serialization.descriptors.PrimitiveSerialDescriptor | ||
import kotlinx.serialization.descriptors.SerialDescriptor | ||
import kotlinx.serialization.encoding.Decoder | ||
import kotlinx.serialization.encoding.Encoder | ||
import java.time.LocalTime | ||
|
||
public object LocalTimeSerializer : KSerializer<LocalTime> { | ||
override val descriptor: SerialDescriptor = PrimitiveSerialDescriptor("java.time.LocalTime", PrimitiveKind.STRING) | ||
|
||
override fun deserialize(decoder: Decoder): LocalTime { | ||
return LocalTime.parse(decoder.decodeString()) | ||
} | ||
|
||
override fun serialize(encoder: Encoder, value: LocalTime) { | ||
encoder.encodeString(value.toString()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import kotlinx.serialization.decodeFromString | ||
import kotlinx.serialization.encodeToString | ||
import org.junit.jupiter.api.Test | ||
import utils.json | ||
import java.time.Instant | ||
import kotlin.test.assertEquals | ||
|
||
class Test_InstantSerializer { | ||
@Test | ||
fun `test serialization`() { | ||
val instant = Instant.now() | ||
|
||
/** Encoding */ | ||
val instantJson = json.encodeToString(instant) | ||
assertEquals("\"$instant\"", instantJson) | ||
|
||
/** Decoding */ | ||
val instant_new = json.decodeFromString<Instant>(instantJson) | ||
assertEquals(instant, instant_new) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import kotlinx.serialization.decodeFromString | ||
import kotlinx.serialization.encodeToString | ||
import org.junit.jupiter.api.Test | ||
import utils.json | ||
import java.time.LocalDate | ||
import kotlin.test.assertEquals | ||
|
||
class Test_LocalDateSerializer { | ||
@Test | ||
fun `test serialization`() { | ||
val instant = LocalDate.now() | ||
|
||
/** Encoding */ | ||
val instantJson = json.encodeToString(instant) | ||
assertEquals("\"$instant\"", instantJson) | ||
|
||
/** Decoding */ | ||
val instant_new = json.decodeFromString<LocalDate>(instantJson) | ||
assertEquals(instant, instant_new) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import kotlinx.serialization.decodeFromString | ||
import kotlinx.serialization.encodeToString | ||
import org.junit.jupiter.api.Test | ||
import utils.json | ||
import java.time.LocalTime | ||
import kotlin.test.assertEquals | ||
|
||
class Test_LocalTimeSerializer { | ||
@Test | ||
fun `test serialization`() { | ||
val instant = LocalTime.now() | ||
|
||
/** Encoding */ | ||
val instantJson = json.encodeToString(instant) | ||
assertEquals("\"$instant\"", instantJson) | ||
|
||
/** Decoding */ | ||
val instant_new = json.decodeFromString<LocalTime>(instantJson) | ||
assertEquals(instant, instant_new) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters