• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Fine grained serialization using json

 
chris kumar
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65128
92
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
chris kumar
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65128
92
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Annotations require additions to the bean (albeit innocuous ones), custom renderers do not.
 
chris kumar
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You mean custom renderer for each bean ?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65128
92
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65128
92
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic