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

Primitive values cannot be serialized to JsonElement #451

Closed
oxc opened this issue May 7, 2019 · 2 comments
Closed

Primitive values cannot be serialized to JsonElement #451

oxc opened this issue May 7, 2019 · 2 comments

Comments

@oxc
Copy link

oxc commented May 7, 2019

Describe the bug
While Json.plain.stringify can be used on primitives and complex objects alike, Json.plain.toJson fails on primitive objects with an exception:

kotlinx.serialization.SerializationException: No tag in stack for requested element
	at kotlinx.serialization.TaggedEncoder.popTag(Tagged.kt:160)
	at kotlinx.serialization.TaggedEncoder.encodeBoolean(Tagged.kt:95)
	at kotlinx.serialization.internal.BooleanSerializer.serialize(Primitives.kt:54)
	at kotlinx.serialization.internal.BooleanSerializer.serialize(Primitives.kt:51)
	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)

To Reproduce
Run (e.g. ./gradlew run) the attached demo project from
primitive-serialization.zip

Expected behavior
I would expect both Strings and Booleans to be serialized to JsonPrimitive instances.

Environment

  • Kotlin version: 1.3.31
  • Library version: 0.11.0
  • Kotlin platforms: JVM
  • Gradle version: 4.10.3
@yshrsmz
Copy link

yshrsmz commented May 7, 2019

Kotlin: 1.3.31
Serialization: 0.11.0
Platform: JVM
Gradle: 5.1.1

I'm having similar issue.
In my case, Json.nonstrict.toJson with NullableSerializer or EnumSerializer fails with similar exception.

for NullableSerializer:

@Serializable
data class UserProfile(val id:String)

fun test() {
  val jsonElement = Json.nonstrict.toJson<UserProfile?>(NullableSerializer(UserProfile.serializer()), null)
  println("jsonElement: $jsonElement")
}
 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)

for EnumSerializer

enum class CollaboratorAffiliation {
    OUTSIDE, DIRECT, ALL
}

fun test() {
  val el = Json.nonstrict.toJson(EnumSerializer(CollaboratorAffiliation::class), CollaboratorAffiliation.ALL)
  println("el: $el")
}
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)

@oxc
Copy link
Author

oxc commented May 7, 2019

Ok, so this seems to apply to all cases where the value is not serialized to a json object (or array, probably), including null.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants