在 kotlinx.serialization 中编码/解码 JSON "string"

Encode / decode JSON "string" in kotlinx.serialization

是否可以在自定义序列化程序中以字符串格式编码/解码任何有效的 json 对象。 例如下面的代码,但不让它序列化为 json 字符串,而是序列化为任何具有未知结构的有效 JSON?

object JsonObjectSerializer : KSerializer<JsonObject> {

    override val descriptor = PrimitiveSerialDescriptor("JsonObject", PrimitiveKind.STRING)

    override fun deserialize(decoder: Decoder): JsonObject =
        JsonObject(decoder.decodeString())

    override fun serialize(encoder: Encoder, value: JsonObject): Unit =
        encoder.encodeString(value.encode())
}

Out 会是这样的..

{
    "some": "data",
    "jsonObject": "{\"this\": \"should not be a string\"}"
}

但是想要的输出是..

{
    "some": "data",
    "jsonObject": {"this": "should not be a string"}
}

encoder.encodeJsonElement可能会如你所愿。

我在 UnknownPolymorphicSerializer<P, W>

的实现中使用 encodeJsonElement

A serializer for polymorph objects of type [P] which wraps extending types unknown at runtime as instances of type [W].

我提取已知结构并包装未知结构。也许与您所追求的用例相似?具体细节和用例相当复杂,但记录在“UnknownPolymorphicSerializer: (De)serializing unknown types”。

@InternalSerializationApi
override fun serialize( encoder: Encoder, value: P )
{
    // This serializer assumes JSON serialization with class discriminator configured for polymorphism.
    // TODO: It should also be possible to support array polymorphism, but that is not a priority now.
    if ( encoder !is JsonEncoder )
    {
        throw unsupportedException
    }
    getClassDiscriminator( encoder.json ) // Throws error in case array polymorphism is used.

    // Get the unknown JSON object.
    check( value is UnknownPolymorphicWrapper )
    val unknown = Json.parseToJsonElement( value.jsonSource ) as JsonObject

    // HACK: Modify kotlinx.serialization internals to ensure the encoder is not in polymorphic mode.
    //  Otherwise, `encoder.encodeJsonElement` encodes type information, but this is already represented in the wrapped unknown object.
    AccessInternals.setField( encoder, "writePolymorphic", false )

    // Output the originally wrapped JSON.
    encoder.encodeJsonElement( unknown )
}

P.s。 AccessInternals 是我的预期实现,能够使用 kotlin reflect,在 JS 上不受支持,因为这是一个多平台库。