Skip to content

Commit 318751f

Browse files
committed
Replace some object comparisons with type checks
`JsonNull` is a non-`data` object, meaning its `==` is not safe for comparing instances of `JsonNull` after deserialization. After ^KT-76635, such comparisons are discouraged in favor of `is`-checks. I presume turning `JsonNull` into a `data` object may be a decision requiring additional attention, so I don't want it to block the MR related to ^KT-76635. Intuitively, it would probably be safe if we also add a custom `toString()` override that would match the default one? The same story with the checks inside `selectMode()`.
1 parent 7dcb068 commit 318751f

File tree

2 files changed

+5
-5
lines changed

2 files changed

+5
-5
lines changed

formats/json/commonMain/src/kotlinx/serialization/json/internal/TreeJsonDecoder.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public fun <T> readJson(json: Json, element: JsonElement, deserializer: Deserial
2020
val input = when (element) {
2121
is JsonObject -> JsonTreeDecoder(json, element)
2222
is JsonArray -> JsonTreeListDecoder(json, element)
23-
is JsonLiteral, JsonNull -> JsonPrimitiveDecoder(json, element as JsonPrimitive)
23+
is JsonLiteral, is JsonNull -> JsonPrimitiveDecoder(json, element)
2424
}
2525
return input.decodeSerializableValue(deserializer)
2626
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -235,10 +235,10 @@ private class DynamicObjectEncoder(
235235

236236
@OptIn(ExperimentalSerializationApi::class)
237237
fun selectMode(desc: SerialDescriptor) = when (desc.kind) {
238-
StructureKind.CLASS, StructureKind.OBJECT, SerialKind.CONTEXTUAL -> WriteMode.OBJ
239-
StructureKind.LIST, is PolymorphicKind -> WriteMode.LIST
240-
StructureKind.MAP -> WriteMode.MAP
241-
is PrimitiveKind, SerialKind.ENUM -> {
238+
is StructureKind.CLASS, is StructureKind.OBJECT, is SerialKind.CONTEXTUAL -> WriteMode.OBJ
239+
is StructureKind.LIST, is PolymorphicKind -> WriteMode.LIST
240+
is StructureKind.MAP -> WriteMode.MAP
241+
is PrimitiveKind, is SerialKind.ENUM -> {
242242
// the two cases are handled in DynamicObjectSerializer. But compiler does not know
243243
error("DynamicObjectSerializer does not support serialization of singular primitive values or enum types.")
244244
}

0 commit comments

Comments
 (0)