Grails - grails.converters.JSON - removing the class name

后端 未结 7 826
梦如初夏
梦如初夏 2020-12-16 00:14

Is there a way to remove the class field in a JSON converter?

Example:

import testproject.*
import grails.converters.*  
emp = new Employee()  
emp         


        
相关标签:
7条回答
  • 2020-12-16 00:42

    My preferred way of doing this:

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

    This will return all the objects from Book.getAllBoks() but the collect method will change ALL into the format you specify.

    0 讨论(0)
  • 2020-12-16 00:45

    Here is yet one way to do it. I've added a next code to the domain class:

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

    But as I found if you have used Groovy @ToString class annotation when you also must add 'class' to excludes parameter, e.g.:

    @ToString(includeNames = true, includeFields = true, excludes = "metaClass,class")
    
    0 讨论(0)
  • 2020-12-16 00:46

    You can customize the fields to be excluded (including the class name) using the setExcludes method provided in the grails.converters.JSON

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

    and then, you can use it just like according to your requirements,

    println converter.toString()
    converter.render(new java.io.FileWriter("/path/to/my/file.xml"))
    converter.render(response)
    
    0 讨论(0)
  • 2020-12-16 00:47

    @wwarlock's answer is partly right, I have to put the registerObjectMarshaller on Bootstrap, it can work.

    0 讨论(0)
  • 2020-12-16 00:51
    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())
    
    0 讨论(0)
  • 2020-12-16 00:53
    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) }
            }
        }
    }
    

    This works for me. You can easily pass in any property to exclude. Or turn it around:

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

    Beware: This is only for simple objects. If you see "Misplaced key: expected mode of KEY but was OBJECT" this solution is not for you. :)

    HP

    0 讨论(0)
提交回复
热议问题