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.
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.
Du musst angemeldet sein, um einen Kommentar abzugeben.
Wie beschrieben in diese Antwort Gson kann nicht sicher Griff no-args-Konstruktor Szenarien. Es tut dies durch die Nutzung
UnsafeAllocator
, die wiederum verwendetallocateInstance(Klasse p)
. Dies bedeutet, dass, wenn es keine no-args-Konstruktor und keine eigenen Instanz-Ersteller registriert das Objekt erstelltGson
werden die Felder initialisiert, die mit Ihren deklarierten Typen default-Werte.Zu beheben, die entweder einen no-arg-Konstruktor ich.e
Oder fügen Sie die Standardwerte der parameter auf Ihre primären Konstruktor in etwa so:
Das ist komisch, ich habe gerade verwendet @SerializedName normal, wie in Java, eine Daten-Klasse und es funktionierte: