gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 06:03:32 25 4
gpt4 key购买 nike

是否可以在自定义序列化程序中以字符串格式编码/解码任何有效的 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 可能会做你想做的事。

我使用 encodeJsonElement我自己在执行 UnknownPolymorphicSerializer<P, W>

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 )
}

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

关于json - 在 kotlinx.serialization 中编码/解码 JSON "string",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66615434/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com