Validate class's non-null property after Gson's deserialization.
Use NonNullValidate
to annotate your class:
@NonNullValidate
data class Repo(val name: String,
val description: String)
Name of your TypeAdapterFactory:
kapt {
arguments {
arg("nonNullValidator.factoryName", "example.GsonNonNullValidator")
}
}
val gson = GsonBuilder().registerTypeAdapterFactory(GsonNonNullValidator()).create()
// this will throw JsonSyntaxException, since `description` is absent
val repo = gson.fromJson("""{"name":"NonNullValidate"}""", Repo::class.java)
TODO
TODO
MIT