aspose file tools*
The moose likes Servlets and the fly likes Fine grained serialization using json Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Fine grained serialization using json" Watch "Fine grained serialization using json" New topic
Author

Fine grained serialization using json

chris kumar
Greenhorn

Joined: Jun 04, 2009
Posts: 17
Hi All,

I have a set of beans in the server side that needs to be rendered as a json object to the browser, for doin this we can use any of the open source libraraies like
json, google json and many more but how can we have a fine grained control on the object properties.

For ex: i have a bean that have 10 properties but i want to render 6 properties out of the 10, if i send this bean object to the renderer it will serialize all the properties.

How can i have such a fine grained control ?

Thanks,
Chris
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61437
    
  67

With Gson, for example, there are some annotations that can control this, or you could override the default renderers with your own. The Gson documentation is pretty good -- give it a read.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
chris kumar
Greenhorn

Joined: Jun 04, 2009
Posts: 17
Hi Bear,

This would lead me to make changes to my bean, and the other problem i see is if i want the properties on a role basis ?

Thanks,
Chris
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61437
    
  67

Annotations require additions to the bean (albeit innocuous ones), custom renderers do not.
chris kumar
Greenhorn

Joined: Jun 04, 2009
Posts: 17
You mean custom renderer for each bean ?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61437
    
  67

Depends. Usually. If you can determine what you want to render and what you don't using reflection you could probably share a renderer among multiple bean types.

Or, rather than render the bean itself, create a Map with the properties you want from the bean, excluding those that you do not, and JSON-ify that instead.
chris kumar
Greenhorn

Joined: Jun 04, 2009
Posts: 17
Converting a huge object graph to a map and then again jsoni-fy, probably need to think about performance.

Was thinking to have some meta defination passed along with the object to the renderer
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61437
    
  67

I'm not sure if there is any provisions made for such metadata. Best to look at the documentation for the various JSON libraries to see if there's one that meets your needs.
 
Consider Paul's rocket mass heater.
 
subject: Fine grained serialization using json