Kotlin data class Gson serialization issue

Viewed 2248

Im a little bit confused for my kotlin class didn't work as expected:

the data class used to check update info:

data class UpdateInfo constructor(//kotlin class
    val description: String,
    val force: Int,
    val platform: String,
    val title: String,
    val url: String,
    @SerializedName("version")
    val versionCode: Int = 0
) : Serializable {
    val isForceUpdate = force == 1
}

also a util used to decode object form json:

public class JsonUtil {//java class
    private static final Gson gson;
    static {
        BooleanAdapter booleanAdapter = new BooleanAdapter();
        gson = new GsonBuilder()
            .serializeNulls()
            .disableHtmlEscaping()
            .setLenient()
            .registerTypeAdapter(Boolean.class, booleanAdapter)
            .registerTypeAdapter(boolean.class, booleanAdapter)
            .create();
    }
}

when I test it:

val json = "{\"force\"=\"1\"}"
val info = JsonUtil.fromJsonObject(json, UpdateInfo::class.java)
println(info)
println(info.force == 1)
println(info.isForceUpdate)

I get:

UpdateInfo(description=null, force=1, platform=null, title=null, url=null,versionCode=0)
true
false

What?? info.isForceUpdate = false ???
Then I tried lateinit or by lazy{}, still not works. So, what should I do..I'm useing info.force==1 directly now,but I still want to know why this happend.

1 Answers
Related