I have a strange problem. ```@Parcelize data class Post( @SerializedName("topic_list") val topics: M...
h
I have a strange problem.
Copy code
@Parcelize
data class Post(
@SerializedName("topic_list")
val topics: MutableList<Topic> = mutableListOf(),
@SerializedName("tag_list")
val tagList: MutableList<PostTag> = mutableListOf()
)
I build a android project with the kotlin-parcelize, if json with out topic_list/tag_list, I think it will be the default value which two empty list. but in last week. it’s generate by kapt without default value. after decompile:
Copy code
@SerializedName("media_list")
    @NotNull
    private List<Media> mediaList;
Why….😅
1
t
So you mention JSON and Parcelize together, and I’m getting confused. I didn’t think those two were related.
So set me straight here - we are definitely talking about Parcels and not json right?
h
yeah, now the data class post will failed when parcel write the topic list, because it generated with @NotNull but with init value. I want to get the real reason.
sorry, i have decompile the diff version. the parcelize generate the same code. It’s may be the gson config problem. I’ll find out why.
t
I guess its possible that if Parcelize was giving you default values, that would affect Gson!
h
In the weekend, i have find the reason. the gson library use an unsafe operator to create a obj. some related: https://github.com/google/gson/issues/1657 https://github.com/google/gson/issues/1550 https://github.com/google/gson/issues/1444 in consideration of the kotlin null/no-null is a very clear define. the gson is a bad impl. It took me an hour to switch to moshi. kotln frindly is important.
A parameterless constructor must be provided. If not, the gson will use a unsafe new instance api.
// is OK
data class A ( @SerializedName(“a”) val a:String=“”, @SerializedName(“b”) val b:Int=0 ) // is Failed data class A ( @SerializedName(“a”) val a:String, @SerializedName(“b”) val b:Int=0 )