2011-06-27 14 views
12

Có cách nào để loại bỏ trường lớp trong trình chuyển đổi JSON không?Grails - grails.converters.JSON - xóa tên lớp

Ví dụ:

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

như là một chuỗi là

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

Tôi muốn

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

Có cách nào để thêm một dòng mã vào cuối để loại bỏ trường lớp?

+0

Kiểm tra câu trả lời này khác http://stackoverflow.com/questions/5538423/grails-jsonbuilder/5540471#5540471 – Maricel

Trả lời

12

Dưới đây là một cách để thực hiện. Tôi đã thêm một mã bên cạnh các lớp miền:

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

Nhưng khi tôi tìm thấy nếu bạn đã sử dụng Groovy @ToString lớp chú thích khi bạn cũng phải thêm 'lớp' để tham số không bao gồm, ví dụ:

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

Một thay thế là không sử dụng xây dựng:

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

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

Đây là một chút ít trực giao vì bạn sẽ cần phải thay đổi render của bạn nếu thay đổi nhân viên; mặt khác, bạn có nhiều quyền kiểm soát hơn đối với những gì được hiển thị.

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

cách ưa thích của tôi để làm điều này:

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) 
} 

này sẽ trả lại tất cả các đối tượng từ Book.getAllBoks() nhưng phương pháp thu thập sẽ thay đổi tất cả sang định dạng bạn chỉ định.

1

@ wwarlock của câu trả lời là một phần đúng, tôi phải đặt registerObjectMarshaller trên Bootstrap, nó có thể làm việc.

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) } 
     } 
    } 
} 

Điều này phù hợp với tôi. Bạn có thể dễ dàng vượt qua bất kỳ thuộc tính nào để loại trừ. Hoặc xoay vòng quanh:

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) } 
     } 
    } 
} 

Hãy coi chừng: Điều này chỉ dành cho các đối tượng đơn giản. Nếu bạn thấy "Khóa không đúng chỗ: chế độ mong đợi của KEY nhưng là OBJECT", giải pháp này không dành cho bạn. :)

HP

+0

application/json phải là loại nội dung phù hợp –

1

Bạn có thể tùy chỉnh các lĩnh vực được loại trừ (bao gồm cả tên lớp) bằng cách sử dụng phương pháp setExcludes cung cấp trong grails.converters.JSON

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

và sau đó, bạn có thể sử dụng nó giống như theo yêu cầu của bạn,

println converter.toString() 
converter.render(new java.io.FileWriter("/path/to/my/file.xml")) 
converter.render(response)