Skip to content

Commit 924ac14

Browse files
committed
Undesirable change, see #2308 (comment)
1 parent 38977b3 commit 924ac14

File tree

5 files changed

+165
-36
lines changed

5 files changed

+165
-36
lines changed

formats/json-tests/commonTest/src/kotlinx/serialization/json/JsonBuildersTest.kt

+25
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,34 @@ class JsonBuildersTest {
2626
put("literal", "foo")
2727
put("null2", null)
2828
}
29+
JsonObject(json.minus("b"))
30+
json["foo"]?.jsonObject?.get("nested")?.jsonObject?.filterKeys { it == "x" }
31+
buildJsonObject {
32+
putJsonArray("x") {
33+
34+
}
35+
this.putAll(json)
36+
}
2937
assertEquals("""{"object":{"k":"v"},"array":[{"nestedLiteral":true}],"null":null,"primitive":42,"boolean":true,"literal":"foo","null2":null}""", json.toString())
3038
}
3139

40+
class X {
41+
var size = 11
42+
fun doStuff(): JsonObject {
43+
return buildJsonObject {
44+
put("foo", "bar")
45+
if (size > 10) put("additionalSize", size - 10)
46+
}
47+
}
48+
}
49+
50+
51+
52+
@Test
53+
fun x() {
54+
println(X().doStuff().toString())
55+
}
56+
3257
@Test
3358
fun testBuildJsonArray() {
3459
val json = buildJsonArray {

formats/json/api/kotlinx-serialization-json.api

+63-5
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,45 @@ public final class kotlinx/serialization/json/JsonArray$Companion {
7777
public final fun serializer ()Lkotlinx/serialization/KSerializer;
7878
}
7979

80-
public final class kotlinx/serialization/json/JsonArrayBuilder {
80+
public final class kotlinx/serialization/json/JsonArrayBuilder : java/util/List, kotlin/jvm/internal/markers/KMutableList {
8181
public fun <init> ()V
82-
public final fun add (Lkotlinx/serialization/json/JsonElement;)Z
83-
public final fun addAll (Ljava/util/Collection;)Z
82+
public fun <init> (Ljava/util/List;)V
83+
public synthetic fun <init> (Ljava/util/List;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
84+
public synthetic fun add (ILjava/lang/Object;)V
85+
public fun add (ILkotlinx/serialization/json/JsonElement;)V
86+
public synthetic fun add (Ljava/lang/Object;)Z
87+
public fun add (Lkotlinx/serialization/json/JsonElement;)Z
88+
public fun addAll (ILjava/util/Collection;)Z
89+
public fun addAll (Ljava/util/Collection;)Z
8490
public final fun build ()Lkotlinx/serialization/json/JsonArray;
91+
public fun clear ()V
92+
public final fun contains (Ljava/lang/Object;)Z
93+
public fun contains (Lkotlinx/serialization/json/JsonElement;)Z
94+
public fun containsAll (Ljava/util/Collection;)Z
95+
public synthetic fun get (I)Ljava/lang/Object;
96+
public fun get (I)Lkotlinx/serialization/json/JsonElement;
97+
public fun getSize ()I
98+
public final fun indexOf (Ljava/lang/Object;)I
99+
public fun indexOf (Lkotlinx/serialization/json/JsonElement;)I
100+
public fun isEmpty ()Z
101+
public fun iterator ()Ljava/util/Iterator;
102+
public final fun lastIndexOf (Ljava/lang/Object;)I
103+
public fun lastIndexOf (Lkotlinx/serialization/json/JsonElement;)I
104+
public fun listIterator ()Ljava/util/ListIterator;
105+
public fun listIterator (I)Ljava/util/ListIterator;
106+
public synthetic fun remove (I)Ljava/lang/Object;
107+
public final fun remove (I)Lkotlinx/serialization/json/JsonElement;
108+
public final fun remove (Ljava/lang/Object;)Z
109+
public fun remove (Lkotlinx/serialization/json/JsonElement;)Z
110+
public fun removeAll (Ljava/util/Collection;)Z
111+
public fun removeAt (I)Lkotlinx/serialization/json/JsonElement;
112+
public fun retainAll (Ljava/util/Collection;)Z
113+
public synthetic fun set (ILjava/lang/Object;)Ljava/lang/Object;
114+
public fun set (ILkotlinx/serialization/json/JsonElement;)Lkotlinx/serialization/json/JsonElement;
115+
public final fun size ()I
116+
public fun subList (II)Ljava/util/List;
117+
public fun toArray ()[Ljava/lang/Object;
118+
public fun toArray ([Ljava/lang/Object;)[Ljava/lang/Object;
85119
}
86120

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

355-
public final class kotlinx/serialization/json/JsonObjectBuilder {
389+
public final class kotlinx/serialization/json/JsonObjectBuilder : java/util/Map, kotlin/jvm/internal/markers/KMutableMap {
356390
public fun <init> ()V
391+
public fun <init> (Ljava/util/Map;)V
392+
public synthetic fun <init> (Ljava/util/Map;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
357393
public final fun build ()Lkotlinx/serialization/json/JsonObject;
358-
public final fun put (Ljava/lang/String;Lkotlinx/serialization/json/JsonElement;)Lkotlinx/serialization/json/JsonElement;
394+
public fun clear ()V
395+
public final fun containsKey (Ljava/lang/Object;)Z
396+
public fun containsKey (Ljava/lang/String;)Z
397+
public final fun containsValue (Ljava/lang/Object;)Z
398+
public fun containsValue (Lkotlinx/serialization/json/JsonElement;)Z
399+
public final fun entrySet ()Ljava/util/Set;
400+
public final synthetic fun get (Ljava/lang/Object;)Ljava/lang/Object;
401+
public final fun get (Ljava/lang/Object;)Lkotlinx/serialization/json/JsonElement;
402+
public fun get (Ljava/lang/String;)Lkotlinx/serialization/json/JsonElement;
403+
public fun getEntries ()Ljava/util/Set;
404+
public fun getKeys ()Ljava/util/Set;
405+
public fun getSize ()I
406+
public fun getValues ()Ljava/util/Collection;
407+
public fun isEmpty ()Z
408+
public final fun keySet ()Ljava/util/Set;
409+
public synthetic fun put (Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;
410+
public fun put (Ljava/lang/String;Lkotlinx/serialization/json/JsonElement;)Lkotlinx/serialization/json/JsonElement;
411+
public fun putAll (Ljava/util/Map;)V
412+
public final synthetic fun remove (Ljava/lang/Object;)Ljava/lang/Object;
413+
public final fun remove (Ljava/lang/Object;)Lkotlinx/serialization/json/JsonElement;
414+
public fun remove (Ljava/lang/String;)Lkotlinx/serialization/json/JsonElement;
415+
public final fun size ()I
416+
public final fun values ()Ljava/util/Collection;
359417
}
360418

361419
public final class kotlinx/serialization/json/JsonObjectSerializer : kotlinx/serialization/KSerializer {

formats/json/api/kotlinx-serialization-json.klib.api

+53-4
Original file line numberDiff line numberDiff line change
@@ -155,12 +155,39 @@ final class kotlinx.serialization.json/JsonArray : kotlin.collections/List<kotli
155155
final fun asJsReadonlyArrayView(): kotlin.js.collections/JsReadonlyArray<kotlinx.serialization.json/JsonElement> // kotlinx.serialization.json/JsonArray.asJsReadonlyArrayView|asJsReadonlyArrayView(){}[0]
156156
}
157157

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

161+
final val size // kotlinx.serialization.json/JsonArrayBuilder.size|{}size[0]
162+
final fun <get-size>(): kotlin/Int // kotlinx.serialization.json/JsonArrayBuilder.size.<get-size>|<get-size>(){}[0]
163+
164+
final fun add(kotlin/Int, kotlinx.serialization.json/JsonElement) // kotlinx.serialization.json/JsonArrayBuilder.add|add(kotlin.Int;kotlinx.serialization.json.JsonElement){}[0]
161165
final fun add(kotlinx.serialization.json/JsonElement): kotlin/Boolean // kotlinx.serialization.json/JsonArrayBuilder.add|add(kotlinx.serialization.json.JsonElement){}[0]
162166
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]
167+
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]
163168
final fun build(): kotlinx.serialization.json/JsonArray // kotlinx.serialization.json/JsonArrayBuilder.build|build(){}[0]
169+
final fun clear() // kotlinx.serialization.json/JsonArrayBuilder.clear|clear(){}[0]
170+
final fun contains(kotlinx.serialization.json/JsonElement): kotlin/Boolean // kotlinx.serialization.json/JsonArrayBuilder.contains|contains(kotlinx.serialization.json.JsonElement){}[0]
171+
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]
172+
final fun get(kotlin/Int): kotlinx.serialization.json/JsonElement // kotlinx.serialization.json/JsonArrayBuilder.get|get(kotlin.Int){}[0]
173+
final fun indexOf(kotlinx.serialization.json/JsonElement): kotlin/Int // kotlinx.serialization.json/JsonArrayBuilder.indexOf|indexOf(kotlinx.serialization.json.JsonElement){}[0]
174+
final fun isEmpty(): kotlin/Boolean // kotlinx.serialization.json/JsonArrayBuilder.isEmpty|isEmpty(){}[0]
175+
final fun iterator(): kotlin.collections/MutableIterator<kotlinx.serialization.json/JsonElement> // kotlinx.serialization.json/JsonArrayBuilder.iterator|iterator(){}[0]
176+
final fun lastIndexOf(kotlinx.serialization.json/JsonElement): kotlin/Int // kotlinx.serialization.json/JsonArrayBuilder.lastIndexOf|lastIndexOf(kotlinx.serialization.json.JsonElement){}[0]
177+
final fun listIterator(): kotlin.collections/MutableListIterator<kotlinx.serialization.json/JsonElement> // kotlinx.serialization.json/JsonArrayBuilder.listIterator|listIterator(){}[0]
178+
final fun listIterator(kotlin/Int): kotlin.collections/MutableListIterator<kotlinx.serialization.json/JsonElement> // kotlinx.serialization.json/JsonArrayBuilder.listIterator|listIterator(kotlin.Int){}[0]
179+
final fun remove(kotlinx.serialization.json/JsonElement): kotlin/Boolean // kotlinx.serialization.json/JsonArrayBuilder.remove|remove(kotlinx.serialization.json.JsonElement){}[0]
180+
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]
181+
final fun removeAt(kotlin/Int): kotlinx.serialization.json/JsonElement // kotlinx.serialization.json/JsonArrayBuilder.removeAt|removeAt(kotlin.Int){}[0]
182+
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]
183+
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]
184+
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]
185+
186+
// Targets: [js]
187+
final fun asJsArrayView(): kotlin.js.collections/JsArray<kotlinx.serialization.json/JsonElement> // kotlinx.serialization.json/JsonArrayBuilder.asJsArrayView|asJsArrayView(){}[0]
188+
189+
// Targets: [js]
190+
final fun asJsReadonlyArrayView(): kotlin.js.collections/JsReadonlyArray<kotlinx.serialization.json/JsonElement> // kotlinx.serialization.json/JsonArrayBuilder.asJsReadonlyArrayView|asJsReadonlyArrayView(){}[0]
164191
}
165192

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

292-
final class kotlinx.serialization.json/JsonObjectBuilder { // kotlinx.serialization.json/JsonObjectBuilder|null[0]
293-
constructor <init>() // kotlinx.serialization.json/JsonObjectBuilder.<init>|<init>(){}[0]
319+
final class kotlinx.serialization.json/JsonObjectBuilder : kotlin.collections/MutableMap<kotlin/String, kotlinx.serialization.json/JsonElement> { // kotlinx.serialization.json/JsonObjectBuilder|null[0]
320+
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]
321+
322+
final val entries // kotlinx.serialization.json/JsonObjectBuilder.entries|{}entries[0]
323+
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]
324+
final val keys // kotlinx.serialization.json/JsonObjectBuilder.keys|{}keys[0]
325+
final fun <get-keys>(): kotlin.collections/MutableSet<kotlin/String> // kotlinx.serialization.json/JsonObjectBuilder.keys.<get-keys>|<get-keys>(){}[0]
326+
final val size // kotlinx.serialization.json/JsonObjectBuilder.size|{}size[0]
327+
final fun <get-size>(): kotlin/Int // kotlinx.serialization.json/JsonObjectBuilder.size.<get-size>|<get-size>(){}[0]
328+
final val values // kotlinx.serialization.json/JsonObjectBuilder.values|{}values[0]
329+
final fun <get-values>(): kotlin.collections/MutableCollection<kotlinx.serialization.json/JsonElement> // kotlinx.serialization.json/JsonObjectBuilder.values.<get-values>|<get-values>(){}[0]
294330

295331
final fun build(): kotlinx.serialization.json/JsonObject // kotlinx.serialization.json/JsonObjectBuilder.build|build(){}[0]
332+
final fun clear() // kotlinx.serialization.json/JsonObjectBuilder.clear|clear(){}[0]
333+
final fun containsKey(kotlin/String): kotlin/Boolean // kotlinx.serialization.json/JsonObjectBuilder.containsKey|containsKey(kotlin.String){}[0]
334+
final fun containsValue(kotlinx.serialization.json/JsonElement): kotlin/Boolean // kotlinx.serialization.json/JsonObjectBuilder.containsValue|containsValue(kotlinx.serialization.json.JsonElement){}[0]
335+
final fun get(kotlin/String): kotlinx.serialization.json/JsonElement? // kotlinx.serialization.json/JsonObjectBuilder.get|get(kotlin.String){}[0]
336+
final fun isEmpty(): kotlin/Boolean // kotlinx.serialization.json/JsonObjectBuilder.isEmpty|isEmpty(){}[0]
296337
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]
338+
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]
339+
final fun remove(kotlin/String): kotlinx.serialization.json/JsonElement? // kotlinx.serialization.json/JsonObjectBuilder.remove|remove(kotlin.String){}[0]
340+
341+
// Targets: [js]
342+
final fun asJsMapView(): kotlin.js.collections/JsMap<kotlin/String, kotlinx.serialization.json/JsonElement> // kotlinx.serialization.json/JsonObjectBuilder.asJsMapView|asJsMapView(){}[0]
343+
344+
// Targets: [js]
345+
final fun asJsReadonlyMapView(): kotlin.js.collections/JsReadonlyMap<kotlin/String, kotlinx.serialization.json/JsonElement> // kotlinx.serialization.json/JsonObjectBuilder.asJsReadonlyMapView|asJsReadonlyMapView(){}[0]
297346
}
298347

299348
sealed class kotlinx.serialization.json/Json : kotlinx.serialization/StringFormat { // kotlinx.serialization.json/Json|null[0]

formats/json/commonMain/src/kotlinx/serialization/json/JsonElementBuilders.kt

+23-26
Original file line numberDiff line numberDiff line change
@@ -57,17 +57,17 @@ public inline fun buildJsonArray(builderAction: JsonArrayBuilder.() -> Unit): Js
5757
/**
5858
* DSL builder for a [JsonObject]. To create an instance of builder, use [buildJsonObject] build function.
5959
*/
60+
// Source breaking change
6061
@JsonDslMarker
61-
public class JsonObjectBuilder @PublishedApi internal constructor() {
62-
63-
private val content: MutableMap<String, JsonElement> = linkedMapOf()
62+
public class JsonObjectBuilder @PublishedApi internal constructor(private val content: MutableMap<String, JsonElement> = linkedMapOf()) :
63+
MutableMap<String, JsonElement> by content {
6464

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

7272
@PublishedApi
7373
internal fun build(): JsonObject = JsonObject(content)
@@ -123,28 +123,25 @@ public fun JsonObjectBuilder.put(key: String, value: Nothing?): JsonElement? = p
123123
* DSL builder for a [JsonArray]. To create an instance of builder, use [buildJsonArray] build function.
124124
*/
125125
@JsonDslMarker
126-
public class JsonArrayBuilder @PublishedApi internal constructor() {
127-
128-
private val content: MutableList<JsonElement> = mutableListOf()
129-
130-
/**
131-
* Adds the given JSON [element] to a resulting JSON array.
132-
*
133-
* Always returns `true` similarly to [ArrayList] specification.
134-
*/
135-
public fun add(element: JsonElement): Boolean {
136-
content += element
137-
return true
138-
}
139-
140-
/**
141-
* Adds the given JSON [elements] to a resulting JSON array.
142-
*
143-
* @return `true` if the list was changed as the result of the operation.
144-
*/
145-
@ExperimentalSerializationApi
146-
public fun addAll(elements: Collection<JsonElement>): Boolean =
147-
content.addAll(elements)
126+
public class JsonArrayBuilder @PublishedApi internal constructor(private val content: MutableList<JsonElement> = mutableListOf()): MutableList<JsonElement> by content {
127+
// /**
128+
// * Adds the given JSON [element] to a resulting JSON array.
129+
// *
130+
// * Always returns `true` similarly to [ArrayList] specification.
131+
// */
132+
// public fun add(element: JsonElement): Boolean {
133+
// content += element
134+
// return true
135+
// }
136+
//
137+
// /**
138+
// * Adds the given JSON [elements] to a resulting JSON array.
139+
// *
140+
// * @return `true` if the list was changed as the result of the operation.
141+
// */
142+
// @ExperimentalSerializationApi
143+
// public fun addAll(elements: Collection<JsonElement>): Boolean =
144+
// content.addAll(elements)
148145

149146
@PublishedApi
150147
internal fun build(): JsonArray = JsonArray(content)

formats/json/jsMain/src/kotlinx/serialization/json/internal/DynamicDecoders.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ private open class DynamicInput(
6060

6161
return buildJsonObject {
6262
for (i in 0 until size) {
63-
val key = keys[i]
63+
val key = this@DynamicInput.keys[i]
6464
val value = json.decodeDynamic(JsonElement.serializer(), value[key])
6565
put(key.toString(), value)
6666
}

0 commit comments

Comments
 (0)