From de88eda4569f07b8b156508247766f367ecc8174 Mon Sep 17 00:00:00 2001 From: Adam <897017+aSemy@users.noreply.github.com> Date: Sat, 14 Jan 2023 23:35:45 +0100 Subject: [PATCH 01/15] addAll functions for JsonArrayBuilder --- .../serialization/json/JsonBuildersTest.kt | 50 ++++++++++++++++++- .../serialization/json/JsonElementBuilders.kt | 45 +++++++++++++++++ 2 files changed, 93 insertions(+), 2 deletions(-) diff --git a/formats/json-tests/commonTest/src/kotlinx/serialization/json/JsonBuildersTest.kt b/formats/json-tests/commonTest/src/kotlinx/serialization/json/JsonBuildersTest.kt index 9e4e57223..15bc8b113 100644 --- a/formats/json-tests/commonTest/src/kotlinx/serialization/json/JsonBuildersTest.kt +++ b/formats/json-tests/commonTest/src/kotlinx/serialization/json/JsonBuildersTest.kt @@ -4,7 +4,8 @@ package kotlinx.serialization.json -import kotlin.test.* +import kotlin.test.Test +import kotlin.test.assertEquals class JsonBuildersTest { @@ -26,7 +27,10 @@ class JsonBuildersTest { put("literal", "foo") put("null2", null) } - assertEquals("""{"object":{"k":"v"},"array":[{"nestedLiteral":true}],"null":null,"primitive":42,"boolean":true,"literal":"foo","null2":null}""", json.toString()) + assertEquals( + """{"object":{"k":"v"},"array":[{"nestedLiteral":true}],"null":null,"primitive":42,"boolean":true,"literal":"foo","null2":null}""", + json.toString() + ) } @Test @@ -43,4 +47,46 @@ class JsonBuildersTest { } assertEquals("""[true,[1,2,3,4,5,6,7,8,9,10],null,{"stringKey":"stringValue"}]""", json.toString()) } + + @Test + fun testBuildJsonArrayAddAll() { + val jsonNumbers = buildJsonArray { + addAll(1) + addAll(2, 3, 4, 5, null) + addAll(listOf(1, 2, 3, 4, 5, null)) + } + assertEquals("""[1,2,3,4,5,null,1,2,3,4,5,null]""", jsonNumbers.toString()) + + val jsonStrings = buildJsonArray { + addAll("a") + addAll("b", "c", null) + addAll(listOf("a", "b", "c", null)) + } + assertEquals("""["a","b","c",null,"a","b","c",null]""", jsonStrings.toString()) + + val jsonBooleans = buildJsonArray { + addAll(true) + addAll(true, true, null) + addAll(listOf(false, false, false, null)) + } + assertEquals("""[true,true,true,null,false,false,false,null]""", jsonBooleans.toString()) + + val jsonPrimitiveElements = buildJsonArray { + addAll(JsonPrimitive(1), JsonPrimitive("a"), JsonPrimitive(false), JsonNull) + addAll(listOf(JsonPrimitive(2), JsonPrimitive("b"), JsonPrimitive(true), JsonNull)) + } + assertEquals("""[1,"a",false,null,2,"b",true,null]""", jsonPrimitiveElements.toString()) + + val jsonObjectElements = buildJsonArray { + addAll(JsonObject(emptyMap()), JsonObject(emptyMap()), JsonObject(emptyMap()), JsonNull) + addAll(listOf(JsonObject(emptyMap()), JsonObject(emptyMap()), JsonObject(emptyMap()), JsonNull)) + } + assertEquals("""[{},{},{},null,{},{},{},null]""", jsonObjectElements.toString()) + + val jsonArrayElements = buildJsonArray { + addAll(JsonArray(emptyList()), JsonArray(emptyList()), JsonArray(emptyList()), JsonNull) + addAll(listOf(JsonArray(emptyList()), JsonArray(emptyList()), JsonArray(emptyList()), JsonNull)) + } + assertEquals("""[[],[],[],null,[],[],[],null]""", jsonArrayElements.toString()) + } } diff --git a/formats/json/commonMain/src/kotlinx/serialization/json/JsonElementBuilders.kt b/formats/json/commonMain/src/kotlinx/serialization/json/JsonElementBuilders.kt index b49a1aaab..fd640b5d0 100644 --- a/formats/json/commonMain/src/kotlinx/serialization/json/JsonElementBuilders.kt +++ b/formats/json/commonMain/src/kotlinx/serialization/json/JsonElementBuilders.kt @@ -7,6 +7,7 @@ package kotlinx.serialization.json import kotlinx.serialization.ExperimentalSerializationApi import kotlin.contracts.* +import kotlin.jvm.JvmName /** * Builds [JsonObject] with the given [builderAction] builder. @@ -136,6 +137,15 @@ public class JsonArrayBuilder @PublishedApi internal constructor() { return true } + /** + * Adds the given JSON [elements] to a resulting JSON array. + * + * @return `true` if the list was changed as the result of the operation. + */ + public fun addAll(elements: Collection): Boolean { + return content.addAll(elements) + } + @PublishedApi internal fun build(): JsonArray = JsonArray(content) } @@ -186,6 +196,41 @@ public fun JsonArrayBuilder.addJsonObject(builderAction: JsonObjectBuilder.() -> public fun JsonArrayBuilder.addJsonArray(builderAction: JsonArrayBuilder.() -> Unit): Boolean = add(buildJsonArray(builderAction)) +/** + * Adds the given JSON [element] and [elements] to a resulting JSON array. + * + * @return `true` if the list was changed as the result of the operation. + */ +public fun JsonArrayBuilder.addAll(element: JsonElement, vararg elements: JsonElement): Boolean { + return addAll(listOf(element) + elements) +} + +/** Adds the given string [values] to a resulting JSON array. */ +@JvmName("addAllStrings") +public fun JsonArrayBuilder.addAll(values: Collection): Boolean = + addAll(values.map(::JsonPrimitive)) + +/** Adds the given boolean [values] to a resulting JSON array. */ +@JvmName("addAllBooleans") +public fun JsonArrayBuilder.addAll(values: Collection): Boolean = + addAll(values.map(::JsonPrimitive)) + +/** Adds the given numeric [values] to a resulting JSON array. */ +@JvmName("addAllNumbers") +public fun JsonArrayBuilder.addAll(values: Collection): Boolean = + addAll(values.map(::JsonPrimitive)) + +/** Adds the given string [values] to a resulting JSON array. */ +public fun JsonArrayBuilder.addAll(vararg values: String?): Boolean = + addAll(values.toList()) + +/** Adds the given boolean [values] to a resulting JSON array. */ +public fun JsonArrayBuilder.addAll(vararg values: Boolean?): Boolean = + addAll(values.toList()) + +/** Adds the given numeric [values] to a resulting JSON array. */ +public fun JsonArrayBuilder.addAll(vararg values: Number?): Boolean = + addAll(values.toList()) @DslMarker internal annotation class JsonDslMarker From cf8be83f84221cef04583153217bb3efb3203add Mon Sep 17 00:00:00 2001 From: Adam <897017+aSemy@users.noreply.github.com> Date: Sat, 14 Jan 2023 23:37:04 +0100 Subject: [PATCH 02/15] revert formatting changes --- .../src/kotlinx/serialization/json/JsonBuildersTest.kt | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/formats/json-tests/commonTest/src/kotlinx/serialization/json/JsonBuildersTest.kt b/formats/json-tests/commonTest/src/kotlinx/serialization/json/JsonBuildersTest.kt index 15bc8b113..ca289865f 100644 --- a/formats/json-tests/commonTest/src/kotlinx/serialization/json/JsonBuildersTest.kt +++ b/formats/json-tests/commonTest/src/kotlinx/serialization/json/JsonBuildersTest.kt @@ -4,8 +4,7 @@ package kotlinx.serialization.json -import kotlin.test.Test -import kotlin.test.assertEquals +import kotlin.test.* class JsonBuildersTest { @@ -27,10 +26,7 @@ class JsonBuildersTest { put("literal", "foo") put("null2", null) } - assertEquals( - """{"object":{"k":"v"},"array":[{"nestedLiteral":true}],"null":null,"primitive":42,"boolean":true,"literal":"foo","null2":null}""", - json.toString() - ) + assertEquals("""{"object":{"k":"v"},"array":[{"nestedLiteral":true}],"null":null,"primitive":42,"boolean":true,"literal":"foo","null2":null}""", json.toString()) } @Test From fc55c4beca2dc27b2ed8d6d313ffbdd89977232d Mon Sep 17 00:00:00 2001 From: Adam <897017+aSemy@users.noreply.github.com> Date: Sat, 14 Jan 2023 23:47:22 +0100 Subject: [PATCH 03/15] convert addAll to expression body --- .../src/kotlinx/serialization/json/JsonElementBuilders.kt | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/formats/json/commonMain/src/kotlinx/serialization/json/JsonElementBuilders.kt b/formats/json/commonMain/src/kotlinx/serialization/json/JsonElementBuilders.kt index fd640b5d0..c32cb1ff3 100644 --- a/formats/json/commonMain/src/kotlinx/serialization/json/JsonElementBuilders.kt +++ b/formats/json/commonMain/src/kotlinx/serialization/json/JsonElementBuilders.kt @@ -142,9 +142,8 @@ public class JsonArrayBuilder @PublishedApi internal constructor() { * * @return `true` if the list was changed as the result of the operation. */ - public fun addAll(elements: Collection): Boolean { - return content.addAll(elements) - } + public fun addAll(elements: Collection): Boolean = + content.addAll(elements) @PublishedApi internal fun build(): JsonArray = JsonArray(content) From 8c2e411b815eec7e3051d35b631a690655f19f87 Mon Sep 17 00:00:00 2001 From: Adam <897017+aSemy@users.noreply.github.com> Date: Sat, 14 Jan 2023 23:47:48 +0100 Subject: [PATCH 04/15] verify addAll modifies the array & returns true --- .../serialization/json/JsonBuildersTest.kt | 126 +++++++++++++----- 1 file changed, 93 insertions(+), 33 deletions(-) diff --git a/formats/json-tests/commonTest/src/kotlinx/serialization/json/JsonBuildersTest.kt b/formats/json-tests/commonTest/src/kotlinx/serialization/json/JsonBuildersTest.kt index ca289865f..57e0606ee 100644 --- a/formats/json-tests/commonTest/src/kotlinx/serialization/json/JsonBuildersTest.kt +++ b/formats/json-tests/commonTest/src/kotlinx/serialization/json/JsonBuildersTest.kt @@ -46,43 +46,103 @@ class JsonBuildersTest { @Test fun testBuildJsonArrayAddAll() { - val jsonNumbers = buildJsonArray { - addAll(1) - addAll(2, 3, 4, 5, null) - addAll(listOf(1, 2, 3, 4, 5, null)) - } - assertEquals("""[1,2,3,4,5,null,1,2,3,4,5,null]""", jsonNumbers.toString()) + assertEquals( + """[1,2,3,4,5,null,1,2,3,4,5,null]""", + buildJsonArray { + assertTrue { addAll(1) } + assertTrue { addAll(2, 3, 4, 5, null) } + assertTrue { addAll(listOf(1, 2, 3, 4, 5, null)) } + }.toString() + ) - val jsonStrings = buildJsonArray { - addAll("a") - addAll("b", "c", null) - addAll(listOf("a", "b", "c", null)) - } - assertEquals("""["a","b","c",null,"a","b","c",null]""", jsonStrings.toString()) + assertEquals( + """["a","b","c",null,"a","b","c",null]""", + buildJsonArray { + assertTrue { addAll("a") } + assertTrue { addAll("b", "c", null) } + assertTrue { addAll(listOf("a", "b", "c", null)) } + }.toString() + ) - val jsonBooleans = buildJsonArray { - addAll(true) - addAll(true, true, null) - addAll(listOf(false, false, false, null)) - } - assertEquals("""[true,true,true,null,false,false,false,null]""", jsonBooleans.toString()) + assertEquals( + """[true,true,true,null,false,false,false,null]""", + buildJsonArray { + assertTrue { addAll(true) } + assertTrue { addAll(true, true, null) } + assertTrue { addAll(listOf(false, false, false, null)) } + }.toString() + ) - val jsonPrimitiveElements = buildJsonArray { - addAll(JsonPrimitive(1), JsonPrimitive("a"), JsonPrimitive(false), JsonNull) - addAll(listOf(JsonPrimitive(2), JsonPrimitive("b"), JsonPrimitive(true), JsonNull)) - } - assertEquals("""[1,"a",false,null,2,"b",true,null]""", jsonPrimitiveElements.toString()) + assertEquals( + """[1,"a",false,null,2,"b",true,null]""", + buildJsonArray { + assertTrue { + addAll( + JsonPrimitive(1), + JsonPrimitive("a"), + JsonPrimitive(false), + JsonNull, + ) + } + assertTrue { + addAll( + listOf( + JsonPrimitive(2), + JsonPrimitive("b"), + JsonPrimitive(true), + JsonNull + ) + ) + } + }.toString() + ) - val jsonObjectElements = buildJsonArray { - addAll(JsonObject(emptyMap()), JsonObject(emptyMap()), JsonObject(emptyMap()), JsonNull) - addAll(listOf(JsonObject(emptyMap()), JsonObject(emptyMap()), JsonObject(emptyMap()), JsonNull)) - } - assertEquals("""[{},{},{},null,{},{},{},null]""", jsonObjectElements.toString()) + assertEquals( + """[{},{},{},null,{},{},{},null]""", + buildJsonArray { + assertTrue { + addAll( + JsonObject(emptyMap()), + JsonObject(emptyMap()), + JsonObject(emptyMap()), + JsonNull, + ) + } + assertTrue { + addAll( + listOf( + JsonObject(emptyMap()), + JsonObject(emptyMap()), + JsonObject(emptyMap()), + JsonNull + ) + ) + } + }.toString() + ) - val jsonArrayElements = buildJsonArray { - addAll(JsonArray(emptyList()), JsonArray(emptyList()), JsonArray(emptyList()), JsonNull) - addAll(listOf(JsonArray(emptyList()), JsonArray(emptyList()), JsonArray(emptyList()), JsonNull)) - } - assertEquals("""[[],[],[],null,[],[],[],null]""", jsonArrayElements.toString()) + assertEquals( + """[[],[],[],null,[],[],[],null]""", + buildJsonArray { + assertTrue { + addAll( + JsonArray(emptyList()), + JsonArray(emptyList()), + JsonArray(emptyList()), + JsonNull, + ) + } + assertTrue { + addAll( + listOf( + JsonArray(emptyList()), + JsonArray(emptyList()), + JsonArray(emptyList()), + JsonNull + ) + ) + } + }.toString() + ) } } From ef6a5ddd1f04cba257a44735a23d6e83a63b2449 Mon Sep 17 00:00:00 2001 From: Adam <897017+aSemy@users.noreply.github.com> Date: Sat, 14 Jan 2023 23:56:59 +0100 Subject: [PATCH 05/15] test for adding null --- .../kotlinx/serialization/json/JsonBuildersTest.kt | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/formats/json-tests/commonTest/src/kotlinx/serialization/json/JsonBuildersTest.kt b/formats/json-tests/commonTest/src/kotlinx/serialization/json/JsonBuildersTest.kt index 57e0606ee..4a8e0e64a 100644 --- a/formats/json-tests/commonTest/src/kotlinx/serialization/json/JsonBuildersTest.kt +++ b/formats/json-tests/commonTest/src/kotlinx/serialization/json/JsonBuildersTest.kt @@ -144,5 +144,17 @@ class JsonBuildersTest { } }.toString() ) + + assertEquals( + """[null,null,null,null]""", + buildJsonArray { + assertTrue { + addAll(JsonNull, JsonNull) + } + assertTrue { + addAll(listOf(JsonNull, JsonNull)) + } + }.toString() + ) } } From 4f345b62a644b31f1f2f0ee37d44c4738b4a0bf4 Mon Sep 17 00:00:00 2001 From: Adam <897017+aSemy@users.noreply.github.com> Date: Sun, 15 Jan 2023 00:04:52 +0100 Subject: [PATCH 06/15] add 'not modified' tests --- .../kotlinx/serialization/json/JsonBuildersTest.kt | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/formats/json-tests/commonTest/src/kotlinx/serialization/json/JsonBuildersTest.kt b/formats/json-tests/commonTest/src/kotlinx/serialization/json/JsonBuildersTest.kt index 4a8e0e64a..1f6b81da4 100644 --- a/formats/json-tests/commonTest/src/kotlinx/serialization/json/JsonBuildersTest.kt +++ b/formats/json-tests/commonTest/src/kotlinx/serialization/json/JsonBuildersTest.kt @@ -157,4 +157,18 @@ class JsonBuildersTest { }.toString() ) } + + @Test + fun testBuildJsonArrayAddAllNotModified() { + assertEquals( + """[]""", + buildJsonArray { + assertFalse { addAll(listOf()) } + assertFalse { addAll(listOf()) } + assertFalse { addAll(listOf()) } + assertFalse { addAll(listOf()) } + assertFalse { addAll(listOf()) } + }.toString() + ) + } } From ae6d93bf7f83b6b117b6944e66c71f3b9c03ac9f Mon Sep 17 00:00:00 2001 From: Adam <897017+aSemy@users.noreply.github.com> Date: Sun, 15 Jan 2023 00:07:39 +0100 Subject: [PATCH 07/15] more 'not modified' tests --- .../serialization/json/JsonBuildersTest.kt | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/formats/json-tests/commonTest/src/kotlinx/serialization/json/JsonBuildersTest.kt b/formats/json-tests/commonTest/src/kotlinx/serialization/json/JsonBuildersTest.kt index 1f6b81da4..9416585c3 100644 --- a/formats/json-tests/commonTest/src/kotlinx/serialization/json/JsonBuildersTest.kt +++ b/formats/json-tests/commonTest/src/kotlinx/serialization/json/JsonBuildersTest.kt @@ -163,11 +163,22 @@ class JsonBuildersTest { assertEquals( """[]""", buildJsonArray { - assertFalse { addAll(listOf()) } - assertFalse { addAll(listOf()) } + // varargs + assertFalse { addAll(*arrayOf()) } + assertFalse { addAll(*arrayOf()) } + assertFalse { addAll(*arrayOf()) } + + // add collections + assertFalse { addAll(listOf()) } assertFalse { addAll(listOf()) } assertFalse { addAll(listOf()) } + + // add json elements + assertFalse { addAll(listOf()) } assertFalse { addAll(listOf()) } + assertFalse { addAll(listOf()) } + assertFalse { addAll(listOf()) } + assertFalse { addAll(listOf()) } }.toString() ) } From b046eaf987a711e99c6799f6558557e2ea0df928 Mon Sep 17 00:00:00 2001 From: Adam <897017+aSemy@users.noreply.github.com> Date: Mon, 16 Jan 2023 12:29:46 +0100 Subject: [PATCH 08/15] update api file --- formats/json/api/kotlinx-serialization-json.api | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/formats/json/api/kotlinx-serialization-json.api b/formats/json/api/kotlinx-serialization-json.api index 898a94a0f..8cefdb858 100644 --- a/formats/json/api/kotlinx-serialization-json.api +++ b/formats/json/api/kotlinx-serialization-json.api @@ -70,6 +70,7 @@ public final class kotlinx/serialization/json/JsonArray$Companion { public final class kotlinx/serialization/json/JsonArrayBuilder { public fun ()V public final fun add (Lkotlinx/serialization/json/JsonElement;)Z + public final fun addAll (Ljava/util/Collection;)Z public final fun build ()Lkotlinx/serialization/json/JsonArray; } @@ -173,6 +174,13 @@ public final class kotlinx/serialization/json/JsonElementBuildersKt { public static final fun add (Lkotlinx/serialization/json/JsonArrayBuilder;Ljava/lang/Number;)Z public static final fun add (Lkotlinx/serialization/json/JsonArrayBuilder;Ljava/lang/String;)Z public static final fun add (Lkotlinx/serialization/json/JsonArrayBuilder;Ljava/lang/Void;)Z + public static final fun addAll (Lkotlinx/serialization/json/JsonArrayBuilder;Lkotlinx/serialization/json/JsonElement;[Lkotlinx/serialization/json/JsonElement;)Z + public static final fun addAll (Lkotlinx/serialization/json/JsonArrayBuilder;[Ljava/lang/Boolean;)Z + public static final fun addAll (Lkotlinx/serialization/json/JsonArrayBuilder;[Ljava/lang/Number;)Z + public static final fun addAll (Lkotlinx/serialization/json/JsonArrayBuilder;[Ljava/lang/String;)Z + public static final fun addAllBooleans (Lkotlinx/serialization/json/JsonArrayBuilder;Ljava/util/Collection;)Z + public static final fun addAllNumbers (Lkotlinx/serialization/json/JsonArrayBuilder;Ljava/util/Collection;)Z + public static final fun addAllStrings (Lkotlinx/serialization/json/JsonArrayBuilder;Ljava/util/Collection;)Z public static final fun addJsonArray (Lkotlinx/serialization/json/JsonArrayBuilder;Lkotlin/jvm/functions/Function1;)Z public static final fun addJsonObject (Lkotlinx/serialization/json/JsonArrayBuilder;Lkotlin/jvm/functions/Function1;)Z public static final fun buildJsonArray (Lkotlin/jvm/functions/Function1;)Lkotlinx/serialization/json/JsonArray; From f38d11dc8472961fbf30cbbad07ea526a1264a18 Mon Sep 17 00:00:00 2001 From: Adam <897017+aSemy@users.noreply.github.com> Date: Fri, 21 Apr 2023 17:10:40 +0200 Subject: [PATCH 09/15] add `@ExperimentalSerializationApi` to new funcions, tidy `addAll(vararg elements: JsonElement)` --- .../serialization/json/JsonElementBuilders.kt | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/formats/json/commonMain/src/kotlinx/serialization/json/JsonElementBuilders.kt b/formats/json/commonMain/src/kotlinx/serialization/json/JsonElementBuilders.kt index c32cb1ff3..30a116e2a 100644 --- a/formats/json/commonMain/src/kotlinx/serialization/json/JsonElementBuilders.kt +++ b/formats/json/commonMain/src/kotlinx/serialization/json/JsonElementBuilders.kt @@ -6,7 +6,9 @@ package kotlinx.serialization.json import kotlinx.serialization.ExperimentalSerializationApi -import kotlin.contracts.* +import kotlin.contracts.ExperimentalContracts +import kotlin.contracts.InvocationKind +import kotlin.contracts.contract import kotlin.jvm.JvmName /** @@ -142,6 +144,7 @@ public class JsonArrayBuilder @PublishedApi internal constructor() { * * @return `true` if the list was changed as the result of the operation. */ + @ExperimentalSerializationApi public fun addAll(elements: Collection): Boolean = content.addAll(elements) @@ -196,38 +199,45 @@ public fun JsonArrayBuilder.addJsonArray(builderAction: JsonArrayBuilder.() -> U add(buildJsonArray(builderAction)) /** - * Adds the given JSON [element] and [elements] to a resulting JSON array. + * Adds the given JSON [elements] to a resulting JSON array. * * @return `true` if the list was changed as the result of the operation. */ -public fun JsonArrayBuilder.addAll(element: JsonElement, vararg elements: JsonElement): Boolean { - return addAll(listOf(element) + elements) +@ExperimentalSerializationApi +public fun JsonArrayBuilder.addAll(vararg elements: JsonElement): Boolean { + return addAll(elements.toList()) } /** Adds the given string [values] to a resulting JSON array. */ @JvmName("addAllStrings") +@ExperimentalSerializationApi public fun JsonArrayBuilder.addAll(values: Collection): Boolean = addAll(values.map(::JsonPrimitive)) /** Adds the given boolean [values] to a resulting JSON array. */ @JvmName("addAllBooleans") +@ExperimentalSerializationApi public fun JsonArrayBuilder.addAll(values: Collection): Boolean = addAll(values.map(::JsonPrimitive)) /** Adds the given numeric [values] to a resulting JSON array. */ @JvmName("addAllNumbers") +@ExperimentalSerializationApi public fun JsonArrayBuilder.addAll(values: Collection): Boolean = addAll(values.map(::JsonPrimitive)) /** Adds the given string [values] to a resulting JSON array. */ +@ExperimentalSerializationApi public fun JsonArrayBuilder.addAll(vararg values: String?): Boolean = addAll(values.toList()) /** Adds the given boolean [values] to a resulting JSON array. */ +@ExperimentalSerializationApi public fun JsonArrayBuilder.addAll(vararg values: Boolean?): Boolean = addAll(values.toList()) /** Adds the given numeric [values] to a resulting JSON array. */ +@ExperimentalSerializationApi public fun JsonArrayBuilder.addAll(vararg values: Number?): Boolean = addAll(values.toList()) From 0f7b113c78758148ad2e8b074689cdc1118e36ef Mon Sep 17 00:00:00 2001 From: Adam <897017+aSemy@users.noreply.github.com> Date: Fri, 21 Apr 2023 17:11:27 +0200 Subject: [PATCH 10/15] update API dump --- formats/json/api/kotlinx-serialization-json.api | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/formats/json/api/kotlinx-serialization-json.api b/formats/json/api/kotlinx-serialization-json.api index 8cefdb858..0bd53d610 100644 --- a/formats/json/api/kotlinx-serialization-json.api +++ b/formats/json/api/kotlinx-serialization-json.api @@ -174,10 +174,10 @@ public final class kotlinx/serialization/json/JsonElementBuildersKt { public static final fun add (Lkotlinx/serialization/json/JsonArrayBuilder;Ljava/lang/Number;)Z public static final fun add (Lkotlinx/serialization/json/JsonArrayBuilder;Ljava/lang/String;)Z public static final fun add (Lkotlinx/serialization/json/JsonArrayBuilder;Ljava/lang/Void;)Z - public static final fun addAll (Lkotlinx/serialization/json/JsonArrayBuilder;Lkotlinx/serialization/json/JsonElement;[Lkotlinx/serialization/json/JsonElement;)Z public static final fun addAll (Lkotlinx/serialization/json/JsonArrayBuilder;[Ljava/lang/Boolean;)Z public static final fun addAll (Lkotlinx/serialization/json/JsonArrayBuilder;[Ljava/lang/Number;)Z public static final fun addAll (Lkotlinx/serialization/json/JsonArrayBuilder;[Ljava/lang/String;)Z + public static final fun addAll (Lkotlinx/serialization/json/JsonArrayBuilder;[Lkotlinx/serialization/json/JsonElement;)Z public static final fun addAllBooleans (Lkotlinx/serialization/json/JsonArrayBuilder;Ljava/util/Collection;)Z public static final fun addAllNumbers (Lkotlinx/serialization/json/JsonArrayBuilder;Ljava/util/Collection;)Z public static final fun addAllStrings (Lkotlinx/serialization/json/JsonArrayBuilder;Ljava/util/Collection;)Z From ec87d84f69ba8a13f4ba7710915381947176cdaf Mon Sep 17 00:00:00 2001 From: Adam <897017+aSemy@users.noreply.github.com> Date: Mon, 1 May 2023 18:25:41 +0200 Subject: [PATCH 11/15] remove JsonArrayBuilder addAll varargs util --- .../serialization/json/JsonBuildersTest.kt | 21 +++++-------------- .../json/api/kotlinx-serialization-json.api | 3 --- .../serialization/json/JsonElementBuilders.kt | 15 ------------- 3 files changed, 5 insertions(+), 34 deletions(-) diff --git a/formats/json-tests/commonTest/src/kotlinx/serialization/json/JsonBuildersTest.kt b/formats/json-tests/commonTest/src/kotlinx/serialization/json/JsonBuildersTest.kt index 9416585c3..0778b11ec 100644 --- a/formats/json-tests/commonTest/src/kotlinx/serialization/json/JsonBuildersTest.kt +++ b/formats/json-tests/commonTest/src/kotlinx/serialization/json/JsonBuildersTest.kt @@ -47,29 +47,23 @@ class JsonBuildersTest { @Test fun testBuildJsonArrayAddAll() { assertEquals( - """[1,2,3,4,5,null,1,2,3,4,5,null]""", + """[1,2,3,4,5,null]""", buildJsonArray { - assertTrue { addAll(1) } - assertTrue { addAll(2, 3, 4, 5, null) } assertTrue { addAll(listOf(1, 2, 3, 4, 5, null)) } }.toString() ) assertEquals( - """["a","b","c",null,"a","b","c",null]""", + """["a","b","c",null]""", buildJsonArray { - assertTrue { addAll("a") } - assertTrue { addAll("b", "c", null) } assertTrue { addAll(listOf("a", "b", "c", null)) } }.toString() ) assertEquals( - """[true,true,true,null,false,false,false,null]""", + """[true,false,null]""", buildJsonArray { - assertTrue { addAll(true) } - assertTrue { addAll(true, true, null) } - assertTrue { addAll(listOf(false, false, false, null)) } + assertTrue { addAll(listOf(true, false, null)) } }.toString() ) @@ -90,7 +84,7 @@ class JsonBuildersTest { JsonPrimitive(2), JsonPrimitive("b"), JsonPrimitive(true), - JsonNull + JsonNull, ) ) } @@ -163,11 +157,6 @@ class JsonBuildersTest { assertEquals( """[]""", buildJsonArray { - // varargs - assertFalse { addAll(*arrayOf()) } - assertFalse { addAll(*arrayOf()) } - assertFalse { addAll(*arrayOf()) } - // add collections assertFalse { addAll(listOf()) } assertFalse { addAll(listOf()) } diff --git a/formats/json/api/kotlinx-serialization-json.api b/formats/json/api/kotlinx-serialization-json.api index 0bd53d610..f503cc1e1 100644 --- a/formats/json/api/kotlinx-serialization-json.api +++ b/formats/json/api/kotlinx-serialization-json.api @@ -174,9 +174,6 @@ public final class kotlinx/serialization/json/JsonElementBuildersKt { public static final fun add (Lkotlinx/serialization/json/JsonArrayBuilder;Ljava/lang/Number;)Z public static final fun add (Lkotlinx/serialization/json/JsonArrayBuilder;Ljava/lang/String;)Z public static final fun add (Lkotlinx/serialization/json/JsonArrayBuilder;Ljava/lang/Void;)Z - public static final fun addAll (Lkotlinx/serialization/json/JsonArrayBuilder;[Ljava/lang/Boolean;)Z - public static final fun addAll (Lkotlinx/serialization/json/JsonArrayBuilder;[Ljava/lang/Number;)Z - public static final fun addAll (Lkotlinx/serialization/json/JsonArrayBuilder;[Ljava/lang/String;)Z public static final fun addAll (Lkotlinx/serialization/json/JsonArrayBuilder;[Lkotlinx/serialization/json/JsonElement;)Z public static final fun addAllBooleans (Lkotlinx/serialization/json/JsonArrayBuilder;Ljava/util/Collection;)Z public static final fun addAllNumbers (Lkotlinx/serialization/json/JsonArrayBuilder;Ljava/util/Collection;)Z diff --git a/formats/json/commonMain/src/kotlinx/serialization/json/JsonElementBuilders.kt b/formats/json/commonMain/src/kotlinx/serialization/json/JsonElementBuilders.kt index 30a116e2a..226ea6f8b 100644 --- a/formats/json/commonMain/src/kotlinx/serialization/json/JsonElementBuilders.kt +++ b/formats/json/commonMain/src/kotlinx/serialization/json/JsonElementBuilders.kt @@ -226,20 +226,5 @@ public fun JsonArrayBuilder.addAll(values: Collection): Boolean = public fun JsonArrayBuilder.addAll(values: Collection): Boolean = addAll(values.map(::JsonPrimitive)) -/** Adds the given string [values] to a resulting JSON array. */ -@ExperimentalSerializationApi -public fun JsonArrayBuilder.addAll(vararg values: String?): Boolean = - addAll(values.toList()) - -/** Adds the given boolean [values] to a resulting JSON array. */ -@ExperimentalSerializationApi -public fun JsonArrayBuilder.addAll(vararg values: Boolean?): Boolean = - addAll(values.toList()) - -/** Adds the given numeric [values] to a resulting JSON array. */ -@ExperimentalSerializationApi -public fun JsonArrayBuilder.addAll(vararg values: Number?): Boolean = - addAll(values.toList()) - @DslMarker internal annotation class JsonDslMarker From dc46b707e81113e8dd19f8fb53713f93343a63bb Mon Sep 17 00:00:00 2001 From: Adam <897017+aSemy@users.noreply.github.com> Date: Sat, 6 May 2023 09:21:25 +0200 Subject: [PATCH 12/15] remove JsonArrayBuilder addAll varargs JsonElement util --- .../serialization/json/JsonBuildersTest.kt | 35 +++---------------- .../serialization/json/JsonElementBuilders.kt | 10 ------ 2 files changed, 4 insertions(+), 41 deletions(-) diff --git a/formats/json-tests/commonTest/src/kotlinx/serialization/json/JsonBuildersTest.kt b/formats/json-tests/commonTest/src/kotlinx/serialization/json/JsonBuildersTest.kt index 0778b11ec..c2dab0894 100644 --- a/formats/json-tests/commonTest/src/kotlinx/serialization/json/JsonBuildersTest.kt +++ b/formats/json-tests/commonTest/src/kotlinx/serialization/json/JsonBuildersTest.kt @@ -68,16 +68,8 @@ class JsonBuildersTest { ) assertEquals( - """[1,"a",false,null,2,"b",true,null]""", + """[2,"b",true,null]""", buildJsonArray { - assertTrue { - addAll( - JsonPrimitive(1), - JsonPrimitive("a"), - JsonPrimitive(false), - JsonNull, - ) - } assertTrue { addAll( listOf( @@ -92,16 +84,8 @@ class JsonBuildersTest { ) assertEquals( - """[{},{},{},null,{},{},{},null]""", + """[{},{},{},null]""", buildJsonArray { - assertTrue { - addAll( - JsonObject(emptyMap()), - JsonObject(emptyMap()), - JsonObject(emptyMap()), - JsonNull, - ) - } assertTrue { addAll( listOf( @@ -116,16 +100,8 @@ class JsonBuildersTest { ) assertEquals( - """[[],[],[],null,[],[],[],null]""", + """[[],[],[],null]""", buildJsonArray { - assertTrue { - addAll( - JsonArray(emptyList()), - JsonArray(emptyList()), - JsonArray(emptyList()), - JsonNull, - ) - } assertTrue { addAll( listOf( @@ -140,11 +116,8 @@ class JsonBuildersTest { ) assertEquals( - """[null,null,null,null]""", + """[null,null]""", buildJsonArray { - assertTrue { - addAll(JsonNull, JsonNull) - } assertTrue { addAll(listOf(JsonNull, JsonNull)) } diff --git a/formats/json/commonMain/src/kotlinx/serialization/json/JsonElementBuilders.kt b/formats/json/commonMain/src/kotlinx/serialization/json/JsonElementBuilders.kt index 226ea6f8b..e1d29543e 100644 --- a/formats/json/commonMain/src/kotlinx/serialization/json/JsonElementBuilders.kt +++ b/formats/json/commonMain/src/kotlinx/serialization/json/JsonElementBuilders.kt @@ -198,16 +198,6 @@ public fun JsonArrayBuilder.addJsonObject(builderAction: JsonObjectBuilder.() -> public fun JsonArrayBuilder.addJsonArray(builderAction: JsonArrayBuilder.() -> Unit): Boolean = add(buildJsonArray(builderAction)) -/** - * Adds the given JSON [elements] to a resulting JSON array. - * - * @return `true` if the list was changed as the result of the operation. - */ -@ExperimentalSerializationApi -public fun JsonArrayBuilder.addAll(vararg elements: JsonElement): Boolean { - return addAll(elements.toList()) -} - /** Adds the given string [values] to a resulting JSON array. */ @JvmName("addAllStrings") @ExperimentalSerializationApi From d7385a262df93565d521b63a26ff0e7375aa220e Mon Sep 17 00:00:00 2001 From: Adam <897017+aSemy@users.noreply.github.com> Date: Sat, 6 May 2023 09:26:31 +0200 Subject: [PATCH 13/15] add `@return` KDoc for JsonArrayBuilder.addAll --- .../serialization/json/JsonElementBuilders.kt | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/formats/json/commonMain/src/kotlinx/serialization/json/JsonElementBuilders.kt b/formats/json/commonMain/src/kotlinx/serialization/json/JsonElementBuilders.kt index e1d29543e..b6ad16b83 100644 --- a/formats/json/commonMain/src/kotlinx/serialization/json/JsonElementBuilders.kt +++ b/formats/json/commonMain/src/kotlinx/serialization/json/JsonElementBuilders.kt @@ -198,19 +198,31 @@ public fun JsonArrayBuilder.addJsonObject(builderAction: JsonObjectBuilder.() -> public fun JsonArrayBuilder.addJsonArray(builderAction: JsonArrayBuilder.() -> Unit): Boolean = add(buildJsonArray(builderAction)) -/** Adds the given string [values] to a resulting JSON array. */ +/** + * Adds the given string [values] to a resulting JSON array. + * + * @return `true` if the list was changed as the result of the operation. + */ @JvmName("addAllStrings") @ExperimentalSerializationApi public fun JsonArrayBuilder.addAll(values: Collection): Boolean = addAll(values.map(::JsonPrimitive)) -/** Adds the given boolean [values] to a resulting JSON array. */ +/** + * Adds the given boolean [values] to a resulting JSON array. + * + * @return `true` if the list was changed as the result of the operation. + */ @JvmName("addAllBooleans") @ExperimentalSerializationApi public fun JsonArrayBuilder.addAll(values: Collection): Boolean = addAll(values.map(::JsonPrimitive)) -/** Adds the given numeric [values] to a resulting JSON array. */ +/** + * Adds the given numeric [values] to a resulting JSON array. + * + * @return `true` if the list was changed as the result of the operation. + */ @JvmName("addAllNumbers") @ExperimentalSerializationApi public fun JsonArrayBuilder.addAll(values: Collection): Boolean = From 99eb6c8d3106f61864d359aecb348656e71dfc57 Mon Sep 17 00:00:00 2001 From: Leonid Startsev Date: Tue, 9 May 2023 16:37:09 +0200 Subject: [PATCH 14/15] update API dump --- formats/json/api/kotlinx-serialization-json.api | 1 - 1 file changed, 1 deletion(-) diff --git a/formats/json/api/kotlinx-serialization-json.api b/formats/json/api/kotlinx-serialization-json.api index f503cc1e1..fec5e1b59 100644 --- a/formats/json/api/kotlinx-serialization-json.api +++ b/formats/json/api/kotlinx-serialization-json.api @@ -174,7 +174,6 @@ public final class kotlinx/serialization/json/JsonElementBuildersKt { public static final fun add (Lkotlinx/serialization/json/JsonArrayBuilder;Ljava/lang/Number;)Z public static final fun add (Lkotlinx/serialization/json/JsonArrayBuilder;Ljava/lang/String;)Z public static final fun add (Lkotlinx/serialization/json/JsonArrayBuilder;Ljava/lang/Void;)Z - public static final fun addAll (Lkotlinx/serialization/json/JsonArrayBuilder;[Lkotlinx/serialization/json/JsonElement;)Z public static final fun addAllBooleans (Lkotlinx/serialization/json/JsonArrayBuilder;Ljava/util/Collection;)Z public static final fun addAllNumbers (Lkotlinx/serialization/json/JsonArrayBuilder;Ljava/util/Collection;)Z public static final fun addAllStrings (Lkotlinx/serialization/json/JsonArrayBuilder;Ljava/util/Collection;)Z From e1440d68502dbe05949cfb2d82d2907cbeb9e1c8 Mon Sep 17 00:00:00 2001 From: Leonid Startsev Date: Tue, 9 May 2023 17:29:23 +0200 Subject: [PATCH 15/15] return star import --- .../src/kotlinx/serialization/json/JsonElementBuilders.kt | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/formats/json/commonMain/src/kotlinx/serialization/json/JsonElementBuilders.kt b/formats/json/commonMain/src/kotlinx/serialization/json/JsonElementBuilders.kt index b6ad16b83..7a25efdf4 100644 --- a/formats/json/commonMain/src/kotlinx/serialization/json/JsonElementBuilders.kt +++ b/formats/json/commonMain/src/kotlinx/serialization/json/JsonElementBuilders.kt @@ -6,9 +6,7 @@ package kotlinx.serialization.json import kotlinx.serialization.ExperimentalSerializationApi -import kotlin.contracts.ExperimentalContracts -import kotlin.contracts.InvocationKind -import kotlin.contracts.contract +import kotlin.contracts.* import kotlin.jvm.JvmName /**