3

我有一个像这样的对象结构:

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
}

以及以下用于序列化为 JSON 的代码

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

但是,没有任何嵌入式集合被序列化。他们只是失踪了。我尝试将以下内容添加到我的 Config.groovy 以使其默认深度序列化:

grails.converters.json.default.deep=true

但这似乎并没有改变什么。我已经看到对象是从调试器中的 MongoDB 填充的,但它只是没有进入 JSON 序列化输出。我怎样才能解决这个问题?

更新

好的,我通过调试代码想出了更多。在 DefaultGrailsDomainClass.getPersistentProperties() 内部,它不会在调用时将集合作为属性返回。JSON 序列化程序从不访问它们。DomainClassMarshaller 的第 103 行是对 getPersistentProperties 的调用,它没有返回所有属性。

    GrailsDomainClassProperty[] properties = domainClass.getPersistentProperties();

似乎这是一个错误!怎么没有人发现过这个?

4

1 回答 1

1

您可以使用GSON 插件。它没有帮助我解决类似的问题,但它可以帮助你。

这个插件是为了克服标准 Grails JSON 转换器中的嵌套对象反序列化问题而编写的,但它也可能更好地序列化它们。

于 2013-09-12T10:45:00.533 回答