2011-05-09 59 views
10

我正在使用grails-1.3.2和hbase-0.2.4。grails.converters.JSON幾個屬性除外

我有以下域類:

class MyClass{ 
    String val1 
    String val2 
    String val3 

    //---- 

} 

class MyClassController{ 
    def someAction = { 
     def myClass = new MyClass() 
     //---- 

     String valAsJson = (myClass as JSON) 

     render valAsJson 
    } 
} 

我的問題是,是任何短期方式渲染性能的唯一部分(例如渲染以外的所有財產VAL3)?

回答

3

如果您只想一直包含特定屬性,那麼您確實需要使用ObjectMarshaller接口。有關更多詳細信息,請參閱this article

+0

Gregg它的工作原理,但我想除了屬性,因爲我有幾個域類,可以需要20個以上的屬性,我想除了只有一個或two.If我沒有找到其他的解決方案,除了我會用它...謝謝 – Bella 2011-05-10 11:06:32

+0

很酷的東西!你救了我的一天) – 2014-06-30 12:46:53

+0

@Gregg你認爲或遇到性能問題時marshallers實施。我們在2個項目中使用了它們,並且我們體驗了它們。有沒有其他方法呢? – 2015-08-07 08:46:51

5

你可以做這樣的事情:

def myClass = MyClass.get(1) 

//include 
render myClass.part(include:['val1', 'val2']) as JSON 

//except 
render job.part(except:['val2','val3']) as JSON 

BootStrap.groovy中:

import org.codehaus.groovy.grails.orm.hibernate.support.ClosureEventTriggeringInterceptor as Events 

class BootStrap { 
def grailsApplication 

def excludedProps = [Events.ONLOAD_EVENT, 
    Events.BEFORE_DELETE_EVENT, Events.AFTER_DELETE_EVENT, 
    Events.BEFORE_INSERT_EVENT, Events.AFTER_INSERT_EVENT, 
    Events.BEFORE_UPDATE_EVENT, Events.AFTER_UPDATE_EVENT] 

    def init = { servletContext -> 
    grailsApplication.domainClasses.each{ domainClass -> 
     domainClass.metaClass.part= { m -> 
      def map= [:] 
      if(m.'include'){ 
       m.'include'.each{ 
        map[it]= delegate."${it}" 
       } 
      }else if(m.'except'){ 
       m.'except'.addAll excludedProps 
       def props= domainClass.persistentProperties.findAll { 
        !(it.name in m.'except') 
       } 
       props.each{ 
        map[it.name]= delegate."${it.name}" 
       } 
      } 
      return map 
     } 
    } 
    } 
    def destroy = { 
    } 
} 

如果你知道如何創建自己的插件,那麼只需創建一個插件,這一點,所以您可以在所有Grails應用程序中使用它。

+0

謝謝你的回答...它不適用於hbase,我找不到類似的hbase。 – Bella 2011-05-09 13:40:40

+2

你應該參考你得到代碼http://foxgem.blogspot的位置。com/2010/06/return-partial-domain-class.html – 2012-06-11 13:32:44

3

或者,你可以只製作一張你想要的屬性,然後對其進行編碼爲JSON

Map m = [ 'val1', 'val2' ].inject([:]) { map, val -> map."$val" = a."$val" ; map } 
render m as JSON 

要排除的屬性,你需要做這樣的事情(未測試)

def exclude = [ 'val3' ] 
Map m = new DefaultGrailsDomainClass(MyClass.class).properties.findAll { 
    !(it.name in exclude) 
}.inject([:]) { map, val -> 
    map."$val.name" = a."$val.name" ; map 
} 
render m as JSON 
+0

@tim_yates ..是否有類似的代碼除外屬性? – Bella 2011-05-09 13:43:11

+0

添加了我認爲應該工作的內容......但未對其進行測試...如果您嘗試了它,並且它能正常工作,您可以告訴我嗎? – 2011-05-09 13:55:24

+0

它的工作,但它不呈現我的班級名稱。 – Bella 2011-05-09 15:35:17

2

如果你只是想呈現的MyClass JSON作爲一個實例,排除某些屬性,這是一個使用由Grails中提供的JSONBuilder類的解決方案

import grails.web.JSONBuilder 

class MyClassController{ 

    def someAction = { 

     def myClass = new MyClass() 

     def builder = new JSONBuilder.build { 
      myClass.properties.each {propName, propValue -> 

      // Properties excluded from the JSON 
      def excludes = ['class', 'metaClass', 'val3'] 

      if (!excludes.contains(propName)) { 

       setProperty(propName, propValue) 
      } 
     } 
     render(text: builder.toString(), contentType: 'application/json') 
    } 
} 
+0

它不適合我。 – Bella 2011-05-10 07:25:07

1

的JSON排除的Marshaller插件

我最近需要解決這個問題。我繼續將解決方案打包到一個插件中,該插件允許您輕鬆地從JSON轉換器的輸出中排除類屬性。它可以在Grails插件門戶上找到。

install the plugin之後,您將有權訪問名爲excludeFor *()的grails.converters.JSON類上的方法。

更廣泛的文檔可以在這裏找到:How to use the JSON Exclusion Marshaller

但基本上可以作爲這樣的:

import grails.converters.JSON 

def json, resultTeachersWillSee, resultOtherStudentsWillSee 

// Given a TestStudent Domain Class 
def student = new TestStudent([ 
    firstName: "Tobias", 
    lastName: "Funke", 
    gradePointAverage: 3.6, 
    studentID: "FS-210-7312", 
    socialSecurityNumber: "555-55-5555" 
]) 
student.save(flush: true) 

// When 
JSON.excludeForTeachers(TestStudent, ['socialSecurityNumber', 'id', 'class']) 

JSON.use('excludeForTeachers') { 
    json = new JSON(student) 
} 
resultTeachersWillSee = json.toString() 

// Then 
assert resultTeachersWillSee == '{"firstName":"Tobias", 
     "gradePointAverage":3.6, "lastName":"Funke", 
     "studentID":"FS-210-7312"}' 



// And When 
JSON.excludeForOtherStudents(TestStudent, ['gradePointAverage', 'studentID', 
    'socialSecurityNumber', 'id', 'class']) 

JSON.use('excludeForOtherStudents') { 
    json = new JSON(student) 
} 
resultOtherStudentsWillSee = json.toString() 

// Then 
assert resultOtherStudentsWillSee == '{"firstName":"Tobias", 
     "lastName":"Funke"}' 

JSON.excludeForTeachers(...)創建了一個名爲 「excludeForTeachers」 命名對象編組。編組器從生成的JSON輸出中排除學生對象的三個屬性。 'socialSecurityNumber'屬性在類中顯式定義,而'id'屬性在後臺被GORM添加。無論如何,教師不需要看到任何這些屬性。

該插件正在爲我服務......我希望別人也發現它也有幫助。