Grails json 编组

发布于 2024-11-09 06:38:17 字数 779 浏览 0 评论 0原文

我有一个域对象,它与另一个域对象具有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

知你几分 2024-11-16 06:38:17

也许你正在寻找类似的东西:

def filterLang = Language.findByCode("de")

// search for persons having the filter language assigned
def foundPersons = Persong.executeQuery("select p from Person as p inner join p.languages as lang where lang = :filterLang",[filterLang : filterLang])

def json = []

foundPersons .each {
    json << [id: it.id, name: it.name, language: filterLang]
}

render json as JSON

maybe you're looking for something like that:

def filterLang = Language.findByCode("de")

// search for persons having the filter language assigned
def foundPersons = Persong.executeQuery("select p from Person as p inner join p.languages as lang where lang = :filterLang",[filterLang : filterLang])

def json = []

foundPersons .each {
    json << [id: it.id, name: it.name, language: filterLang]
}

render json as JSON
唐婉 2024-11-16 06:38:17

对于这样一个常见的用例来说,听起来非常复杂。您可以使用条件搜索您的人员和相应的语言,然后将结果转换为适当的 json 格式。例如

def result = Person.createCriteria().list {
   language {
      eq("lang", "de") // could be your search term
   }
} // you can also use hql to achieve our requirement

def json = []

result.each {
    json << [id: result.id, name: result.name, ...]
}

render json as 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.

def result = Person.createCriteria().list {
   language {
      eq("lang", "de") // could be your search term
   }
} // you can also use hql to achieve our requirement

def json = []

result.each {
    json << [id: result.id, name: result.name, ...]
}

render json as JSON
み零 2024-11-16 06:38:17

您可以过滤查询并准备渲染:

import org.hibernate.criterion.CriteriaSpecification

List list = Person.withCriteria {
  maxResults 5
  resultTransformer(CriteriaSpecification.ALIAS_TO_ENTITY_MAP)
  alias 'languages', 'lan'
  projections {
    property 'id', 'id'
    property 'displayName', 'name'
    property 'lan.language', 'language'
  }
  eq 'lan.language', 'FR'    
}

然后您可以使用结果

render list as JSON

您必须使用 resultTransformer 并为每个 property 设置 alias >。因此,您可以为特定情况创建自定义渲染。

注意:该代码尚未经过测试,只是一个粗略的想法。

You can to filter your query and prepare it to render:

import org.hibernate.criterion.CriteriaSpecification

List list = Person.withCriteria {
  maxResults 5
  resultTransformer(CriteriaSpecification.ALIAS_TO_ENTITY_MAP)
  alias 'languages', 'lan'
  projections {
    property 'id', 'id'
    property 'displayName', 'name'
    property 'lan.language', 'language'
  }
  eq 'lan.language', 'FR'    
}

then you can to use the result

render list as JSON

You must to use resultTransformer and set alias for each property. Thus you can create custom renders for specific situations.

Note: that code has not be tested, is simply a rough idea.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文