Skip to content

Commit

Permalink
Replace ULongNumber with JsonPrimitive(ULong)
Browse files Browse the repository at this point in the history
An overload of JsonPrimitive taking ULong was added in
kotlinx.serialization 1.5.1:
https://github.com/Kotlin/kotlinx.serialization/releases/tag/v1.5.1
  • Loading branch information
lukellmann committed Sep 3, 2023
1 parent 8895352 commit ccdd266
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 24 deletions.
15 changes: 6 additions & 9 deletions gateway/src/commonTest/kotlin/json/SnowflakeTest.kt
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
package dev.kord.gateway.json

import dev.kord.common.entity.Snowflake
import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.Serializable
import kotlinx.serialization.decodeFromString
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.buildJsonObject
import kotlinx.serialization.json.decodeFromJsonElement
import kotlinx.serialization.json.put
import kotlinx.serialization.json.*
import kotlin.js.JsName
import kotlin.test.Test
import kotlin.test.assertEquals
Expand Down Expand Up @@ -56,14 +53,14 @@ class SnowflakeTest {
@Test
@JsName("test4")
fun `Deserialization of Snowflake as large Number completes successfully`() {
// ULong is inline class and therefore cannot extend abstract class Number but put only takes Number
val value = Snowflake.validValues.last.toNumber()
val value = Snowflake.validValues.last
val json = buildJsonObject {
put("snowflake", value)
@OptIn(ExperimentalSerializationApi::class)
put("snowflake", JsonPrimitive(value))
}

val container = Json.decodeFromJsonElement<SnowflakeContainer>(json)
assertEquals(value.value, container.snowflake.value)
assertEquals(value, container.snowflake.value)
}

@Test
Expand Down
15 changes: 0 additions & 15 deletions gateway/src/commonTest/kotlin/json/Util.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,3 @@ suspend fun readFile(prefix: String, name: String) = file("gateway", "json/$pref
infix fun <T> T.shouldBe(that: T) {
assertEquals(that, this)
}

fun ULong.toNumber() = ULongNumber(this)

class ULongNumber(val value: ULong) : Number(), Comparable<ULongNumber> {
override fun toByte() = value.toByte()
override fun toShort() = value.toShort()
override fun toInt() = value.toInt()
override fun toLong() = value.toLong()
override fun toFloat() = value.toFloat()
override fun toDouble() = value.toDouble()
override fun compareTo(other: ULongNumber) = this.value.compareTo(other.value)
override fun equals(other: Any?) = other is ULongNumber && this.value == other.value
override fun hashCode() = value.hashCode()
override fun toString() = value.toString()
}

0 comments on commit ccdd266

Please sign in to comment.