Why GSON fails to convert Object when it's a field of another Object? [duplicate]
问题 This question already has answers here : Gson doesn't serialize fields defined in subclasses (4 answers) Closed yesterday . GSON fails to convert Errorneous to JSON properly when it's inside of other Object . But it works well when it's converted as a top level object. Why, and how to fix it? Example: import com.google.gson.GsonBuilder sealed class Errorneous<R> {} data class Success<R>(val result: R) : Errorneous<R>() data class Fail<R>(val error: String) : Errorneous<R>() class Container