-
Notifications
You must be signed in to change notification settings - Fork 82
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support other SerialFormats in LongOrStringSerializer (#937)
Non-Json formats are supported by falling back to a String.
- Loading branch information
1 parent
14b98f4
commit 7fbcc24
Showing
4 changed files
with
67 additions
and
10 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
38 changes: 38 additions & 0 deletions
38
common/src/jvmTest/kotlin/serialization/LongOrStringSerializerBsonTest.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,38 @@ | ||
package dev.kord.common.serialization | ||
|
||
import com.github.jershell.kbson.KBson | ||
import dev.kord.common.entity.Permission.* | ||
import dev.kord.common.entity.Permissions | ||
import kotlinx.serialization.Serializable | ||
import kotlin.random.Random | ||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
|
||
class LongOrStringSerializerBsonTest { | ||
|
||
@Serializable | ||
private data class SomeObject( | ||
@Serializable(with = LongOrStringSerializer::class) val someLong: String, | ||
@Serializable(with = LongOrStringSerializer::class) val someString: String, | ||
val somePermissions: Permissions, | ||
) | ||
|
||
private val someObject = SomeObject( | ||
someLong = Random.nextLong().toString(), | ||
someString = "some totally random string", | ||
somePermissions = Permissions(DeafenMembers, ManageThreads, SendTTSMessages, ModerateMembers), | ||
) | ||
|
||
@Test | ||
fun `test Bson serialization and deserialization with LongOrStringSerializer`() { | ||
val kBson = KBson.default | ||
assertEquals( | ||
expected = someObject, | ||
actual = kBson.load(SomeObject.serializer(), kBson.stringify(SomeObject.serializer(), someObject)), | ||
) | ||
assertEquals( | ||
expected = someObject, | ||
actual = kBson.load(SomeObject.serializer(), kBson.dump(SomeObject.serializer(), someObject)), | ||
) | ||
} | ||
} |
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