Search code examples
gsongrails-controllergrails3

List of String throws ClassCastException when using gson with rest profile in Grails


When I try to render List of Strings with GSON and Grails Rest profile app, I am getting

java.lang.ClassCastException: _info_app_name__schemaImporter_index_gson$_run_closure1 cannot be cast to grails.plugin.json.builder.StreamingJsonBuilder$StreamingJsonDelegate

My Controller class is as below

class SchemaImporterController {
    static responseFormats = ['json']

    def index() {
        def data = [:]
        data.stringList = [
            'One',
            'Two',
            'Three',
            'Four
        ] as ArrayList<String>
        return data
    }
}

and my GSON index view is as below

model{
    List<String> stringList
}

json{
    informationList stringList.each { String str ->
        singleEntry str
    }
}

I also tried different variations when declaring model variables like, List stringList, ArrayList

But its returning same error everytime I do this. Any idea why ? Just to note here that its working fine when I render other domain classes.


Solution

  • I found the culprit. I was using each when iterating over the list. I removed it and made my GSON view like below and it started working fine.

    model{
        List<String> stringList
    }
    
    json{
        informationList stringList, { String str ->
            singleEntry str
        }
    }