You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
public class Person {
public String firstname;
public String lastname;
public int age;
public Person(String f,String l,int a) {
firstname = f;
lastname = l;
age = a;
}
}
According to Gson's javadoc problems doing that should throw an exception:
JsonSyntaxException - if json is not a valid representation for an
object of type classOfT
But if I give it e.g. "age" : "23", Gson doesn't throw an exception, instead it silently converts "23" to an int.
If there's a typo, e.g. "firstnam" : "John", firstname ends up as null (because it can't map "firstnam" to "firstname" of course) but there's still no exception, which later on causes problems when working with the Person.
The JSON that you're giving to GSON is 100% completely valid. There are no syntax errors in it. So that's why you're not getting a JsonSyntaxException.
Though it will return a User [Person in my case] object with completely default values, since it can't map any of the fields from the input JSON to the User object fields.
This doesn't address how to deal with this though.
How do I make Gson give me some type of feedback after mapping/parsing? Is there any way to ask Gson for a "report" (and if it's just a simple "error" boolean), so I can be sure that it didn't silently convert between types or even fail to map?
The text was updated successfully, but these errors were encountered:
@LorenzNickel, I think those conversions would only apply if Gson methods which work with JsonElement and subclasses are used.
For the example code in this issue, Gson's internal TypeAdapters.INTEGER is probably used. That adapter then calls JsonReader#nextInt which intentionally also supports the case where the number is stored as JSON string. Maybe that was done to account for other programming languages which cannot precisely represent all numbers as number type, but only as string.
Regarding the handling of property names with typo, the underlying issue is that Gson ignores unknown (#188) and missing (#1005) properties.
Since this GitHub issue here includes multiple issues in one, and since there already existing GitHub issues I am going to close this one here.
I'm using Gson 2.8.6. to map a JSON text to multiple Java objects that my app can then work with easily:
Example for the JSON text (the actual text is more complicated, including an array):
and the
Person
class:According to Gson's javadoc problems doing that should throw an exception:
But if I give it e.g.
"age" : "23"
, Gson doesn't throw an exception, instead it silently converts"23"
to anint
.If there's a typo, e.g.
"firstnam" : "John"
,firstname
ends up asnull
(because it can't map "firstnam" to "firstname" of course) but there's still no exception, which later on causes problems when working with thePerson
.There's an answer about this here:
This doesn't address how to deal with this though.
How do I make Gson give me some type of feedback after mapping/parsing? Is there any way to ask Gson for a "report" (and if it's just a simple "error" boolean), so I can be sure that it didn't silently convert between types or even fail to map?
The text was updated successfully, but these errors were encountered: