Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gson doesn't give any "feedback" #1664

Closed
Nathipha opened this issue Mar 30, 2020 · 2 comments
Closed

Gson doesn't give any "feedback" #1664

Nathipha opened this issue Mar 30, 2020 · 2 comments

Comments

@Nathipha
Copy link

Nathipha commented Mar 30, 2020

I'm using Gson 2.8.6. to map a JSON text to multiple Java objects that my app can then work with easily:

Gson gson = new Gson();
Person p = gson.fromJson(somePersonText, Person.class);

Example for the JSON text (the actual text is more complicated, including an array):

{
"firstname" : "John",
"lastname" : "Smith",
"age" : 23
}

and the Person class:

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.

There's an answer about this here:

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?

@LorenzNickel
Copy link
Contributor

From my understanding the thing with the automatic conversion between types can't be disabled or connected with a warning. We have automatic conversion String -> Boolean, String -> Number, Number -> String, Boolean -> String, Number, String -> Double, String -> BigDecimal, String -> BigInteger, Number, String -> Float, Number, String -> Long, Number, String -> Short, Number, String -> Integer, Number, String -> Byte and finally String -> Character. These are converted without any warnings or something like this.

@Marcono1234
Copy link
Collaborator

@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.

@Marcono1234 Marcono1234 closed this as not planned Won't fix, can't repro, duplicate, stale Jul 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants