Skip to content

encodeToJsonElement on an inline class throws "No tag in stack for requested element" #1774

@bartvanheukelom

Description

@bartvanheukelom

Describe the bug

@Serializable @JvmInline
value class Name(val v: String)

Encoding a value of that class to a JSON string works as expected, but encodeToJsonElement throws the exception in the title.

This only occurs when serializing the value directly. It works as expected when embedded in a data class.

It can be worked around by creating a custom serializer that effectively does the same thing as the generated one would.

To Reproduce
For a unit test that reproduces it and shows various workarounds that do work, see https://github.com/bartvanheukelom/jbali/blob/java17/src/jvmTest/java/org/jbali/kotser/TaggedEncoderBugTest.kt

Excepted Behaviour

  1. For it to, well, work :D
  2. Generally speaking, I've seen various situations over the years where encoding something to a string worked but to an element didn't. This surprises me, since I would expect these 2 encoders to have like 80% of their implementation in common, where the differences in the 20% wouldn't cause bugs such as these. But that is just an outsider's perspective, so please correct me if I'm wrong! I'm not complaining, this is after all a great and free library, but I am curious what's up.

Environment

  • Kotlin version: 1.6.0
  • Library version: 1.3.0 & 1.3.1
  • Kotlin platforms: JVM
  • Gradle version: 7.2

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions