Quantcast

Weird class name trying to JSON render a class

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Weird class name trying to JSON render a class

barry.alexander
Using this code snippet returns a Day object OK, but the class name gets appended with an @ and a string of characters.  What is that?  When I try to render as JSON, it blows chunks.

                Datastore datastore = datastoreService.releaseCalendarDatastore()
               
                Date calendarDate = new SimpleDateFormat("yyyy-MM-dd").parse(params.calDate)
               
                def Day day = datastore.find(Day.class, "relCalDay =", calendarDate).get()
                render day as JSON

Cannot cast object 'com.gap.release.calendar.Day@6371be16' with class 'com.gap.release.calendar.Day' to class 'grails.converters.JSON'. Stacktrace follows:
Message: Cannot cast object 'com.gap.release.calendar.Day@6371be16' with class 'com.gap.release.calendar.Day' to class 'grails.converters.JSON'
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Weird class name trying to JSON render a class

ideasculptor


On Mon, Aug 6, 2012 at 9:24 PM, barry.alexander <[hidden email]> wrote:
Using this code snippet returns a Day object OK, but the class name gets
appended with an @ and a string of characters.  What is that?  When I try to
render as JSON, it blows chunks.

                Datastore datastore = datastoreService.releaseCalendarDatastore()

                Date calendarDate = new
SimpleDateFormat("yyyy-MM-dd").parse(params.calDate)

                def Day day = datastore.find(Day.class, "relCalDay =", calendarDate).get()
                render day as JSON

Cannot cast object 'com.gap.release.calendar.Day@6371be16' with class
'com.gap.release.calendar.Day' to class 'grails.converters.JSON'. Stacktrace
follows:
Message: Cannot cast object 'com.gap.release.calendar.Day@6371be16' with
class 'com.gap.release.calendar.Day' to class 'grails.converters.JSON'



The JSON converter doesn't know how to deal with your Day.class object.  You need to register a converter for that class when you bootstrap your app.

        JSON.registerObjectMarshaller(Polygon) {
            return new WKTWriter().write(it);
        }
        JSON.registerObjectMarshaller(EspaceStyle) {
            return it.name()
        }
        JSON.registerObjectMarshaller(DeviceType) {
            return it.name()
        }

In the examples above, any property of type Polygon, EspaceStyle, or DeviceType will be written to JSON by executing the supplied closure.  So long as you return an object that JSON knows how to convert, it'll handle the rest, so if you have a complex object, just return a Map.  I'm sure you can find lots of info by googling registerObjectMarshaller or looking at the docs for the JSON object.


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Weird class name trying to JSON render a class

barry.alexander
I also found out that I can use encodeAsJSON on my Day object

        def listDay = {
                Datastore datastore = datastoreService.releaseCalendarDatastore()
               
                Date calendarDate = new SimpleDateFormat("yyyy-MM-dd").parse(params.calDate)
               
                def Day day = datastore.find(Day.class, "relCalDay =", calendarDate).get()
                render day.encodeAsJSON()
        }

This returns:

{"class":"com.gap.release.calendar.Day","id":{"class":"org.bson.types.ObjectId","inc":994497492,"machine":56910623,"new":false,"time":1344198578000,"timeSecond":1344198578},"iterationDay":2,"iterationNumber":216,"relCalDay":"2012-08-13T07:00:00Z","release":"12.09","releaseId":{"class":"org.bson.types.ObjectId","inc":994497460,"machine":56910623,"new":false,"time":1344198578000,"timeSecond":1344198578},"version":0}
Loading...