使用GSON进行Kotlin对象反序列化

时间:2016-09-29 21:06:05

标签: gson kotlin

我有一个班级

class ThreadComment(
    banned: Int,
    closed: Int,
    comment: String?,
    date: String?,
    email: String?,
    files: ArrayList<File>?,
    lasthit: Int,
    name: String?,
    num: String?,
    op: Int,
    parent: String?,
    postsCount: Int,
    sticky: Int,
    subject: String?,
    tags: String?,
    timestamp: Int,
    trip: String?) : Comment(banned, closed, comment, date, email, files, lasthit, name, num, op, parent, postsCount, sticky, subject, tags, timestamp, trip) {

val answers: ArrayList<String> = ArrayList()}

父类看起来像

open class Comment(
    @com.google.gson.annotations.SerializedName("banned")
    val banned: Int = 0,

    @com.google.gson.annotations.SerializedName("closed")
    val closed: Int = 0,

    @com.google.gson.annotations.SerializedName("comment")
    val comment: String? = null,

    @com.google.gson.annotations.SerializedName("date")
    val date: String? = null,

    @com.google.gson.annotations.SerializedName("email")
    val email: String? = null,

    @com.google.gson.annotations.SerializedName("files")
    val files: ArrayList<File>? = null,

    @com.google.gson.annotations.SerializedName("lasthit")
    val lasthit: Int = 0,

    @com.google.gson.annotations.SerializedName("name")
    val name: String? = null,

    @com.google.gson.annotations.SerializedName("num")
    val num: String? = null,

    @com.google.gson.annotations.SerializedName("op")
    val op: Int = 0,

    @com.google.gson.annotations.SerializedName("parent")
    val parent: String? = null,

    @com.google.gson.annotations.SerializedName("posts_count")
    val postsCount: Int = 0,

    @com.google.gson.annotations.SerializedName("sticky")
    val sticky: Int = 0,

    @com.google.gson.annotations.SerializedName("subject")
    val subject: String? = null,

    @com.google.gson.annotations.SerializedName("tags")
    val tags: String? = null,

    @com.google.gson.annotations.SerializedName("timestamp")
    val timestamp: Int = 0,

    @com.google.gson.annotations.SerializedName("trip")
    val trip: String? = null) : Serializable

但是在使用GSON answer字段进行反序列化后,该字段为空。 Debugger

我也尝试将init放入init {},但它仍为null,懒惰抛出了一个关于在null对象引用上调用lazy.getValue的异常

2 个答案:

答案 0 :(得分:9)

这很奇怪,我只是在Java中使用了@SerializedName和一个数据类,它起作用了:

data class(@SerializedName("my_string_value") val myStringValue: String, @SerializedName("my_int_value") val myIntValue: Int)

答案 1 :(得分:7)

this answer Gson中所述,不安全地处理no-args构造函数方案。它通过利用UnsafeAllocator反过来使用allocateInstanсe(Class p)来实现这一目的。这意味着当没有no-args构造函数并且没有注册自定义instance creator时,Gson 创建的对象将使用其声明的类型默认值初始化字段。< / p>

要解决这个问题,要么添加一个no-arg构造函数,即

private constructor() : this(-1, -1, "", "", "", null, -1, "", "", -1, null, -1, -1, null, null, -1, null)

或者将默认参数值添加到主构造函数中,如下所示:

class ThreadComment(
        banned: Int = -1,
        closed: Int = -1,
        comment: String? = null,
        date: String? = null,
        email: String? = null,
        files: ArrayList<File>? = null,
        lasthit: Int = -1,
        name: String? = null,
        num: String? = null,
        op: Int = -1,
        parent: String? = null,
        postsCount: Int = -1,
        sticky: Int = -1,
        subject: String? = null,
        tags: String? = null,
        timestamp: Int = -1,
        trip: String? = null
) : Comment(banned, closed, comment, date, email, files, lasthit, name, num, op, parent, postsCount, sticky, subject, tags, timestamp, trip) {
    val answers: ArrayList<String> = ArrayList()
}