I have the following Json structure and I want to deserialize the Json in to my own class. Can this be done using a custom deserializer or do I have to manually create the object by traversing the JsonTree?
Input Json
{
"20160411": [
{
"name": "John",
"provider": "Some Name",
"published": "2016-04-11 00:00:00 -0400",
"formats": {
"key1": {
"width": 2048,
"height": 651,
"url": "https://some.dummy.url"
},
"key2": {
"width": 2048,
"height": 651,
"url": "https://some.dummy.url"
},
"key3": {
"width": 2048,
"height": 651,
"url": "https://some.dummy.url"
},
"key4": {
"width": 2048,
"height": 651,
"url": "https://some.dummy.url"
}
}
},
{
"name": "John2",
"provider": "Some Name2",
"published": "2016-04-11 00:00:00 -0400",
"formats": {
"key1": {
"width": 2048,
"height": 651,
"url": "https://some.dummy.url"
},
"key2": {
"width": 2048,
"height": 651,
"url": "https://some.dummy.url"
},
"key3": {
"width": 2048,
"height": 651,
"url": "https://some.dummy.url"
},
"key4": {
"width": 2048,
"height": 651,
"url": "https://some.dummy.url"
}
}
}
]
"20160412" : [
{
"name": "John",
"provider": "Some Name",
"published": "2016-04-11 00:00:00 -0400",
"formats": {
"key1": {
"width": 2048,
"height": 651,
"url": "https://some.dummy.url"
},
"key2": {
"width": 2048,
"height": 651,
"url": "https://some.dummy.url"
},
"key3": {
"width": 2048,
"height": 651,
"url": "https://some.dummy.url"
},
"key4": {
"width": 2048,
"height": 651,
"url": "https://some.dummy.url"
}
}
},
{
"name": "John",
"provider": "Some Name",
"published": "2016-04-11 00:00:00 -0400",
"formats": {
"key1": {
"width": 2048,
"height": 651,
"url": "https://some.dummy.url"
},
"key2": {
"width": 2048,
"height": 651,
"url": "https://some.dummy.url"
},
"key3": {
"width": 2048,
"height": 651,
"url": "https://some.dummy.url"
},
"key4": {
"width": 2048,
"height": 651,
"url": "https://some.dummy.url"
}
}
}
]
}
Class Object
Class MyClass{
private String name;
private String provider;
private Date published;
private Map<String, Format> formats;
}
Class Format{
private int width;
private int height;
private String url;
}
While deserializing I am using the following for converting the json string to object
Type type = new TypeToken<Map<Date, List<MyClass>>>(){}.getType();
Map<Date,List<Comic>> resultMap= gson.fromJson(jsonStr, type);
The issue I am facing is that I dont want all the objects corresponding to key2, key3, key4 in the formats map inside MyClass(I just want a single key1). I want MyClass to look like the following.
Desired Class
Class MyClass{
private String name;
private String provider;
private Date published;
private Format format;
}
How can I write custom deserializer for the above?