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

JsonDecoder: decodeSerializableValuePolymorphic - Include currentTag in cast error #2630

Open
hfhbd opened this issue Apr 15, 2024 · 0 comments
Assignees
Labels

Comments

@hfhbd
Copy link
Contributor

hfhbd commented Apr 15, 2024

What is your use-case and why do you need this feature?
JsonDecodingException: Expected class kotlinx.serialization.json.JsonObject (Kotlin reflection is not available) as the serialized body of F, but had class kotlinx.serialization.json.JsonLiteral (Kotlin reflection is not available)

This error message is not very helpful without the property name, but the name is available: currentTag.

To reproduce:

@Serializable
sealed interface F {
  @Serializable
  @SerialName("a")
  data class A(val f: F): F
}

Json.decodeFromString(F.serializer(), """{"type":"a", "f":"wrong-input"}""")

Describe the solution you'd like
JsonDecodingException: Serial name f expected class kotlinx.serialization.json.JsonObject (Kotlin reflection is not available) as the serialized body of F, but had class kotlinx.serialization.json.JsonLiteral (Kotlin reflection is not available)

@hfhbd hfhbd added the feature label Apr 15, 2024
@sandwwraith sandwwraith self-assigned this Apr 17, 2024
sandwwraith added a commit that referenced this issue Apr 22, 2024
…ent.

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
sandwwraith added a commit that referenced this issue May 14, 2024
…ent. (#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 <sergey.shanshin@jetbrains.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants