-
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.
Merge pull request #36 from spaceapi-community/timestamps
Serialize and deserialize timestamp fields as `Instant`
- Loading branch information
Showing
5 changed files
with
59 additions
and
19 deletions.
There are no files selected for viewing
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
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
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 |
---|---|---|
@@ -1,14 +1,17 @@ | ||
package io.spaceapi | ||
|
||
import io.spaceapi.types.Contact | ||
import io.spaceapi.types.MemberCount | ||
import io.spaceapi.types.SpaceFed | ||
import io.spaceapi.types.State | ||
import io.spaceapi.types.serializers.TimestampSerializer | ||
import kotlinx.serialization.decodeFromString | ||
import kotlinx.serialization.json.Json | ||
import kotlinx.serialization.json.JsonElement | ||
import kotlinx.serialization.json.JsonObject | ||
import org.junit.Assert | ||
import java.net.URL | ||
import java.time.Instant | ||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
|
||
|
@@ -52,7 +55,6 @@ class ParserTestKotlin { | |
assertEquals(false, parsed.state!!.open) | ||
assertEquals(null, parsed.state!!.lastchange) | ||
assertEquals("Open Mondays from 20:00", parsed.state!!.message) | ||
assertEquals("Open Mondays from 20:00", parsed.state!!.message) | ||
|
||
assertEquals("[email protected]", parsed.contact.email) | ||
assertEquals("irc://freenode.net/#coredump", parsed.contact.irc) | ||
|
@@ -103,7 +105,6 @@ class ParserTestKotlin { | |
assertEquals(false, parsed.state!!.open) | ||
assertEquals(null, parsed.state!!.lastchange) | ||
assertEquals("Open Mondays from 20:00", parsed.state!!.message) | ||
assertEquals("Open Mondays from 20:00", parsed.state!!.message) | ||
|
||
assertEquals("[email protected]", parsed.contact.email) | ||
assertEquals("irc://freenode.net/#coredump", parsed.contact.irc) | ||
|
@@ -156,14 +157,10 @@ class ParserTestKotlin { | |
*/ | ||
@Test | ||
fun parseFloatAsInteger() { | ||
val parsed: State = Json.decodeFromString("""{ | ||
"open": false, | ||
"message": "Open Mondays from 20:00", | ||
"lastchange": 1605400210.0 | ||
val parsed: MemberCount = Json.decodeFromString("""{ | ||
"value": 42.0 | ||
}""") | ||
assertEquals(false, parsed.open) | ||
assertEquals("Open Mondays from 20:00", parsed.message) | ||
assertEquals(1605400210L, parsed.lastchange) | ||
assertEquals(42L, parsed.value) | ||
} | ||
|
||
/** | ||
|
@@ -275,4 +272,30 @@ class ParserTestKotlin { | |
@Suppress("DEPRECATION") | ||
assertEquals(false, parsed.spacephone) | ||
} | ||
|
||
@Test | ||
fun parseTimestampsAsInstant() { | ||
val parsed: State = Json.decodeFromString("""{ | ||
"open": true, | ||
"lastchange": 1693685542 | ||
}""") | ||
assertEquals(true, parsed.open) | ||
assertEquals(Instant.ofEpochSecond(1693685542), parsed.lastchange) | ||
} | ||
|
||
@Test | ||
fun parseFloatTimestampsAsInstant() { | ||
val parsed: State = Json.decodeFromString("""{ | ||
"open": true, | ||
"lastchange": 1693685542.1234321 | ||
}""") | ||
assertEquals(true, parsed.open) | ||
assertEquals(Instant.ofEpochMilli(1693685542000), parsed.lastchange) | ||
} | ||
|
||
@Test | ||
fun serializeInstantsAsLong() { | ||
val serialized = Json.encodeToString(TimestampSerializer, Instant.ofEpochSecond(1693685542)) | ||
assertEquals("1693685542", serialized) | ||
} | ||
} |