Kotlin-Objekt-Deserialisierung mit GSON

Habe ich eine Klasse

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

Übergeordneten Klasse aussieht

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

Aber nach der Deserialisierung mit GSON Antworten-Feld ist null.
Kotlin-Objekt-Deserialisierung mit GSON

Ich auch versuchte, um initiation in die init{}, aber es immer noch null, die durch lazy löst eine Ausnahme aus, über den Aufruf faul.getValue auf ein null-Objekt Verweis

  • Zeigen Sie uns die json-bitte.
  • json-Objekte enthalten kein Feld "Antworten". Aber ich habe auch triyed zu verwenden ExclusionStrategy für die Serialisierung und Deserialisierung zu verwenden, schließen Sie diesen Bereich.
  • Eine Randnotiz: Jackson (JSON deser) hat Kotlin Unterstützung für den Umgang mit Kotlin Konstruktoren und use cases über github.com/FasterXML/jackson-module-kotlin ... aus anderen Java-Bibliotheken, die Sie sollten überprüfen, die für ähnliche, da Sie das alles nicht verstehen Konzepte Kotlin, noch nutzen Sie. Sie wird am Ende dabei unnatürlich Kotlin (d.h. die Antwort von @miensol), nur um zu befriedigen Ihrer deser Bibliothek. unfortuneatly GSON ist nicht erweiterbar, so dass es freundlicher zu Kotlin.
InformationsquelleAutor Ufkoku | 2016-09-29
Schreibe einen Kommentar