Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement addAll functions for JsonArrayBuilder #2156

Merged
merged 15 commits into from
May 9, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,105 @@ class JsonBuildersTest {
}
assertEquals("""[true,[1,2,3,4,5,6,7,8,9,10],null,{"stringKey":"stringValue"}]""", json.toString())
}

@Test
fun testBuildJsonArrayAddAll() {
assertEquals(
"""[1,2,3,4,5,null]""",
buildJsonArray {
assertTrue { addAll(listOf(1, 2, 3, 4, 5, null)) }
}.toString()
)

assertEquals(
"""["a","b","c",null]""",
buildJsonArray {
assertTrue { addAll(listOf("a", "b", "c", null)) }
}.toString()
)

assertEquals(
"""[true,false,null]""",
buildJsonArray {
assertTrue { addAll(listOf(true, false, null)) }
}.toString()
)

assertEquals(
"""[2,"b",true,null]""",
buildJsonArray {
assertTrue {
addAll(
listOf(
JsonPrimitive(2),
JsonPrimitive("b"),
JsonPrimitive(true),
JsonNull,
)
)
}
}.toString()
)

assertEquals(
"""[{},{},{},null]""",
buildJsonArray {
assertTrue {
addAll(
listOf(
JsonObject(emptyMap()),
JsonObject(emptyMap()),
JsonObject(emptyMap()),
JsonNull
)
)
}
}.toString()
)

assertEquals(
"""[[],[],[],null]""",
buildJsonArray {
assertTrue {
addAll(
listOf(
JsonArray(emptyList()),
JsonArray(emptyList()),
JsonArray(emptyList()),
JsonNull
)
)
}
}.toString()
)

assertEquals(
"""[null,null]""",
buildJsonArray {
assertTrue {
addAll(listOf(JsonNull, JsonNull))
}
}.toString()
)
}

@Test
fun testBuildJsonArrayAddAllNotModified() {
assertEquals(
"""[]""",
buildJsonArray {
// add collections
assertFalse { addAll(listOf<Number>()) }
assertFalse { addAll(listOf<String>()) }
assertFalse { addAll(listOf<Boolean>()) }

// add json elements
assertFalse { addAll(listOf()) }
assertFalse { addAll(listOf<JsonNull>()) }
assertFalse { addAll(listOf<JsonObject>()) }
assertFalse { addAll(listOf<JsonArray>()) }
assertFalse { addAll(listOf<JsonPrimitive>()) }
}.toString()
)
}
}
4 changes: 4 additions & 0 deletions formats/json/api/kotlinx-serialization-json.api
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ public final class kotlinx/serialization/json/JsonArray$Companion {
public final class kotlinx/serialization/json/JsonArrayBuilder {
public fun <init> ()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;
}

Expand Down Expand Up @@ -173,6 +174,9 @@ 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 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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
*/
@ExperimentalSerializationApi
public fun addAll(elements: Collection<JsonElement>): Boolean =
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's good this function's signature matches one from MutableList — we can add superclass later without breaking anything 👍🏻

content.addAll(elements)

@PublishedApi
internal fun build(): JsonArray = JsonArray(content)
}
Expand Down Expand Up @@ -186,6 +196,35 @@ 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.
*
* @return `true` if the list was changed as the result of the operation.
*/
@JvmName("addAllStrings")
@ExperimentalSerializationApi
public fun JsonArrayBuilder.addAll(values: Collection<String?>): Boolean =
addAll(values.map(::JsonPrimitive))

/**
* 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?>): Boolean =
addAll(values.map(::JsonPrimitive))

/**
* 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<Number?>): Boolean =
addAll(values.map(::JsonPrimitive))

@DslMarker
internal annotation class JsonDslMarker