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;
}
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());
an alternative, Jackson JSON Processor
@JsonDeserialize(contentAs=Integer.class)
private Map<ComplexKey, Integer> myDictionary;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With