Question

I am trying to deserialize a class with Gson.

This is the class I am trying to deserialize :

public class Product {

    public String id;
    public String name;
    public String expDate;
    public String iconPath;

    public Product(final String id, final String name, final String expDate, final String iconPath) {
        this.id = id;
        this.name = name;
        this.expDate = expDate;
        this.iconPath = iconPath;
    }

    @Override
    public final String toString() {
        return new Gson().toJson(this);
    }

}

And this is the code used :

final String serializedProducts = PreferenceManager.getDefaultSharedPreferences(activity).getString("products_" + sectionNumber, null);
if(serializedProducts != null) {
    final Gson gson = new GsonBuilder().create();
    final List<?> deserializedProducts = gson.fromJson(serializedProducts, List.class);
    final List<Product> result = new ArrayList<Product>();
    for(final Object product : deserializedProducts) {
        //System.out.println(product.toString());
        result.add(gson.fromJson(product.toString(), Product.class));
    }
}

And this is the output of System.out.println(...) :

{expDate=4/6/2014, iconPath=/data/data/fr.skyost.fridge/files/images/5000112558265, id=5000112558265, name=Coca-Cola Zéro}

{expDate=4/6/2014, iconPath=/data/data/fr.skyost.fridge/files/images/5000112558265, id=5000112558265, name=Coca-Cola Zéro}

{expDate=4/6/2014, iconPath=/data/data/fr.skyost.fridge/files/images/3564700371107, id=3564700371107, name=Eau minérale}

I am having an exception MalformedJsonException at line 1, column 12 (the slash).

So how can I proceed to deserialize it ?

Thanks ;)

Was it helpful?

Solution

JSON requires string values to be quoted (including the keywords). And the name/value separator for JSON is a colon, not equals.

So to be valid JSON the text should be:

{"expDate":"4/6/2014", "iconPath":"/data/.../5000112558265", "id":5000112558265, "name":"Coca-Cola Zéro"}

Refer to http://json.org for full details.

I just noticed that your toString implementation uses GSON to produce ostensibly JSON output, but either that toString method is not being used, or GSON has a very mistaken understanding of what constitutes valid JSON. My expectation is that it's surely the former.

I now see the problem, I think, now that I look closer; whatever they are your deserialized objects are not instances of Product. So the following code:

for(final Object product : deserializedProducts) {
    //System.out.println(product.toString());
    result.add(gson.fromJson(product.toString(), Product.class));

does not invoke Product.toString, but the toString of whatever object gson.fromJson(serializedProducts, List.class); produces and adds to the list.

You should add product.getClass() to your debug output to prove this. And then you should should investigate how to get actual instances of Product created.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top