Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Deserialize JSON to Java class

 
James Dekker
Ranch Hand
Posts: 221
  • 0
  • Mark post as helpful
  • send pies
  • 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!
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64192
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • 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: 221
  • 0
  • Mark post as helpful
  • send pies
  • 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
Author and ninkuma
Marshal
Pie
Posts: 64192
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic