summaryrefslogtreecommitdiff
path: root/formats/json-tests/commonTest/src/kotlinx/serialization/features/LongAsStringTest.kt
diff options
context:
space:
mode:
Diffstat (limited to 'formats/json-tests/commonTest/src/kotlinx/serialization/features/LongAsStringTest.kt')
-rw-r--r--formats/json-tests/commonTest/src/kotlinx/serialization/features/LongAsStringTest.kt32
1 files changed, 32 insertions, 0 deletions
diff --git a/formats/json-tests/commonTest/src/kotlinx/serialization/features/LongAsStringTest.kt b/formats/json-tests/commonTest/src/kotlinx/serialization/features/LongAsStringTest.kt
new file mode 100644
index 00000000..c459b6a3
--- /dev/null
+++ b/formats/json-tests/commonTest/src/kotlinx/serialization/features/LongAsStringTest.kt
@@ -0,0 +1,32 @@
+/*
+ * Copyright 2017-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
+ */
+
+package kotlinx.serialization.features
+
+import kotlinx.serialization.*
+import kotlinx.serialization.builtins.*
+import kotlinx.serialization.json.*
+import kotlin.test.*
+
+class LongAsStringTest : JsonTestBase() {
+ @Serializable
+ data class HasLong(@Serializable(LongAsStringSerializer::class) val l: Long)
+
+ @Test
+ fun canSerializeAsStringAndParseBack() = parametrizedTest { jsonTestingMode ->
+ val original = HasLong(Long.MAX_VALUE - 1)
+ val str = default.encodeToString(HasLong.serializer(), original, jsonTestingMode)
+ assertEquals("""{"l":"9223372036854775806"}""", str)
+ val restored = default.decodeFromString(HasLong.serializer(), str, jsonTestingMode)
+ assertEquals(original, restored)
+ }
+
+ @Test
+ fun canNotDeserializeInvalidString() = parametrizedTest { jsonTestingMode ->
+ val str = """{"l": "this is definitely not a long"}"""
+ assertFailsWith<NumberFormatException> { default.decodeFromString(HasLong.serializer(), str, jsonTestingMode) }
+ val str2 = """{"l": "1000000000000000000000"}""" // toooo long for Long
+ assertFailsWith<NumberFormatException> { default.decodeFromString(HasLong.serializer(), str2, jsonTestingMode) }
+ }
+}