Excluding some fields from JSON Serialization

View: New views
2 Messages — Rating Filter:   Alert me  

Excluding some fields from JSON Serialization

by Jeffrey D. Gortatowsky :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

When writing a domain object via JSON to a response in a controller, is there a way to prevent certain fields from be written? Most examples show the entire domain object being written in a controller method via render().

What I did is I used the JSON markup builder syntax to only write the fields I want written to the client. But it's ... well ... ugly I think. I hate that nagging feeling I am missing something simple... :)

The original service I am rewriting in GRAILS used the Jackson JSON library and you could annotate fields to be excluded...

Cheers

 ---------------------------------------


Jeff Gortatowsky, Fullerton, CA | Twitter: JeffGortatowsky

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email



Re: Excluding some fields from JSON Serialization

by Alfredo Cavalcanti Segundo :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

My temporary solution was to create a new object that has only the properties that will be sent to the client. And before you call the "render ()" you pass the old object as a parameter to the new and the binding of grails solve the problem.

On Sun, Nov 8, 2009 at 16:38, Jeff Gortatowsky <indanapt@...> wrote:
When writing a domain object via JSON to a response in a controller, is there a way to prevent certain fields from be written? Most examples show the entire domain object being written in a controller method via render().

What I did is I used the JSON markup builder syntax to only write the fields I want written to the client. But it's ... well ... ugly I think. I hate that nagging feeling I am missing something simple... :)

The original service I am rewriting in GRAILS used the Jackson JSON library and you could annotate fields to be excluded...

Cheers

 ---------------------------------------


Jeff Gortatowsky, Fullerton, CA | Twitter: JeffGortatowsky

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

   http://xircles.codehaus.org/manage_email





--
Alfredo Cavalcanti Segundo