Grails / Jackson JSON Serialization How To

How do I enable "Jackson serialization in a Grails application?"

In BuildConfig.groovy, I added:

compile 'com.fasterxml.jackson.core:jackson-core:2.5.3'
compile 'com.fasterxml.jackson.core:jackson-annotations:2.5.3'

      

Is that all it takes? Or do you need some other magic in Config.groovy?

How do I serialize a property with just a getter?

I want to include in the generated JSON the value from the get method in one of the domain classes. I tried variations of this:

@JsonAutoDetect(setterVisibility=JsonAutoDetect.Visibility.NONE)
class SportsAgent {
    String name
    String address
    String phone
    String email
    String state

    Client client

    @JsonProperty('clientName')
    String getClientName() {
        if (client) {
            client.name
        }
    }
}

      

This is serialized to JSON but does not include the 'clientName'. All other options I've tried usually resulted in some kind of parsing error. I have done this successfully in older versions of Jackson in Java with @JsonSerialize and @JsonDeserialize, but these annotations no longer exist.

+3


source to share





All Articles