javaee-esprit / google-api-java-client

Automatically exported from code.google.com/p/google-api-java-client
0 stars 0 forks source link

Error parsing a json response #700

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Version of google-api-java-client (e.g. 1.5.0-beta)?
google-oauth-client-java7 -> 1.13.1-beta
google-http-client-gson -> 1.13.1-beta

Java environment (e.g. Java 6, Android 2.3, App Engine)?
Java 7u10

Describe the problem.
i'm playing around with Dropbox Api, and i'm getting a json response like this 
one

{
    "referral_link": "https://www.dropbox.com/referrals/r1a2n3d4m5s6t7",
    "display_name": "John P. User",
    "uid": 12345678,
    "country": "US",
    "quota_info": {
        "shared": 253738410565,
        "quota": 107374182400000,
        "normal": 680031877871
    }
}

no problem sending/singing the req or getting the input stream from the 
response, is when i try to parse the response using something like this

DropboxAccountInfo accountInfo = response.parseAs(DropboxAccountInfo.class);

it actually does return a new instance of the DropboxAccountInfo class but all 
the field inside of it are null.

this is my DropboxAccountInfo class:

public class DropboxAccountInfo {

    private String uid;
    private String display_name;
    private String country;
    private String referral_link;
    private DropboxQuota quota_info;

       //Getters and setters not who do not necessary match with the variables names

    private class DropboxQuota {

        private long quota;
        private long normal;
        private long shared;

       //Getters and setters not who do not necessary match with the variables names
    }

now, i'm using a GsonObjectParser and it's working fine, the problem seems to 
be on the ClassInfo.class when the constructor is running, the 
nameToFieldInfoMap map is never filled up and i think to that's the root of the 
entire problem.

this map is not filled up because on the line 174 FieldInfo fieldInfo = 
FieldInfo.of(field); is always getting null, and this is because inside of the 
"of" method the variable field is not an enum and then check for the @Key 
annotation line (93), and it doesn't have that one cuz... just don't really 
need to, anyways, i don't see why if it doesn't contain the @key annotation 
this method should return null. 

i hope have explained me well enough

How would you expect it to be fixed?
The parseAs() method should return a new instance of the class but with all the 
values populated according to the json

IN ADVANCE THANKS SO MUCH AND SORRY FOR MY BAD ENGLISH, if you need more 
information (or want to pair to track this down or whatever) please do not no 
hesitate on contact me (lcc.julio@gmail.com) 

Original issue reported on code.google.com by lcc.ju...@gmail.com on 7 Jan 2013 at 5:21

GoogleCodeExporter commented 9 years ago
By design: unlike GSON, you need to explicitly specify the @Key annotation on 
every field that you want to parse.  For example:

public class DropboxAccountInfo {

    @Key private String uid;
    @Key private String display_name;
    @Key private String country;
    @Key private String referral_link;
    @Key private DropboxQuota quota_info;

       //Getters and setters not who do not necessary match with the variables names

    private class DropboxQuota {

        @Key private long quota;
        @Key private long normal;
        @Key private long shared;

       //Getters and setters not who do not necessary match with the variables names
    }

Original comment by yan...@google.com on 24 Jan 2013 at 2:38