3

I know that there are utilities out there that will take a large, nested JSON object, and generate the Java POJOs for it.

But are there any utilities that will take that same JSON, and assuming that POJOs already exist, create getter and setter statements, to actually populate the POJOs with the data in the JSON?

Given this small JSON sample, I am looking for a utility to generate:

data.setSiteId(422950);

And so on. It is a very large JSON object.

{
    "data": {
        "siteId": 422950,
        "protocol": 1,
        "networkId": 253762,
        "comsNetworkId": 0,
        "circuitIds": [
            "A",
            "B",
            "C",
            "D",
            "E",
            "F"
        ],
        "comsCircuitId": "",
        "corpId": "",
        "networkAccessType": 1,
        "localExchangeCarrier": 19,
        "ldCarrier": 1,
        "isdnServiceType": 4,
        "primarySwitchType": 6,
        "t1PriTimeslots": "",
        "centralOfficeSwitchType": 5,
        "framingMode": 4,
        "signaling": 7,
        "encoding": 4,
        "isdnLineFromCentrex": false,
        "sharedDALTestPassed": false,
        "t120": false
    }
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
EdgeCase
  • 4,719
  • 16
  • 45
  • 73
  • 1
    Why you need to generate that source code? Can't you simply deserialize the JSON into the generated POJO? – Raffaele Sep 24 '15 at 14:29
  • On Raffaelas note here's one stackoverflow question that might get you on the right track http://stackoverflow.com/questions/6096940/how-do-i-write-a-custom-json-deserializer-for-gson . There are several other deserializes and you should try to go as far as you can without resorting to custom deserialization. – Aleksi Yrttiaho Sep 24 '15 at 14:30
  • This is good question. Sometimes type safe DTO is required, not only sophisticated map like JSONObject, and when is produced in non-java world (or closed source). Then (thread cited by @AleksiYrttiaho ) ) POJO class (`User` in this sample) don't exist and required is generator (or by hand, of-course) – Jacek Cz Sep 24 '15 at 16:17

2 Answers2

0

You can simple use JSONObject class in

    <dependency>
        <groupId>org.json</groupId>
        <artifactId>json</artifactId>
        <version>20141113</version>
    </dependency>
Oğuz Tanrıkulu
  • 239
  • 2
  • 12
  • I don't agree (but don't give down too). Question is (as I understand) about more type-safe solution – Jacek Cz Sep 24 '15 at 16:19
0

You should look at XStream, it does exactly what you want in very few lines. Essentially:

YourPojo buildObjectFromString(String xmlString) { 
   XStream xStream = new XStream(new DomDriver());
   xStream.alias("data", YourPojo.class);

   return (YourPojo)xStream.fromXML(xmlString);
}


<dependency>
   <groupId>xstream</groupId>
   <artifactId>xstream</artifactId>
   <version>1.2.2</version>
</dependency>
HulkSmash
  • 386
  • 1
  • 6
  • As I understand question, is "how to generate YourPojo class?") - maybe I'm wrong? – Jacek Cz Sep 24 '15 at 16:20
  • @JacekCz in the question, OP says "assuming that POJOs already exist" – HulkSmash Sep 24 '15 at 18:23
  • Seems You are right (i work too much with cross host & cross language projects ;) and question istn totally clear) Then few deserialization techniques exist, in Your answer & above – Jacek Cz Sep 24 '15 at 18:26