summaryrefslogtreecommitdiff
path: root/formats/json-tests
diff options
context:
space:
mode:
authorSergey Shanshin <sergey.shanshin@jetbrains.com>2022-08-15 18:52:44 +0300
committerGitHub <noreply@github.com>2022-08-15 18:52:44 +0300
commit0c0648e8894a95ff57a2cb1c9e52f8e6831c4361 (patch)
tree06650548ea474140e85842bd7d88cae30c30902f /formats/json-tests
parent83b6e332df01c035f2cee55fc232ab6f55fea5ed (diff)
downloadkotlinx.serialization-0c0648e8894a95ff57a2cb1c9e52f8e6831c4361.tar.gz
Fixed decoding of huge JSON data for okio streams
Fixes #2006 Co-authored-by: Leonid Startsev <sandwwraith@users.noreply.github.com>
Diffstat (limited to 'formats/json-tests')
-rw-r--r--formats/json-tests/commonTest/src/kotlinx/serialization/json/JsonHugeDataSerializationTest.kt40
1 files changed, 40 insertions, 0 deletions
diff --git a/formats/json-tests/commonTest/src/kotlinx/serialization/json/JsonHugeDataSerializationTest.kt b/formats/json-tests/commonTest/src/kotlinx/serialization/json/JsonHugeDataSerializationTest.kt
new file mode 100644
index 00000000..0a633268
--- /dev/null
+++ b/formats/json-tests/commonTest/src/kotlinx/serialization/json/JsonHugeDataSerializationTest.kt
@@ -0,0 +1,40 @@
+/*
+ * Copyright 2017-2022 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
+ */
+
+package kotlinx.serialization.json
+
+import kotlinx.serialization.Serializable
+import kotlin.test.Test
+
+class JsonHugeDataSerializationTest : JsonTestBase() {
+
+ @Serializable
+ private data class Node(
+ val children: List<Node>
+ )
+
+ private fun createNodes(count: Int, depth: Int): List<Node> {
+ val ret = mutableListOf<Node>()
+ if (depth == 0) return ret
+ for (i in 0 until count) {
+ ret.add(Node(createNodes(1, depth - 1)))
+ }
+ return ret
+ }
+
+ @Test
+ fun test() {
+ // create some huge instance
+ val rootNode = Node(createNodes(1000, 10))
+
+ val expectedJson = Json.encodeToString(Node.serializer(), rootNode)
+
+ /*
+ The assertJsonFormAndRestored function, when checking the encoding, will call Json.encodeToString(...) for `JsonTestingMode.STREAMING`
+ since the string `expectedJson` was generated by the same function, the test will always consider
+ the encoding to the `STREAMING` mode is correct, even if there was actually an error there. So only TREE, JAVA_STREAMS and OKIO are actually being tested here
+ */
+ assertJsonFormAndRestored(Node.serializer(), rootNode, expectedJson)
+ }
+}