gfhuertac
gfhuertac

Reputation: 366

Retrofit: GSON custom deserialization of children list

I am having problems deserializing a custom object using gson. I have this class:

public class Content implements java.io.Serializable {

    private Long id;
    private int tipo;
    private String title;
    private String content;
    private Integer reward;
    /** Not-null value. */
    private String imageUrl;
    private String bannerUrl;
    private String logoUrl;
    /** Not-null value. */
    private String actionUrl;
    private Integer templateType;
    private String expiresAt;
    private Integer unlockReward;
    private Integer reportType;
    private String completionType;
    private Integer interactionType;
    private Integer productId;
    private Integer views;
    private Boolean saved;
    private Double weight;

    /** Used to resolve relations */
    private transient DaoSession daoSession;

    /** Used for active entity operations. */
    private transient ContentDao myDao;

    private List<Rule> ruleList;
    private List<Category> categoryList;
    ...
    // all getters and setters code
    ...
}

What I want is to deserialize the categoryList using a custom adapter. I created one using the following code:

Type typeOfListOfCategory = new com.google.gson.reflect.TypeToken<List<Category>>(){}.getType();
builder.registerTypeAdapter(typeOfListOfCategory, new JsonDeserializer<List<Category>>() {
    @Override
    public List<Category> deserialize(JsonElement element, Type type,
        JsonDeserializationContext context) throws JsonParseException {
    // my code goes in here
    }
});
Gson gson = builder.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES).create();

And then I used it on a rest adapter

RestAdapter restAdapter = new RestAdapter.Builder()
    .setEndpoint("http://localhost/api")
    .setConverter(new GsonConverter(gson)).build();

But my custom adapter is never called.

The json that I am receiving is like this:

[
    {
        "id": 1,
        "tipo": 1,
        "title": "title",
        "content": "content",
        "reward": 2.0,
        "image_url": "url1",
        "banner_url": "url2",
        "logo_url": "url3",
        "action_url": "url4",
        "template_type": 0,
        "expires_at": "2014-08-31T00:00:00.000Z",
        "unlock_reward": 2,
        "report_type": 0,
        "completion_type": 0,
        "rules": [
            {
                "range": "1",
                "operator": "==",
                "key": "key1"
            }
        ],
        "categories": [
            {
                "description": "description"
            }
        ]
    },
    ...
]

Any ideas?

Upvotes: 1

Views: 2941

Answers (1)

Jimmy
Jimmy

Reputation: 2619

If you are not using annotation to tag the Name of the JSON field, your field name should match the JSON field name. Like list of List<Rule> ruleList in your java class should be List<Rule> rules since the list name in JSON is rules. Cross check all the field names.

This is just my guess because you just said you have problem in deserializing which is very very vague term. If this solution does not work, post your error message by updating your question.

Upvotes: 1

Related Questions