{"id":14590,"date":"2014-07-04T09:42:15","date_gmt":"2014-07-04T04:12:15","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=14590"},"modified":"2014-07-04T10:17:58","modified_gmt":"2014-07-04T04:47:58","slug":"custom-json-and-xml-marshalling","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/custom-json-and-xml-marshalling\/","title":{"rendered":"Marshall JSON and XML in your own way"},"content":{"rendered":"<p>Has there been instances when you wished to exclude certain properties while generating JSON object from a POJO and felt we had no control over it? Recently I was stuck with the same requirement and that\u2019s what we are going to discuss in this blog post.<\/p>\n<p>Lets say, I have a \u201cUser\u201d class as given below and which of course has a password field too. We would never like it to be a exposed when rendered as JSON \/ XML.<\/p>\n<p>[code]<\/p>\n<p>class User {   <\/p>\n<p>    String firstName<br \/>\n    String lastName<br \/>\n    String email<br \/>\n    String password<br \/>\n}<\/p>\n<p>User user = new User(firstName:\u201dfirst name\u201d,lastName:\u201dlast name\u201d,email:\u201dyour     email\u201d,password:\u201dyour password\u201d)<\/p>\n<p>render user as JSON<\/p>\n<p>\/\/output<br \/>\n{\u201cfirstName\u201d:\u201dfirst name\u201d,\u201clastName\u201d:\u201dlast name\u201d,\u201demail\u201d:\u201dyour email\u201d,\u201dpassword\u201d:\u201dyour password\u201d}<\/p>\n<p>[\/code]<\/p>\n<p>So now the question is how to exclude this password from User Json? Well, after some research I found we could define the way JSON would get marshalled. We need to do two things for the same :<\/p>\n<p><strong>1. Register the marshaller :<\/strong><br \/>\nIt could be done in BootStrap.groovy as given below :<\/p>\n<p>[code]<br \/>\n\/\/For JSON<br \/>\nJSON.registerObjectMarshaller(new org.codehaus.groovy.grails.web.converters.marshaller.json.InstanceMethodBasedMarshaller()) <\/p>\n<p>\/\/For XML<br \/>\nXML.registerObjectMarshaller(new org.codehaus.groovy.grails.web.converters.marshaller.xml.InstanceMethodBasedMarshaller()) <\/p>\n<p>[\/code]<\/p>\n<p><strong>2. Define your own marshaller in the POJO :<\/strong><\/p>\n<p>[code]<br \/>\nclass User {<\/p>\n<p>    String firstName<br \/>\n    String lastName<br \/>\n    String email<br \/>\n    String password<\/p>\n<p>    JSON toJSON(json) {<br \/>\n         json.build {<br \/>\n              firstName(firstName)<br \/>\n              email(email)<br \/>\n              fullName(firstName+lastName)<br \/>\n         }<br \/>\n    }<br \/>\n}<\/p>\n<p>[\/code]<\/p>\n<p>Voilla! Its that simple. Now, as you are expecting the output would be<\/p>\n<p>[code]<\/p>\n<p>{\u201cfirstName\u201d:\u201dfirst name\u201d,\u201demail\u201d:\u201dyour email\u201d,\u201dfullName\u201d:\u201dfirst name last name\u201d}<\/p>\n<p>[\/code]<br \/>\nsame goes for the XML response.<\/p>\n<p>Hope this helped.<\/p>\n<p>Thank You,<br \/>\nJeevesh Pandey<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Has there been instances when you wished to exclude certain properties while generating JSON object from a POJO and felt we had no control over it? Recently I was stuck with the same requirement and that\u2019s what we are going to discuss in this blog post. Lets say, I have a \u201cUser\u201d class as given [&hellip;]<\/p>\n","protected":false},"author":75,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":3},"categories":[7],"tags":[],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/14590"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/75"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=14590"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/14590\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=14590"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=14590"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=14590"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}