grails.converters.JSON 除了少数属性
我正在使用 grails-1.3.2 和 hbase-0.2.4。
我有以下域类:
class MyClass{
String val1
String val2
String val3
//----
}
class MyClassController{
def someAction = {
def myClass = new MyClass()
//----
String valAsJson = (myClass as JSON)
render valAsJson
}
}
我的问题是,是否有任何短方法仅渲染部分属性(例如渲染除 val3 属性之外的所有属性)?
I am using grails-1.3.2 and hbase-0.2.4.
I have the following domain class:
class MyClass{
String val1
String val2
String val3
//----
}
class MyClassController{
def someAction = {
def myClass = new MyClass()
//----
String valAsJson = (myClass as JSON)
render valAsJson
}
}
My question is, is any short way render only part of properties(for example render all except val3 property) ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
你可以这样做:
Bootstrap.groovy:
如果你知道如何创建我们自己的插件,那么只需为此创建一个插件,这样你就可以在所有 grails 应用程序中使用它。
You can do something like this :
Bootstrap.groovy :
If you know how to create our own plugin, then just create one plugin for this, so that you can use it across all the grails applications.
如果您只想将
MyClass
的实例呈现为 JSON,不包括某些属性,这里有一个使用 Grails 提供的JSONBuilder
类的解决方案If you simply want to render an instance of
MyClass
as JSON, excluding certain properties, here's a solution that uses theJSONBuilder
class provided by Grails如果您只想始终包含特定属性,那么您确实需要使用 ObjectMarshaller 接口。请参阅 这篇文章了解更多详细信息。
If you want to only include specific properties all the time, you would really want to use the ObjectMarshaller interface. See this article for more details.
或者,您可以只创建所需属性的映射,然后将它们编码为 JSON
要排除属性,您需要执行类似的操作(未经测试)
Or, you could just create a map of the properties you wanted, then encode them as JSON
To exclude properties, you would need to do something like this (UNTESTED)
JSON Exclusion Marshaller 插件
我最近需要解决这个问题。我继续将解决方案打包到一个插件中,该插件允许您轻松地从 JSON 转换器的输出中排除类属性。它可以在 Grails 插件门户上找到。
安装插件后,您将可以访问一种方法在 grails.converters.JSON 类上,名为 exceptFor*()。
可以在这里找到更广泛的文档:如何使用 JSON 排除 Marshaller
但基本上它可以如下使用:
JSON.excludeForTeachers(...) 创建一个名为“excludeForTeachers”的命名对象编组器。编组器从生成的 JSON 输出中排除学生对象的三个属性。 'socialSecurityNumber' 属性是在类中明确定义的,而 'id' 属性是 GORM 在幕后添加的。无论如何,教师不需要看到任何这些属性。
该插件对我很有帮助...我希望其他人也发现它有帮助。
The JSON Exclusion Marshaller Plugin
I needed to solve this problem recently. I went ahead and packaged the solution into a plugin that allows you to easily exclude class properties from the JSON converter's output. It is available on the Grails Plugin Portal.
After you install the plugin, you will have access to a method on the grails.converters.JSON class called excludeFor*().
More extensive documentation can be found here: How to use the JSON Exclusion Marshaller
But basically it can be used as such:
JSON.excludeForTeachers(...) creates a named object marshaller called "excludeForTeachers". The marshaller excludes three properties of the student object from the resulting JSON output. the 'socialSecurityNumber' property is explicitly defined in the class, while the 'id' property was added by GORM behind the scenes. In any case, teachers don't need to see any of those properties.
The plugin is serving me well... I hope others find it helpful too.