• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Deserialize JSON to Java class

 
Ranch Hand
Posts: 231
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Lets say that I have a json object that looks like this:



Is there a way to unmarshall this JSON object to autogenerated Java class (or does the classes already have to be defined)?

Would the classes look like this:







Question(s):

(1) Do I need to have these objects in place or can the Jackson library deserialize this for me and produce the Java source classes?

(2) Are my classes conducive to the JSON Object that I listed above?

With thanks!
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I believe that the class must already exist -- at least for libraries such as Gson (I think Jackson is similar, though I haven't used it much). You can get Gson to create a Map structure that represents the JSON, but it won't magically create a new class definition and load it.

If it's a known construct, why not create a class to represent it? Or is it completely random?
 
James Dekker
Ranch Hand
Posts: 231
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Bear!

The reason is because the JSON has been created theoretically by a non programmer (everyone loves DSL programming ).

And I don't know if this JSON object would deserialize it into one large class or does this class need separate inner classes.

How would one design their prospective Java classe(s) to support this particular JSON object design?
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nested objects are usually their own class. They do not need to be inner classes.

I would design in the other direction though: define the Java classes first, to model what they need to model, and the JSON falls naturally out of that.
 
reply
    Bookmark Topic Watch Topic
  • New Topic