Skip to content

Commit

Permalink
Undesirable change, see #2308 (comment)
Browse files Browse the repository at this point in the history
  • Loading branch information
sandwwraith committed Nov 4, 2024
1 parent 38977b3 commit 924ac14
Show file tree
Hide file tree
Showing 5 changed files with 165 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,34 @@ class JsonBuildersTest {
put("literal", "foo")
put("null2", null)
}
JsonObject(json.minus("b"))
json["foo"]?.jsonObject?.get("nested")?.jsonObject?.filterKeys { it == "x" }
buildJsonObject {
putJsonArray("x") {

}
this.putAll(json)
}
assertEquals("""{"object":{"k":"v"},"array":[{"nestedLiteral":true}],"null":null,"primitive":42,"boolean":true,"literal":"foo","null2":null}""", json.toString())
}

class X {
var size = 11
fun doStuff(): JsonObject {
return buildJsonObject {
put("foo", "bar")
if (size > 10) put("additionalSize", size - 10)
}
}
}



@Test
fun x() {
println(X().doStuff().toString())
}

@Test
fun testBuildJsonArray() {
val json = buildJsonArray {
Expand Down
68 changes: 63 additions & 5 deletions formats/json/api/kotlinx-serialization-json.api
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,45 @@ public final class kotlinx/serialization/json/JsonArray$Companion {
public final fun serializer ()Lkotlinx/serialization/KSerializer;
}

public final class kotlinx/serialization/json/JsonArrayBuilder {
public final class kotlinx/serialization/json/JsonArrayBuilder : java/util/List, kotlin/jvm/internal/markers/KMutableList {
public fun <init> ()V
public final fun add (Lkotlinx/serialization/json/JsonElement;)Z
public final fun addAll (Ljava/util/Collection;)Z
public fun <init> (Ljava/util/List;)V
public synthetic fun <init> (Ljava/util/List;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
public synthetic fun add (ILjava/lang/Object;)V
public fun add (ILkotlinx/serialization/json/JsonElement;)V
public synthetic fun add (Ljava/lang/Object;)Z
public fun add (Lkotlinx/serialization/json/JsonElement;)Z
public fun addAll (ILjava/util/Collection;)Z
public fun addAll (Ljava/util/Collection;)Z
public final fun build ()Lkotlinx/serialization/json/JsonArray;
public fun clear ()V
public final fun contains (Ljava/lang/Object;)Z
public fun contains (Lkotlinx/serialization/json/JsonElement;)Z
public fun containsAll (Ljava/util/Collection;)Z
public synthetic fun get (I)Ljava/lang/Object;
public fun get (I)Lkotlinx/serialization/json/JsonElement;
public fun getSize ()I
public final fun indexOf (Ljava/lang/Object;)I
public fun indexOf (Lkotlinx/serialization/json/JsonElement;)I
public fun isEmpty ()Z
public fun iterator ()Ljava/util/Iterator;
public final fun lastIndexOf (Ljava/lang/Object;)I
public fun lastIndexOf (Lkotlinx/serialization/json/JsonElement;)I
public fun listIterator ()Ljava/util/ListIterator;
public fun listIterator (I)Ljava/util/ListIterator;
public synthetic fun remove (I)Ljava/lang/Object;
public final fun remove (I)Lkotlinx/serialization/json/JsonElement;
public final fun remove (Ljava/lang/Object;)Z
public fun remove (Lkotlinx/serialization/json/JsonElement;)Z
public fun removeAll (Ljava/util/Collection;)Z
public fun removeAt (I)Lkotlinx/serialization/json/JsonElement;
public fun retainAll (Ljava/util/Collection;)Z
public synthetic fun set (ILjava/lang/Object;)Ljava/lang/Object;
public fun set (ILkotlinx/serialization/json/JsonElement;)Lkotlinx/serialization/json/JsonElement;
public final fun size ()I
public fun subList (II)Ljava/util/List;
public fun toArray ()[Ljava/lang/Object;
public fun toArray ([Ljava/lang/Object;)[Ljava/lang/Object;
}

public final class kotlinx/serialization/json/JsonArraySerializer : kotlinx/serialization/KSerializer {
Expand Down Expand Up @@ -352,10 +386,34 @@ public final class kotlinx/serialization/json/JsonObject$Companion {
public final fun serializer ()Lkotlinx/serialization/KSerializer;
}

public final class kotlinx/serialization/json/JsonObjectBuilder {
public final class kotlinx/serialization/json/JsonObjectBuilder : java/util/Map, kotlin/jvm/internal/markers/KMutableMap {
public fun <init> ()V
public fun <init> (Ljava/util/Map;)V
public synthetic fun <init> (Ljava/util/Map;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
public final fun build ()Lkotlinx/serialization/json/JsonObject;
public final fun put (Ljava/lang/String;Lkotlinx/serialization/json/JsonElement;)Lkotlinx/serialization/json/JsonElement;
public fun clear ()V
public final fun containsKey (Ljava/lang/Object;)Z
public fun containsKey (Ljava/lang/String;)Z
public final fun containsValue (Ljava/lang/Object;)Z
public fun containsValue (Lkotlinx/serialization/json/JsonElement;)Z
public final fun entrySet ()Ljava/util/Set;
public final synthetic fun get (Ljava/lang/Object;)Ljava/lang/Object;
public final fun get (Ljava/lang/Object;)Lkotlinx/serialization/json/JsonElement;
public fun get (Ljava/lang/String;)Lkotlinx/serialization/json/JsonElement;
public fun getEntries ()Ljava/util/Set;
public fun getKeys ()Ljava/util/Set;
public fun getSize ()I
public fun getValues ()Ljava/util/Collection;
public fun isEmpty ()Z
public final fun keySet ()Ljava/util/Set;
public synthetic fun put (Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;
public fun put (Ljava/lang/String;Lkotlinx/serialization/json/JsonElement;)Lkotlinx/serialization/json/JsonElement;
public fun putAll (Ljava/util/Map;)V
public final synthetic fun remove (Ljava/lang/Object;)Ljava/lang/Object;
public final fun remove (Ljava/lang/Object;)Lkotlinx/serialization/json/JsonElement;
public fun remove (Ljava/lang/String;)Lkotlinx/serialization/json/JsonElement;
public final fun size ()I
public final fun values ()Ljava/util/Collection;
}

public final class kotlinx/serialization/json/JsonObjectSerializer : kotlinx/serialization/KSerializer {
Expand Down
57 changes: 53 additions & 4 deletions formats/json/api/kotlinx-serialization-json.klib.api
Original file line number Diff line number Diff line change
Expand Up @@ -155,12 +155,39 @@ final class kotlinx.serialization.json/JsonArray : kotlin.collections/List<kotli
final fun asJsReadonlyArrayView(): kotlin.js.collections/JsReadonlyArray<kotlinx.serialization.json/JsonElement> // kotlinx.serialization.json/JsonArray.asJsReadonlyArrayView|asJsReadonlyArrayView(){}[0]
}

final class kotlinx.serialization.json/JsonArrayBuilder { // kotlinx.serialization.json/JsonArrayBuilder|null[0]
constructor <init>() // kotlinx.serialization.json/JsonArrayBuilder.<init>|<init>(){}[0]
final class kotlinx.serialization.json/JsonArrayBuilder : kotlin.collections/MutableList<kotlinx.serialization.json/JsonElement> { // kotlinx.serialization.json/JsonArrayBuilder|null[0]
constructor <init>(kotlin.collections/MutableList<kotlinx.serialization.json/JsonElement> = ...) // kotlinx.serialization.json/JsonArrayBuilder.<init>|<init>(kotlin.collections.MutableList<kotlinx.serialization.json.JsonElement>){}[0]

final val size // kotlinx.serialization.json/JsonArrayBuilder.size|{}size[0]
final fun <get-size>(): kotlin/Int // kotlinx.serialization.json/JsonArrayBuilder.size.<get-size>|<get-size>(){}[0]

final fun add(kotlin/Int, kotlinx.serialization.json/JsonElement) // kotlinx.serialization.json/JsonArrayBuilder.add|add(kotlin.Int;kotlinx.serialization.json.JsonElement){}[0]
final fun add(kotlinx.serialization.json/JsonElement): kotlin/Boolean // kotlinx.serialization.json/JsonArrayBuilder.add|add(kotlinx.serialization.json.JsonElement){}[0]
final fun addAll(kotlin.collections/Collection<kotlinx.serialization.json/JsonElement>): kotlin/Boolean // kotlinx.serialization.json/JsonArrayBuilder.addAll|addAll(kotlin.collections.Collection<kotlinx.serialization.json.JsonElement>){}[0]
final fun addAll(kotlin/Int, kotlin.collections/Collection<kotlinx.serialization.json/JsonElement>): kotlin/Boolean // kotlinx.serialization.json/JsonArrayBuilder.addAll|addAll(kotlin.Int;kotlin.collections.Collection<kotlinx.serialization.json.JsonElement>){}[0]
final fun build(): kotlinx.serialization.json/JsonArray // kotlinx.serialization.json/JsonArrayBuilder.build|build(){}[0]
final fun clear() // kotlinx.serialization.json/JsonArrayBuilder.clear|clear(){}[0]
final fun contains(kotlinx.serialization.json/JsonElement): kotlin/Boolean // kotlinx.serialization.json/JsonArrayBuilder.contains|contains(kotlinx.serialization.json.JsonElement){}[0]
final fun containsAll(kotlin.collections/Collection<kotlinx.serialization.json/JsonElement>): kotlin/Boolean // kotlinx.serialization.json/JsonArrayBuilder.containsAll|containsAll(kotlin.collections.Collection<kotlinx.serialization.json.JsonElement>){}[0]
final fun get(kotlin/Int): kotlinx.serialization.json/JsonElement // kotlinx.serialization.json/JsonArrayBuilder.get|get(kotlin.Int){}[0]
final fun indexOf(kotlinx.serialization.json/JsonElement): kotlin/Int // kotlinx.serialization.json/JsonArrayBuilder.indexOf|indexOf(kotlinx.serialization.json.JsonElement){}[0]
final fun isEmpty(): kotlin/Boolean // kotlinx.serialization.json/JsonArrayBuilder.isEmpty|isEmpty(){}[0]
final fun iterator(): kotlin.collections/MutableIterator<kotlinx.serialization.json/JsonElement> // kotlinx.serialization.json/JsonArrayBuilder.iterator|iterator(){}[0]
final fun lastIndexOf(kotlinx.serialization.json/JsonElement): kotlin/Int // kotlinx.serialization.json/JsonArrayBuilder.lastIndexOf|lastIndexOf(kotlinx.serialization.json.JsonElement){}[0]
final fun listIterator(): kotlin.collections/MutableListIterator<kotlinx.serialization.json/JsonElement> // kotlinx.serialization.json/JsonArrayBuilder.listIterator|listIterator(){}[0]
final fun listIterator(kotlin/Int): kotlin.collections/MutableListIterator<kotlinx.serialization.json/JsonElement> // kotlinx.serialization.json/JsonArrayBuilder.listIterator|listIterator(kotlin.Int){}[0]
final fun remove(kotlinx.serialization.json/JsonElement): kotlin/Boolean // kotlinx.serialization.json/JsonArrayBuilder.remove|remove(kotlinx.serialization.json.JsonElement){}[0]
final fun removeAll(kotlin.collections/Collection<kotlinx.serialization.json/JsonElement>): kotlin/Boolean // kotlinx.serialization.json/JsonArrayBuilder.removeAll|removeAll(kotlin.collections.Collection<kotlinx.serialization.json.JsonElement>){}[0]
final fun removeAt(kotlin/Int): kotlinx.serialization.json/JsonElement // kotlinx.serialization.json/JsonArrayBuilder.removeAt|removeAt(kotlin.Int){}[0]
final fun retainAll(kotlin.collections/Collection<kotlinx.serialization.json/JsonElement>): kotlin/Boolean // kotlinx.serialization.json/JsonArrayBuilder.retainAll|retainAll(kotlin.collections.Collection<kotlinx.serialization.json.JsonElement>){}[0]
final fun set(kotlin/Int, kotlinx.serialization.json/JsonElement): kotlinx.serialization.json/JsonElement // kotlinx.serialization.json/JsonArrayBuilder.set|set(kotlin.Int;kotlinx.serialization.json.JsonElement){}[0]
final fun subList(kotlin/Int, kotlin/Int): kotlin.collections/MutableList<kotlinx.serialization.json/JsonElement> // kotlinx.serialization.json/JsonArrayBuilder.subList|subList(kotlin.Int;kotlin.Int){}[0]

// Targets: [js]
final fun asJsArrayView(): kotlin.js.collections/JsArray<kotlinx.serialization.json/JsonElement> // kotlinx.serialization.json/JsonArrayBuilder.asJsArrayView|asJsArrayView(){}[0]

// Targets: [js]
final fun asJsReadonlyArrayView(): kotlin.js.collections/JsReadonlyArray<kotlinx.serialization.json/JsonElement> // kotlinx.serialization.json/JsonArrayBuilder.asJsReadonlyArrayView|asJsReadonlyArrayView(){}[0]
}

final class kotlinx.serialization.json/JsonBuilder { // kotlinx.serialization.json/JsonBuilder|null[0]
Expand Down Expand Up @@ -289,11 +316,33 @@ final class kotlinx.serialization.json/JsonObject : kotlin.collections/Map<kotli
final fun asJsReadonlyMapView(): kotlin.js.collections/JsReadonlyMap<kotlin/String, kotlinx.serialization.json/JsonElement> // kotlinx.serialization.json/JsonObject.asJsReadonlyMapView|asJsReadonlyMapView(){}[0]
}

final class kotlinx.serialization.json/JsonObjectBuilder { // kotlinx.serialization.json/JsonObjectBuilder|null[0]
constructor <init>() // kotlinx.serialization.json/JsonObjectBuilder.<init>|<init>(){}[0]
final class kotlinx.serialization.json/JsonObjectBuilder : kotlin.collections/MutableMap<kotlin/String, kotlinx.serialization.json/JsonElement> { // kotlinx.serialization.json/JsonObjectBuilder|null[0]
constructor <init>(kotlin.collections/MutableMap<kotlin/String, kotlinx.serialization.json/JsonElement> = ...) // kotlinx.serialization.json/JsonObjectBuilder.<init>|<init>(kotlin.collections.MutableMap<kotlin.String,kotlinx.serialization.json.JsonElement>){}[0]

final val entries // kotlinx.serialization.json/JsonObjectBuilder.entries|{}entries[0]
final fun <get-entries>(): kotlin.collections/MutableSet<kotlin.collections/MutableMap.MutableEntry<kotlin/String, kotlinx.serialization.json/JsonElement>> // kotlinx.serialization.json/JsonObjectBuilder.entries.<get-entries>|<get-entries>(){}[0]
final val keys // kotlinx.serialization.json/JsonObjectBuilder.keys|{}keys[0]
final fun <get-keys>(): kotlin.collections/MutableSet<kotlin/String> // kotlinx.serialization.json/JsonObjectBuilder.keys.<get-keys>|<get-keys>(){}[0]
final val size // kotlinx.serialization.json/JsonObjectBuilder.size|{}size[0]
final fun <get-size>(): kotlin/Int // kotlinx.serialization.json/JsonObjectBuilder.size.<get-size>|<get-size>(){}[0]
final val values // kotlinx.serialization.json/JsonObjectBuilder.values|{}values[0]
final fun <get-values>(): kotlin.collections/MutableCollection<kotlinx.serialization.json/JsonElement> // kotlinx.serialization.json/JsonObjectBuilder.values.<get-values>|<get-values>(){}[0]

final fun build(): kotlinx.serialization.json/JsonObject // kotlinx.serialization.json/JsonObjectBuilder.build|build(){}[0]
final fun clear() // kotlinx.serialization.json/JsonObjectBuilder.clear|clear(){}[0]
final fun containsKey(kotlin/String): kotlin/Boolean // kotlinx.serialization.json/JsonObjectBuilder.containsKey|containsKey(kotlin.String){}[0]
final fun containsValue(kotlinx.serialization.json/JsonElement): kotlin/Boolean // kotlinx.serialization.json/JsonObjectBuilder.containsValue|containsValue(kotlinx.serialization.json.JsonElement){}[0]
final fun get(kotlin/String): kotlinx.serialization.json/JsonElement? // kotlinx.serialization.json/JsonObjectBuilder.get|get(kotlin.String){}[0]
final fun isEmpty(): kotlin/Boolean // kotlinx.serialization.json/JsonObjectBuilder.isEmpty|isEmpty(){}[0]
final fun put(kotlin/String, kotlinx.serialization.json/JsonElement): kotlinx.serialization.json/JsonElement? // kotlinx.serialization.json/JsonObjectBuilder.put|put(kotlin.String;kotlinx.serialization.json.JsonElement){}[0]
final fun putAll(kotlin.collections/Map<out kotlin/String, kotlinx.serialization.json/JsonElement>) // kotlinx.serialization.json/JsonObjectBuilder.putAll|putAll(kotlin.collections.Map<out|kotlin.String,kotlinx.serialization.json.JsonElement>){}[0]
final fun remove(kotlin/String): kotlinx.serialization.json/JsonElement? // kotlinx.serialization.json/JsonObjectBuilder.remove|remove(kotlin.String){}[0]

// Targets: [js]
final fun asJsMapView(): kotlin.js.collections/JsMap<kotlin/String, kotlinx.serialization.json/JsonElement> // kotlinx.serialization.json/JsonObjectBuilder.asJsMapView|asJsMapView(){}[0]

// Targets: [js]
final fun asJsReadonlyMapView(): kotlin.js.collections/JsReadonlyMap<kotlin/String, kotlinx.serialization.json/JsonElement> // kotlinx.serialization.json/JsonObjectBuilder.asJsReadonlyMapView|asJsReadonlyMapView(){}[0]
}

sealed class kotlinx.serialization.json/Json : kotlinx.serialization/StringFormat { // kotlinx.serialization.json/Json|null[0]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,17 +57,17 @@ public inline fun buildJsonArray(builderAction: JsonArrayBuilder.() -> Unit): Js
/**
* DSL builder for a [JsonObject]. To create an instance of builder, use [buildJsonObject] build function.
*/
// Source breaking change
@JsonDslMarker
public class JsonObjectBuilder @PublishedApi internal constructor() {

private val content: MutableMap<String, JsonElement> = linkedMapOf()
public class JsonObjectBuilder @PublishedApi internal constructor(private val content: MutableMap<String, JsonElement> = linkedMapOf()) :
MutableMap<String, JsonElement> by content {

/**
* Add the given JSON [element] to a resulting JSON object using the given [key].
*
* Returns the previous value associated with [key], or `null` if the key was not present.
*/
public fun put(key: String, element: JsonElement): JsonElement? = content.put(key, element)
// public fun put(key: String, element: JsonElement): JsonElement? = content.put(key, element)

@PublishedApi
internal fun build(): JsonObject = JsonObject(content)
Expand Down Expand Up @@ -123,28 +123,25 @@ public fun JsonObjectBuilder.put(key: String, value: Nothing?): JsonElement? = p
* DSL builder for a [JsonArray]. To create an instance of builder, use [buildJsonArray] build function.
*/
@JsonDslMarker
public class JsonArrayBuilder @PublishedApi internal constructor() {

private val content: MutableList<JsonElement> = mutableListOf()

/**
* Adds the given JSON [element] to a resulting JSON array.
*
* Always returns `true` similarly to [ArrayList] specification.
*/
public fun add(element: JsonElement): Boolean {
content += element
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 =
content.addAll(elements)
public class JsonArrayBuilder @PublishedApi internal constructor(private val content: MutableList<JsonElement> = mutableListOf()): MutableList<JsonElement> by content {
// /**
// * Adds the given JSON [element] to a resulting JSON array.
// *
// * Always returns `true` similarly to [ArrayList] specification.
// */
// public fun add(element: JsonElement): Boolean {
// content += element
// 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 =
// content.addAll(elements)

@PublishedApi
internal fun build(): JsonArray = JsonArray(content)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ private open class DynamicInput(

return buildJsonObject {
for (i in 0 until size) {
val key = keys[i]
val key = this@DynamicInput.keys[i]
val value = json.decodeDynamic(JsonElement.serializer(), value[key])
put(key.toString(), value)
}
Expand Down

0 comments on commit 924ac14

Please sign in to comment.