Grails json 编组
我有一个域对象,它与另一个域对象具有 1 - M 关系,例如
Person 1 -> 。 M Langauges
我已经注册了一个 JSON 对象编组器来编组 Person 对象。我正在处理的一个用例是以表格格式显示人员,其中默认显示主要语言。
我遇到的问题是,当用户生成语言搜索时,我想显示该人的匹配语言而不是主要语言。
我遇到的问题是,我不知道如何访问在对象编组器中搜索的语言,因此我无法确定要在 JSON 中呈现表格格式的匹配语言。
以下是我为 Person 提供的示例代码:
JSON.registerObjectMarshaller(Person) {
def returnArray = [:]
returnArray['id'] = it.id
returnArray['name'] = it.displayName?:""
//I would like to be able to get the language matching a search param here
//when a search has been carried out
returnArray['language'] = it.primaryLanguage?:""
}
目前,我的解决方法是使用一个 PersonWrapper,在构造函数中传递搜索项,为包装器注册一个对象编组器,并在包装器中进行过滤。
这对我来说似乎非常浪费,因为我需要迭代我的域结果并为每个实例创建一个包装器。
任何建议将不胜感激。
I have a domain object which has a 1 - M relationship with another domain object e.g.
Person 1 -> M Langauges
I have registered a JSON object marshaller to marshall the Person object. A use case I am dealing with is displaying the Person in tabular format where the primary language is displayed by default.
The issue I have is when a user generates a search for language and I want to display the matching language for the person rather than the primary language.
The issue I have is that I do not know how to access the language being searched in the object marshaller and as such I can't determine the matching language to render in the JSON for the tabular format.
Here's the sample code I have for the Person:
JSON.registerObjectMarshaller(Person) {
def returnArray = [:]
returnArray['id'] = it.id
returnArray['name'] = it.displayName?:""
//I would like to be able to get the language matching a search param here
//when a search has been carried out
returnArray['language'] = it.primaryLanguage?:""
}
At the moment, the workaround I have is to have a PersonWrapper where I pass the search term in the constructor, register an object marshaller for the wrapper and filter in the wrapper.
This seems pretty wasteful to me as I need to iterate over my domain results and create a wrapper for each instance.
Any suggestions would be gratefully appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
也许你正在寻找类似的东西:
maybe you're looking for something like that:
对于这样一个常见的用例来说,听起来非常复杂。您可以使用条件搜索您的人员和相应的语言,然后将结果转换为适当的 json 格式。例如
sounds very complicated for such a common use case. you can search through your persons and corresponding languages by using a criteria and than convert the result into a appropriate json format. e.g.
您可以过滤查询并准备渲染:
然后您可以使用结果
您必须使用
resultTransformer
并为每个property
设置alias
>。因此,您可以为特定情况创建自定义渲染。注意:该代码尚未经过测试,只是一个粗略的想法。
You can to filter your query and prepare it to render:
then you can to use the result
You must to use
resultTransformer
and setalias
for eachproperty
. Thus you can create custom renders for specific situations.Note: that code has not be tested, is simply a rough idea.