chubbsondubs
chubbsondubs

Reputation: 38676

Deep JSON serialization in Grails not working

I have an object structure like so:

class Message {

   static mapWith="mongo"
   static embedded = ['to', 'author', 'comments', 'tags']

   ObjectId id
   Set<ObjectId> to
   Author author
   String text
   List<Comment> comments
   Set<String> tags
   Date postedOn
   Date lastEditOn
}

class Comment {
   Author author
   String text
   int thumbsUp = 0
   int thumbsDown = 0
   Date postedOn
   Date lastEditOn
}

And the following code for serialization to JSON

render Message.findStreamFor( session.user, groups, 0, 20 ) as JSON

However, none of the embedded collections are being serialized. They are just missing. I've tried adding the following to my Config.groovy to make it deeply serialize by default:

grails.converters.json.default.deep=true

But that doesn't seem to change anything. I've seen the objects are populated from MongoDB in the debugger, but it just doesn't make it to the JSON serialized output. How can I fix this?

UPDATE

Ok I've figured out a bit more by debugging the code. Inside the DefaultGrailsDomainClass.getPersistentProperties() it doesn't return the collections as properties when called. And JSON serializer never visits them. On line 103 of DomainClassMarshaller is the call to getPersistentProperties which isn't returning all properties.

    GrailsDomainClassProperty[] properties = domainClass.getPersistentProperties();

Seems like this is a bug! How has no one else ever found this?

Upvotes: 3

Views: 2061

Answers (1)

Greg Dubicki
Greg Dubicki

Reputation: 6940

You could to use GSON plugin. It didn't help me in a similar problem but it may help you.

This plugin was written to overcome nested object deserialization problem in standard Grails JSON converter, but it may also be better at serializing them.

Upvotes: 1

Related Questions