1

I have a very unique use case where I have some JSON data incoming from a third party which I can't modify.

{      
    "Information": {
        "mobile": {
            "name": "Mobile Application",
            "classType" : "A"
        },
        "desktop": {
            "name": "Desktop Application",
            "classType" : "B"
        },
        "tablet": {
            "name": "Tablet Application",
            "classType" : "A"
        }
   }

}

Next, there are multiple components that use this data. Rather than providing the entire JSON, I want to provide these components Java Objects that are easier to work with. So, I create JAVA objects from this JSON using jsonschema2pojo tool.

Now, the problem is that when I use this library, it creates three different Java files for mobile, desktop and tablet(and it is right in doing so) but the components using this information would want a generic class rather than handling differently for mobile desktop and tablet. Also, since more devices can be added and there are just a lot of components I don't wanna keep specific classes. I wish the 3P provided a generic 'device' which had field deviceType that could store mobile/desktop or tablet info but that's gonna take a lot of time. Meanwhile, any solution you can think of ?

  • Json String to Java Object with dynamic key name https://stackoverflow.com/questions/39159770/json-string-to-java-object-with-dynamic-key-name – resource8218 Oct 18 '17 at 23:40

1 Answers1

0

Create JSON schemas rather than JSON to generate classes and use 'javaType' to generate a generic class.

Check the schema corresponding to the above JSON :

{
    "type":"object",
    "properties": {
        "Information": {
            "type": "object",
            "properties": {
               "mobile": {
                   "type": "object",
                   "javaType":"Devices",
                   "properties": {
                       "name" : {
                           "type": "string"
                       },
                       "classType" : {
                           "type": "string"
                       }
                   }
                },
                "tablet": {
                   "type": "object",
                   "javaType":"Devices",
                   "properties": {
                       "name" : {
                           "type": "string"
                       },
                       "classType" : {
                           "type": "string"
                       }
                   }
                },
                "desktop": {
                   "type": "object",
                   "javaType":"Devices",
                   "properties": {
                       "name" : {
                           "type": "string"
                       },
                       "classType" : {
                           "type": "string"
                       }
                   }
                }

            }
       }      
    }
}
some random guy
  • 430
  • 5
  • 9