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

Properly handle top-level value classes in encodeToJsonElement #1777

Merged
merged 2 commits into from
Dec 15, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
/*
* Copyright 2017-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
* Copyright 2017-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
*/
@file:OptIn(ExperimentalSerializationApi::class)

package kotlinx.serialization.json.internal

import kotlinx.serialization.*
import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.SerializationStrategy
import kotlinx.serialization.descriptors.*
import kotlinx.serialization.encoding.*
import kotlinx.serialization.internal.*
import kotlinx.serialization.internal.NamedValueEncoder
import kotlinx.serialization.json.*
import kotlinx.serialization.modules.*
import kotlinx.serialization.modules.SerializersModule
import kotlin.collections.set
import kotlin.jvm.*
import kotlin.jvm.JvmField

internal fun <T> Json.writeJson(value: T, serializer: SerializationStrategy<T>): JsonElement {
lateinit var result: JsonElement
Expand Down Expand Up @@ -69,7 +70,7 @@ private sealed class AbstractJsonTreeEncoder(

override fun <T> encodeSerializableValue(serializer: SerializationStrategy<T>, value: T) {
// Writing non-structured data (i.e. primitives) on top-level (e.g. without any tag) requires special output
if (currentTagOrNull != null || serializer.descriptor.kind !is PrimitiveKind && serializer.descriptor.kind !== SerialKind.ENUM) {
if (currentTagOrNull != null || !serializer.descriptor.needTopLevelTag) {
sandwwraith marked this conversation as resolved.
Show resolved Hide resolved
encodePolymorphically(serializer, value) { polymorphicDiscriminator = it }
} else JsonPrimitiveEncoder(json, nodeConsumer).apply {
encodeSerializableValue(serializer, value)
Expand Down Expand Up @@ -139,6 +140,13 @@ private sealed class AbstractJsonTreeEncoder(
}
}

private val SerialDescriptor.needTopLevelTag: Boolean
sandwwraith marked this conversation as resolved.
Show resolved Hide resolved
get() {
if (kind is PrimitiveKind || kind === SerialKind.ENUM) return true
if (isInline) return getElementDescriptor(0).needTopLevelTag
return false
}

internal const val PRIMITIVE_TAG = "primitive" // also used in JsonPrimitiveInput

private class JsonPrimitiveEncoder(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,46 @@ value class ResourceId(val id: String)
value class ResourceType(val type: String)

@Serializable
data class ResourceIdentifier(val id: ResourceId, val type: ResourceType)
@JvmInline
value class ResourceKind(val kind: SampleEnum)

@Serializable
data class ResourceIdentifier(val id: ResourceId, val type: ResourceType, val type2: ValueWrapper)

@Serializable @JvmInline
value class ValueWrapper(val wrapped: ResourceType)

class InlineClassesTest : JsonTestBase() {
private val precedent: UInt = Int.MAX_VALUE.toUInt() + 10.toUInt()

@Test
fun testTopLevel() = noLegacyJs {
assertJsonFormAndRestored(
ResourceType.serializer(),
ResourceType("foo"),
""""foo"""",
)
}

@Test
fun testTopLevelOverEnum() = noLegacyJs {
assertJsonFormAndRestored(
ResourceKind.serializer(),
ResourceKind(SampleEnum.OptionC),
""""OptionC"""",
)
}

@Test
fun testTopLevelWrapper() = noLegacyJs {
assertJsonFormAndRestored(
ValueWrapper.serializer(),
ValueWrapper(ResourceType("foo")),
""""foo"""",
)
}


@Test
fun testSimpleContainer() = noLegacyJs {
assertJsonFormAndRestored(
Expand Down Expand Up @@ -106,8 +141,8 @@ class InlineClassesTest : JsonTestBase() {
fun testInlineClassesWithStrings() = noLegacyJs {
assertJsonFormAndRestored(
ResourceIdentifier.serializer(),
ResourceIdentifier(ResourceId("resId"), ResourceType("resType")),
"""{"id":"resId","type":"resType"}"""
ResourceIdentifier(ResourceId("resId"), ResourceType("resType"), ValueWrapper(ResourceType("wrappedType"))),
"""{"id":"resId","type":"resType","type2":"wrappedType"}"""
)
}

Expand Down