简体   繁体   English

有更好的方式处理kotlinx序列化吗?

[英]Is there better way for handle kotlinx serialization?

I use kotlinx.serialization on Kotlin native project, I a defined Super class for my models and all of the models extends from it. 我在Kotlin本机项目上使用kotlinx.serialization ,我为我的模型定义了一个Super class ,所有模型都从该模型扩展。 I defined a function to called toJSON() for serialize variables and fields inside model that all of class models have it. 我定义了一个名为toJSON()function ,用于序列化模型中所有类模型都具有的变量和字段。

@Serializable
open class Model {
   fun toJSON(): String = JSON.stringify(this);
}

And I created a subclass 我创建了一个子类

class Me : Model() {
  var name:String = "Jack";
}

but when I invoke JSON.stringify(this) , IDE get a Warning to me: 但是当我调用JSON.stringify(this) ,IDE向我警告:

This declaration is experimental and its usage must be marked with '@kotlinx.serialization.ImplicitReflectionSerializer' or '@UseExperimental(kotlinx.serialization.ImplicitReflectionSerializer::class)' 该声明是实验性的,必须使用“ @ kotlinx.serialization.ImplicitReflectionSerializer”或“ @UseExperimental(kotlinx.serialization.ImplicitReflectionSerializer :: class)”标记。

I paid attention and I used @ImplicitReflectionSerializer annotation while not worked. 我注意了,在@ImplicitReflectionSerializer时使用了@ImplicitReflectionSerializer注释。 Where is my problem? 我的问题在哪里?

This is discussed here . 这在这里讨论。 It's the particular overload you're using which is still experimental. 这是您正在使用的特定过载,仍在试验中。 So your options are either to use the other overload (which takes in a serializer) or to use one of the annotations mentioned in the error message. 因此,您的选择要么是使用其他重载(它需要一个序列化器),要么使用错误消息中提到的注释之一。 If you look at the answer to the question I linked (and the comments following it), you'll see it talks about using @UseExperimental and where it should be used. 如果您查看我链接的问题的答案(及其后的评论),您会看到它谈论使用@UseExperimental以及应在何处使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM