Deserializing a custom class with Gson and Kinvey on Android

563 views Asked by At

I am using the Kinvey Android SDK to retrieve data from their backend. It uses Gson behind the scenes. I have been unable to get it to deserialize an array of custom objects. Here is the entity definition and the JSON in question:

public class AlbumEntity extends GenericJson {
    @Key("_id")
    private String id;

    @Key
    private String name;

    @Key
    private String location_name;

    @Key
    private String start_date;

    @Key("artifacts")
    private Artifact[] artifacts;

    static class Artifact extends GenericJson { 
        @Key
        private String type;
        @Key
        private String photo_url;
        public Artifact() {}
    }

    @Key("_kmd")
    private KinveyMetaData meta; 

    public TripketEntity() {}
}

JSON body:

{
"_id": "5216fec12f7b521a26064f9d",
"_kmd": {
    "ect": "2013-08-26T06:51:00.283Z",
    "lmt": "2013-09-05T15:28:28.079Z"
},
"artifacts": [
    {
        "type": "photo",
        "photo_url": "http://www.califliving.com/title24-energy/images/sanfrancisco.jpg"
    },
    {
        "type": "photo",
        "photo_url": "http://www.sanfrancisco.net/pictures/san-francisco.jpg"
    },
    {
        "type": "photo",
        "photo_url": "http://a2.cdn-hotels.com/images/themedcontent/en_GB/San%20Francisco_Top%2010.jpg"
    },
    {
        "type": "photo",
        "photo_url": "http://sanfranciscoforyou.com/wp-content/uploads/2010/03/san-francisco-city.jpg"
    }
],
"location_name": "San Francisco",
"name": "My Trip to the Bay",
"start_date": "06/15/2013",
"owner": {
    "_type": "KinveyRef",
    "_collection": "user",
    "_id": "5216fcd60b36c57d69000529"
},
"_acl": {
    "creator": "kid_ePPs9jXc_5"
}
}

If I change the private Artifact[] artifacts object to private GenericJson[] artifacts, the array is deserialized as an array of GenericJson objects, with the data fields intact in the Unknown Fields list. How can I get it to work for my custom object?

Other things I have tried include List<Artifact> artifacts as well as ArrayList and Collection.

1

There are 1 answers

0
edthethird On BEST ANSWER

I'm an engineer at Kinvey working on the Android library-

Add a public modifier to the definition for the static inner class, and GSON should be able to pick it up:

...

@Key("artifacts") 
private Artifact[] artifacts;

public static class Artifact extends GenericJson {  
    @Key 
    private String type;
    @Key 
    private String photo_url;
    public Artifact() {} 
} 

@Key("_kmd") 
private KinveyMetaData meta;

...