wood burning stoves 2.0*
The moose likes OO, Patterns, UML and Refactoring and the fly likes Object Model of JSON Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Engineering » OO, Patterns, UML and Refactoring
Bookmark "Object Model of JSON " Watch "Object Model of JSON " New topic
Author

Object Model of JSON

Alex Edword
Greenhorn

Joined: Aug 31, 2013
Posts: 1
Hi There,
It's my first post in this forum. I have just begun my OO journey. Yesterday I was wondering how to represent JSON structure as object model.

I came up with something like :


Now I am little confused about JsonObject. I am good with "key" part of Map, as it could be only String, but "value" could be anything "String || Number || JsonObject || JsonArray || TRUE || FALSE || NULL"

Is it good to do something like :



I might be thinking loud here, (and I smell some bad things as well). Is it good to inherit new object just for making it same type as of its parent. Please let me know better approach for this model.


Thanks.
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4742
    
    7

I think you're overthinking this. What's the point of doing what you're doing?

When it comes to JSON and Java, the problem is actually simpler than what you have in mind: Take a JSON string and map it to a Java object or conversely, take a Java object and translate it into a JSON string. The assumption here is that you have a specific Java object definition that matches the JSON structure. Maybe I'm missing something but IMO, using a generic data structure that can handle the entire universe of possible JSON strings is just an unnecessary layer of abstraction that only adds complexity and no real value.


Junilu - [How to Ask Questions] [How to Answer Questions]
Jelle Klap
Bartender

Joined: Mar 10, 2008
Posts: 1773
    
    7

And there are pleny of frameworks that allow you to easily map between JSON and Java, like Google Gson, JSON Simple or Jackson. It's even possible to combine some of them with a JAXB implementation, and use JAXB to serialize to, and deserialize from JSON instead of XML. Or is this perhaps purely meant as an exercise to practice some OO concepts?


Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4742
    
    7

Jelle Klap wrote:Or is this perhaps purely meant as an exercise to practice some OO concepts?

If this is your intent, then you probably shouldn't start with a recursive structure like JSON. I suggest you choose something simpler, with not only data but interesting behavior as well. Behavior is really what an object should be about; encapsulated data is secondary and should be relevant to the behavior of the object.

That is, when you start thinking "objects", these are some of the basic questions you want to ask:
1. What interesting things does this object do? (Responsibilities)
2. What information does the object need to do these interesting things? (Data encapsulation)
3. Given enough information, can this object do these things independently or does it need help from other objects? (Collaboration)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Object Model of JSON