Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gson serialize/deserialize Map to/from list of KeyValuePairs

On the server side I got this API (example) (I can't modify this.)

namespace MyNameSpace
{
    [Serializable][DataContract]
    public class GetMyObject
    {
        [DataMember]
        public Dictionary<int, int> MyDictionary { get; set; }
    }
}

And the server sends this JSON:

{
    "MyDictionary" : 
        [{
            "Key" : 1,
            "Value" : 1
        },
        {
            "Key" : 2,
            "Value" : 2
        },
        {
            "Key" : 3,
            "Value" : 3
        },
        {
            "Key" : 4,
            "Value" : 4
        }]
}

And on the client side, I have to create these classes for correct deserialization:

class GetMyObject {
    @SerializedName("MyDictionary")
    private List<MyDictionaryItem> myDictionary;
}

class MyDictionaryItem {
    @SerializedName("Key")
    private int key;

    @SerializedName("Value")
    private int value;
}

How can I configure GSON to simply use this: (to serialize and deserialize)

class GetMyObject {
    @SerializedName("MyDictionary")
    private Map<Integer, Integer> myDictionary;
}

It even more intresting with complex key object like:

class ComplexKey {
    @SerializedName("Key1")
    private int key1;

    @SerializedName("Key2")
    private String key2;
}

class GetMyObject {
    @SerializedName("MyDictionary")
    private Map<ComplexKey, Integer> myDictionary;
}
like image 617
Gergely Fehérvári Avatar asked Apr 17 '15 09:04

Gergely Fehérvári


2 Answers

Create a custom JsonDeserializer for Map<?, ?>:

public class MyDictionaryConverter implements JsonDeserializer<Map<?, ?>> {
    public Map<Object, Object> deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext ctx) {
        Type[] keyAndValueTypes = $Gson$Types.getMapKeyAndValueTypes(typeOfT, $Gson$Types.getRawType(typeOfT));

        Map<Object, Object> vals = new HashMap<Object, Object>();
        for (JsonElement item : json.getAsJsonArray()) {
            Object key = ctx.deserialize(item.getAsJsonObject().get("Key"), keyAndValueTypes[0]);
            Object value = ctx.deserialize(item.getAsJsonObject().get("Value"), keyAndValueTypes[1]);
            vals.put(key, value);
        }
        return vals;
    }
}

And register it:

gsonBuilder.registerTypeAdapter(new TypeToken<Map>(){}.getType(),
        new MyDictionaryConverter());
like image 88
Egor Neliuba Avatar answered Nov 11 '22 04:11

Egor Neliuba


an alternative, Jackson JSON Processor

@JsonDeserialize(contentAs=Integer.class)
private Map<ComplexKey, Integer> myDictionary;
like image 2
jjcosare Avatar answered Nov 11 '22 04:11

jjcosare