【发布时间】:2013-06-11 15:45:26
【问题描述】:
我有一个像这样的对象结构:
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();
似乎这是一个错误!怎么没有其他人发现过这个?
【问题讨论】:
-
您是否也尝试过在渲染之前使用
JSON.use('deep')而不是使用配置属性? -
我得到一个编译错误。它不知道有什么用。我见过有人提到这个,但也有很多人说这些信息不好。也许是一个圣杯神话。所以我不知道该相信什么。
-
Source Code 实现了
use。 -
我在输入之后看到了。我在 BootStrap.groovy 中调用了它,但它仍然没有改变它。我也为 Date 和 ObjectId 注册了两个编组器。我不知道这是否对这个有影响。我爬取了 JSON 数据结构,看到注册了 DeepDomainClassMashler。我不知道为什么它不起作用。
-
我不确定这如何准确地应用于这个讨论,但我提交了一个错误,因为在 JSON 中嵌入对象的逻辑是完全错误的。 jira.grails.org/browse/GRAILS-10175