Quantcast

Customize json data depending on request type

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Customize json data depending on request type

lmsilva
Dear all,

I was wondering if there is an elegant way in grails to customize the json being rendered for an object (or objects) returned by a Domain.findAll() call?

I found this really cool article that explains how we can use the json object marshaller to adapt the data being returned from a certain domain class but I need to do it based on the place I'm calling from (e.g. some places return the full thing, some other places only return parts of the domain class object).

For instance, given this data:
[
   {
      username:"lmsilva",
      firstName:"Luis",
      lastName:"Silva",
      settings:{
         defaultShareableObjectAcl:"Private",
         defaultSessionAcl:"Friends",
      },
      friends: [
      ]
   },
   {
      username:"macm",
      firstName:"first name",
      lastName:"second name",
      settings:{
         defaultShareableObjectAcl:"Private",
         defaultSessionAcl:"Friends",
      },
      friends:[
         {
            username:"lmsilva",
            firstName:"Luis",
            lastName:"Silva",
            settings:{
               defaultShareableObjectAcl:"Private",
               defaultSessionAcl:"Friends",
            },
            friends:[
            ]
         }
      ]
   }
]

I do not want to return the settings portion of the "friends".

Any ideas on how I could do this without having to create a new json object by extracting ONLY the portions of it that I'm interested in? Is there an easier way to do it? (e.g. a json filter function, a findAll special query, etc)?

Thanks in advance,
Luis Silva
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Customize json data depending on request type

bksaville
See this: https://github.com/pedjak/grails-marshallers, if it's not released as a plugin (pretty sure it was?), then at least it will give you ideas.  And the associated blog post is really good if I'm remembering right:  http://jwicz.wordpress.com/2011/07/11/grails-custom-xml-marshaller/

-Brian

You didn't wait around for the rest of my answer ;)

On Thu, Jun 21, 2012 at 1:03 PM, lmsilva <[hidden email]> wrote:
Dear all,

I was wondering if there is an elegant way in grails to customize the json
being rendered for an object (or objects) returned by a Domain.findAll()
call?

I found this
http://manbuildswebsite.com/2010/02/15/rendering-json-in-grails-part-3-customise-your-json-with-object-marshallers/
really cool article  that explains how we can use the json object marshaller
to adapt the data being returned from a certain domain class but I need to
do it based on the place I'm calling from (e.g. some places return the full
thing, some other places only return parts of the domain class object).

For instance, given this data:
[
  {
     username:"lmsilva",
     firstName:"Luis",
     lastName:"Silva",
     settings:{
        defaultShareableObjectAcl:"Private",
        defaultSessionAcl:"Friends",
     },
     friends: [
     ]
  },
  {
     username:"macm",
     firstName:"first name",
     lastName:"second name",
     settings:{
        defaultShareableObjectAcl:"Private",
        defaultSessionAcl:"Friends",
     },
     friends:[
        {
           username:"lmsilva",
           firstName:"Luis",
           lastName:"Silva",
           settings:{
              defaultShareableObjectAcl:"Private",
              defaultSessionAcl:"Friends",
           },
           friends:[
           ]
        }
     ]
  }
]

I do not want to return the settings portion of the "friends".

Any ideas on how I could do this without having to create a new json object
by extracting ONLY the portions of it that I'm interested in? Is there an
easier way to do it? (e.g. a json filter function, a findAll special query,
etc)?

Thanks in advance,
Luis Silva

--
View this message in context: http://grails.1312388.n4.nabble.com/Customize-json-data-depending-on-request-type-tp4630513.html
Sent from the Grails - user mailing list archive at Nabble.com.

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

   http://xircles.codehaus.org/manage_email



Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Customize json data depending on request type

lmsilva
Thanks Brian! This does seem to be exactly what I was looking for :o).
Loading...