Jayakrishnan Nair
Jayakrishnan Nair

Reputation: 33

How to stop null value fields from being marshalled in Grails

I am using Grails 1.3.7 and have the following DTO

class Result {
   String key
   List errors
}

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

{"errors":null,"key":"abcde"}

I looked at configuration options 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

class Result {

   static {
      grails.converters.JSON.registerObjectMarshaller(Result) {
     return it.properties.findAll {k,v -> v != null}
     }
   }

}

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.

Upvotes: 1

Views: 904

Answers (1)

Andrew
Andrew

Reputation: 2249

Trying this Grails 2.0.3 results in an error indicating conflict between the errors field and GORM's automatically-added grails.validation.ValidationErrors field (also named errors). Your marshaller looks OK, so you may just need to change your field name to something other then errors?

Upvotes: 1

Related Questions