2016-09-29 23 views
5

Mam klasyKotlin obiekt deserializacji z 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()} 

klasa nadrzędna wygląda

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 

Ale po deserializacji z GSON odpowiada pole jest puste. Debugger

że próbował również umieścić inicjację init, {}, ale jeszcze zerowa, przy leniwa zgłasza wyjątek o wywoływanie lazy.getValue o zerowym odniesienia do obiektu

+0

Pokaż nam json proszę. – miensol

+0

@miensol Obiekty json nie zawierają pól "odpowiedzi". Ale również próbowałem użyć ExclusionStrategy do serializacji i deserializacji, aby wykluczyć to pole. – Ufkoku

+0

Uwaga dodatkowa: Jackson (JSON deser) ma wsparcie Kotlin do radzenia sobie z konstruktorami Kotlin i przypadków użycia przez https://github.com/FasterXML/jackson-module-kotlin ... dla innych bibliotek Java powinieneś sprawdzić podobne, ponieważ nie rozumieją wszystkich koncepcji Kotlin ani nie wykorzystują ich. W efekcie będziesz wykonywał nienaturalny kotlin (czyli odpowiedź od @miensol) tylko po to, by zadowolić twoją bibliotekę. niefortunnie GSON nie jest rozszerzalny w sposób, który pozwala mu być bardziej przyjaznym dla Kotlin. –

Odpowiedz

4

Jak opisano this answerGson can niebezpiecznym obsłużyć scenariusze konstruktorów no-args. Czyni to poprzez wykorzystanie UnsafeAllocator, która z kolei używa allocateInstanсe(Class p). Oznacza to, że gdy nie ma konstruktora no-args i nie ma niestandardowego zarejestrowanego obiektu, obiekt utworzony przez Gsonbędzie miał zainicjalizowane pola z zadeklarowanymi domyślnymi typami wartości.

Aby rozwiązać dodać, że albo nie-Arg konstruktor tj

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

Albo dodać wartości parametrów domyślnych do swojej pierwotnej konstruktora tak:

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 dziwne, po prostu stosować @ SerializedName normalnie jak w Javie z klasą danych i zadziałało:

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