gpt4 book ai didi

kotlin - 使用 gson 从序列化/反序列化中排除 Kotlin 数据类属性/字段

转载 作者:行者123 更新时间:2023-12-02 12:11:01 62 4
gpt4 key购买 nike

我正在尝试使用 gson 从反序列化中排除 Kotlin 属性。我尝试了不同的方法来注释属性 @Transient创建自定义注释策略(当然在 gson 构建器中指定策略),但似乎没有任何效果,因为该属性一直为 null 而不是我初始化该属性的值。

我没有尝试使用 @Expose注释,但我不想用 @Expose 注释其他字段

拜托,我怎样才能使用 gson + Kotlin 实现这一目标?

最佳答案

@Transient 为我工作。

@Transient lateinit var bar: SomeCustomType

根据@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.

关于kotlin - 使用 gson 从序列化/反序列化中排除 Kotlin 数据类属性/字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53425600/

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