How a Deleted Gson Annotation Silently Zeroes Out All Your Data
A common Gson pitfall in Android development causes integer fields to silently default to zero when the @SerializedName annotation is removed from a Kotlin data class. Without the annotation, Gson cannot match the camelCase Kotlin property name 'baseStat' to the snake_case JSON key 'base_stat', so it leaves the field at its default value of 0 without throwing any error or warning. Gson treats missing JSON keys as acceptable, filling unmatched fields with type defaults such as 0 for integers, false for booleans, and null for objects. The library only throws exceptions for malformed JSON or type mismatches, not for absent keys. Developers are advised to always retain @SerializedName annotations when Kotlin property names differ from their corresponding JSON keys, rather than treating them as redundant code.
This is an AI-generated summary. ShortSingh links to the original source for the complete article.
Discussion (0)
Log in to join the discussion and vote.
Log in