0

I have grails 3.1 application.

Below is the code for customizing the elements, which uses JSON marshaller, which works perfectly.

JSON.createNamedConfig("memeListView", {
            JSON.registerObjectMarshaller(Meme) { Meme memeIns ->
                return [
                        caption: memeIns.caption,
                        likeCount: memeIns.likeCount,
                ]
            }
        })

But, I have tried the above code for XML marshaller, but it displays entire object attributes, instead of required. Below is the code for XML marshalling.

XML.createNamedConfig("memeListView", {
                XML.registerObjectMarshaller(Meme) { Meme memeIns ->
                    return [
                            caption: memeIns.caption,
                            likeCount: memeIns.likeCount,
                    ]
                }
            })

In action part

JSON.use('memeListView'){
      render memesList as JSON
}

Kindly suggest me, how to do marshalling for xml response.

Amarnath R
  • 973
  • 3
  • 14
  • 33

1 Answers1

1

I would imagine you have to return something XML-ish. I think the xml marshaller passes in a 2nd arg.

So maybe (trying this from memory):

XML.createNamedConfig("memeListView", {
    it.registerObjectMarshaller(Meme) { Meme memeIns, xml ->
        xml.build {
            caption(memeIns.caption )
            likeCount( memeIns.likeCount )
        }
    }
})

or

XML.createNamedConfig("memeListView", {
    it.registerObjectMarshaller(Meme) { Meme memeIns, xml ->
        xml.attribute 'caption', memeIns.caption
        xml.attribute 'likeCount', memIns.likeCount
    }
})

depending on how you want the XML to look?

billjamesdev
  • 14,554
  • 6
  • 53
  • 76
  • XML.use("memeListView") { render memeList as XML } – Amarnath R Apr 14 '16 at 02:51
  • 1
    try it again? Notice I changed to "it.registerObjectMarshaller" from "XML.registerObejctMarshaller". Also... add a breakpoint to see if the marshaller code is actually being used? – billjamesdev Apr 14 '16 at 03:30
  • Glad it worked... If we're done here, go ahead and click the check-mark so others know you found what you were looking for. – billjamesdev Apr 14 '16 at 22:12