使用gson从序列化/反序列化中排除Kotlin数据类属性/字段 [英] Exclude a Kotlin data class property/field from serialization/deserialization using gson
问题描述
我正在尝试使用gson将Kotlin属性排除在反序列化之外.我尝试了不同的方法,从用@Transient
注释属性到创建自定义注释策略(当然是在gson生成器中指定策略),但是似乎没有任何作用,因为该属性不断获得null而不是我初始化的值财产.
I am trying to exclude a Kotlin property from deserialization using gson. I have tried different methods from annotating the property with @Transient
to creating a custom annotation strategy (specifying the strategy in the gson builder of course), but nothing seems to be working, as the property keeps getting null instead of the value I initialized the property with.
我没有尝试使用@Expose
注释,但是我不想使用@Expose
I have not tried using the @Expose
annotation, but I do not want to annotate other fields with @Expose
请,我如何使用gson + Kotlin来实现这一目标?
Please, how can I achieve this please using gson + Kotlin?
推荐答案
@Transient为我工作.
@Transient worked for me.
@Transient lateinit var bar: SomeCustomType
每个@Transient定义:
Per @Transient definition:
将带注释的属性的JVM支持字段标记为
transient
, 表示它不是该对象的默认序列化形式的一部分.
Marks the JVM backing field of the annotated property as
transient
, meaning that it is not part of the default serialized form of the object.
这篇关于使用gson从序列化/反序列化中排除Kotlin数据类属性/字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!