2011-06-27 7 views
12

JSONコンバータのクラスフィールドを削除する方法はありますか?Grails - grails.converters.JSON - クラス名を削除する

例:

import testproject.* 
import grails.converters.* 
emp = new Employee() 
emp.lastName = "Bar" 
emp as JSON 

文字列が

{"class":"testproject.Employee","id":null,"lastName":"Bar"} 

であるように私は

{"id":null,"lastName":"Bar"} 

を好むの終わりに、コードの1行を追加する方法はありますクラスフィールドを削除しますか?

+0

は、この他の答えhttp://stackoverflow.com/questions/5538423/grails-jsonbuilder/5540471#5540471 – Maricel

答えて

12

これを行う方法はまだありません。 私は、ドメインクラスに次のコードを追加しました:

static { 
    grails.converters.JSON.registerObjectMarshaller(Employee) { 
    return it.properties.findAll {k,v -> k != 'class'} 
    } 
} 

をしかし、あなたはまた、例えば、除外パラメータに「クラス」を追加しなければならないときに、Groovyの@ToStringクラスアノテーションを使用している場合、私は見られるように:

@ToString(includeNames = true, includeFields = true, excludes = "metaClass,class") 
3

1つの選択肢は、ビルダーを使用しないことです。

def myAction = { 
    def emp = new Employee() 
    emp.lastName = 'Bar' 

    render(contentType: 'text/json') { 
     id = emp.id 
     lastName = emp.lastName 
    } 
} 

あなたは従業員が変更された場合、あなたのレンダリングを変更する必要があると思いますので、これは少し小さい直交しています。一方で、レンダリングされる内容をより詳細に制御できます。

1
import testproject.* 
import grails.converters.* 
import grails.web.JSONBuilder 

def emp = new Employee() 
emp.lastName = "Bar" 

def excludedProperties = ['class', 'metaClass'] 

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

    if (!(propName in excludedProperties)) { 
    setProperty(propName, propValue) 
    } 
} 

render(contentType: 'text/json', text: builder.toString()) 
7

これを行うための私の好ましい方法:

def getAllBooks() { 
    def result = Book.getAllBooks().collect { 
     [ 
      title: it.title, 
      author: it.author.firstname + " " + it.author.lastname, 
      pages: it.pageCount, 
     ] 
    } 
    render(contentType: 'text/json', text: result as JSON) 
} 

これは(Book.getAllBoksからすべてのオブジェクトを返します)が、収集方法は、すべてのユーザーが指定の形式に変更されます。

1

@ wwarlockの答えは部分的に正しいです、私はブートストラップにregisterObjectMarshallerを置く必要があります、それは動作することができます。

1
def a = Employee.list() 

String[] excludedProperties=['class', 'metaClass'] 
render(contentType: "text/json") { 
    employees = array { 
     a.each { 
      employee it.properties.findAll { k,v -> !(k in excludedProperties) } 
     } 
    } 
} 

これは私に役立ちます。除外するプロパティを簡単に渡すことができます。または、それを回す:

def a = Employee.list() 

String[] includedProperties=['id', 'lastName'] 
render(contentType: "text/json") { 
    employees = array { 
     a.each { 
      employee it.properties.findAll { k,v -> (k in includedProperties) } 
     } 
    } 
} 

注意:これは単なるオブジェクト用です。 「間違ったキー:予想されるキーのモードですが、オブジェクトだった」と表示された場合、この解決方法はあなたのためではありません。 :)

HP

+0

アプリケーション/ JSONをチェック適切なコンテンツタイプでなければなりません –

1

あなたはgrails.converters.JSON

def converter = emp as JSON 
converter.setExcludes(Employee.class, ["class",""]) 

、その後、あなたが使用することができますで提供setExcludes法を用いて、(クラス名を含む)を除外するためにフィールドをカスタマイズすることができますそれだけで、あなたの要件に応じてのように、

println converter.toString() 
converter.render(new java.io.FileWriter("/path/to/my/file.xml")) 
converter.render(response) 
関連する問題