0

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?

ajb
  • 31,309
  • 3
  • 58
  • 84
sanath01
  • 203
  • 1
  • 2
  • 7
  • 2
    Possible duplicate of [How do I write a custom JSON deserializer for Gson?](http://stackoverflow.com/questions/6096940/how-do-i-write-a-custom-json-deserializer-for-gson) or http://stackoverflow.com/questions/16590377/custom-json-deserializer-using-gson –  Apr 14 '16 at 05:03
  • see also the [fancy manual](https://sites.google.com/site/gson/gson-user-guide#TOC-Custom-Serialization-and-Deserialization) –  Apr 14 '16 at 05:04

0 Answers0