``` @Serializable data class UserProfile(val id:St...
# serialization
y
Copy code
@Serializable
data class UserProfile(val id:String)

fun test() {
  val jsonElement = Json.nonstrict.toJson<UserProfile?>(NullableSerializer(UserProfile.serializer()), null)
  println("jsonElement: $jsonElement")
}
Above code throws following error in kotlin 1.3.31 x serialization 1.1.0.
Copy code
Caused by: kotlinx.serialization.SerializationException: No tag in stack for requested element
        at kotlinx.serialization.TaggedEncoder.popTag(Tagged.kt:160)
        at kotlinx.serialization.TaggedEncoder.encodeNull(Tagged.kt:92)
        at kotlinx.serialization.internal.NullableSerializer.serialize(NullableSerializer.kt:39)
        at kotlinx.serialization.json.internal.AbstractJsonTreeOutput.encodeSerializableValue(TreeJsonOutput.kt:174)
        at kotlinx.serialization.CoreKt.encode(Core.kt:73)
        at kotlinx.serialization.json.internal.TreeJsonOutputKt.writeJson(TreeJsonOutput.kt:29)
        at kotlinx.serialization.json.Json.toJson(Json.kt:129)
has anyone experienced this issue?
Copy code
enum class CollaboratorAffiliation {
    OUTSIDE, DIRECT, ALL
}

fun test() {
  val el = Json.nonstrict.toJson(EnumSerializer(CollaboratorAffiliation::class), CollaboratorAffiliation.ALL)
  println("el: $el")
}
toJson with EnumSerializer also throws similar exception
Copy code
Caused by: kotlinx.serialization.SerializationException: No tag in stack for requested element
        at kotlinx.serialization.TaggedEncoder.popTag(Tagged.kt:160)
        at kotlinx.serialization.TaggedEncoder.encodeEnum(Tagged.kt:108)
        at kotlinx.serialization.internal.CommonEnumSerializer.serialize(Enums.kt:60)
        at kotlinx.serialization.json.internal.AbstractJsonTreeOutput.encodeSerializableValue(TreeJsonOutput.kt:174)
        at kotlinx.serialization.CoreKt.encode(Core.kt:73)
        at kotlinx.serialization.json.internal.TreeJsonOutputKt.writeJson(TreeJsonOutput.kt:29)
        at kotlinx.serialization.json.Json.toJson(Json.kt:129)
s
Please, file a bug to GitHub issues. It seems that this is a limitation of
toJson
function: it expects an object, so serializing
null
is impossible for now
y
@sandwwraith already filed. please take a look https://github.com/Kotlin/kotlinx.serialization/issues/451
🙏 1