title: How to stop null value fields from being marshalled in Grails
body: <p>I am using Grails 1.3.7 and have the following DTO</p>
  
  <pre><code>class Result {
     String key
     List errors
  }
  </code></pre>
  
  <p>Now either the key or the errors will be present, but not both. If the Result has a key, I don't want the marshalled JSON to have the errors field with a null value as shown below</p>
  
  <pre><code>{"errors":null,"key":"abcde"}
  </code></pre>
  
  <p>I looked at <a href="http://grails.org/Converters+Reference" rel="nofollow">configuration options</a> for the converter and it does not have an option for suppressing null values. I also looked at the last section on that page titled 'Customizing Converters Results' and registered an object marshaller as follows</p>
  
  <pre><code>class Result {
  
     static {
        grails.converters.JSON.registerObjectMarshaller(Result) {
       return it.properties.findAll {k,v -&gt; v != null}
       }
     }
  
  }
  </code></pre>
  
  <p>But still the result contains errors with the null value. I am looking for the cleanest way to achieve this for both XML and JSON.</p>