2016-09-29 55 views
5

科特林對象反序列化我有一個類與GSON

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反序列化後回答字段爲空。 Debugger

我還試圖把起始到INIT {},但它仍然爲空,由懶惰拋出異常有關調用lazy.getValue上的空對象引用

+0

請向我們展示json。 – miensol

+0

@miensol json對象不包含字段「答案」。但我也試圖使用ExclusionStrategy進行序列化和反序列化來排除此字段。 – Ufkoku

+0

附註:Jackson(JSON deser)通過https://github.com/FasterXML/jackson-module-kotlin處理Kotlin構造函數和用例的Kotlin支持...對於其他Java庫,您應該檢查類似的因爲他們不瞭解Kotlin的所有概念,也沒有充分利用它們。你最終會做出不自然的Kotlin(即@miensol的答案),以滿足你的需求庫。不幸的是,GSON不可擴展,使其對Kotlin更友善。 –

回答

4

this answerGson可以不安全描述處理無參數構造方案。它通過利用UnsafeAllocator來實現,後者又使用allocateInstanсe(Class p)。這意味着,如果沒有無參數構造函數並且沒有自定義instance creator註冊,則由Gson創建的對象將使用聲明的類型默認值來初始化字段。

要解決,要麼添加一個無參數的構造即

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

That's奇怪,我只是用@ SerializedName通常在Java中使用數據類,它的工作原理如下:

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