You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Refine exception messages in case of deserializing data from JsonElement. (#2648)
Such a code path is often used when we cannot find type discriminator as a first key in Json (for example, if json input is invalid, and we got a string instead of an object). In such cases, we should display a nice error message.
Also add tag stack — equivalent of a Json path — to most of the error messages. Note that it is far from an ideal, since changing between string and tree decoders (such happens in polymorphism) won't preserve stack or path correctly. Yet, it is the best we can do for now.
Fixes#2630
Co-authored-by: Sergey Shanshin <[email protected]>
final fun (kotlinx.serialization.descriptors/SerialDescriptor).getTag(kotlin/Int): kotlin/String // kotlinx.serialization.internal/NamedValueDecoder.getTag|[email protected](kotlin.Int){}[0]
214
214
final fun nested(kotlin/String): kotlin/String // kotlinx.serialization.internal/NamedValueDecoder.nested|nested(kotlin.String){}[0]
215
+
final fun renderTagStack(): kotlin/String // kotlinx.serialization.internal/NamedValueDecoder.renderTagStack|renderTagStack(){}[0]
215
216
open fun composeName(kotlin/String, kotlin/String): kotlin/String // kotlinx.serialization.internal/NamedValueDecoder.composeName|composeName(kotlin.String;kotlin.String){}[0]
216
217
open fun elementName(kotlinx.serialization.descriptors/SerialDescriptor, kotlin/Int): kotlin/String // kotlinx.serialization.internal/NamedValueDecoder.elementName|elementName(kotlinx.serialization.descriptors.SerialDescriptor;kotlin.Int){}[0]
if (!literal.isString) throwJsonDecodingException(
146
-
-1, "String literal for key '$tag' should be quoted.\n$lenientHint", currentObject().toString()
146
+
val value = cast<JsonPrimitive>(currentElement(tag), "string", tag)
147
+
if (value !isJsonLiteral)
148
+
throwJsonDecodingException(-1, "Expected string value for a non-null key '$tag', got null literal instead at element: ${renderTagStack(tag)}", currentObject().toString())
149
+
if (!value.isString &&!json.configuration.isLenient) {
150
+
throwJsonDecodingException(
151
+
-1, "String literal for key '$tag' should be quoted at element: ${renderTagStack(tag)}.\n$lenientHint", currentObject().toString()
147
152
)
148
153
}
149
-
if (value isJsonNull) throwJsonDecodingException(-1, "Unexpected 'null' value instead of string literal", currentObject().toString())
0 commit comments