JSONObject & JSONArray Not Serializable - Work around?

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

JSONObject & JSONArray Not Serializable - Work around?

Chris Cantu
Hello all,

I created a mongo plugin for a grails project.  The project is a basically a REST API which performs CRUD operations and search on a Mongo db.   The plugin leverages the GMongo driver which in turn utilizes the Java Mongo driver.  

When I attempted to store data from JSON REST requests, I quickly realized there was a problem.  The underlying Java Mongo Driver requires all objects to be serializable.  However, org.codehaus.groovy.grails.web.json.JSONObject & JSONArray do not implement Serializable.  Since I could not think of a better approach, I wrote a mapper which converts any object to a serializable Map.

private Map translateObject(Map object){
        def result = [:]        
        object?.each { key, value ->
            if (value instanceof JSONArray) {
                def list = []
                value.each { list << translateObject(it) }
                result."$key" = list
            } else if (value instanceof JSONObject) {
                result."$key" = translateObject(value)
            } else {
                result."$key" = (value == JSONObject.NULL) ? null : value
            }            
        }
       
        return result
  }

Although this approach works, I am not content with the performance or overhead.  I am just wondering if anyone has a better approach to address this problem.  I would appreciate any suggestions.

Thank you,

Chris Cantu



---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email