3

I have a JSON object returned from Groovy's HTTPBuilder. THE JSON contains some null values represented as JSONNull objects. The problem is that when I try to render the JSON back in a response, I get an error when it tries to render the JSONNull. I get a response that is only partially rendered. I want it to render as "null". How do I do this?

Code:

render(contentType: "text/json") {
    listOfJSONObjectsThatIncludeJSONNulls
}

Error:

| Error 2013-09-17 11:33:56,965 [http-bio-8080-exec-4] ERROR errors.GrailsExceptionResolver  - JSONException occurred when processing request: [GET] /my/action
Object is null. Stacktrace follows:
Message: Object is null
   Line | Method
->>  69 | isEmpty        in net.sf.json.JSONNull
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
|   199 | value          in grails.converters.JSON
|   162 | convertAnother in     ''
|   199 | value          in     ''
|   162 | convertAnother in     ''
|   199 | value          in     ''
|   162 | convertAnother in     ''
|   199 | value          in     ''
|   162 | convertAnother in     ''
|   199 | value          in     ''
|   162 | convertAnother in     ''
|   199 | value          in     ''
|   162 | convertAnother in     ''
|   199 | value          in     ''
|   134 | render . . . . in     ''
|   150 | render         in     ''
|    63 | doCall . . . . in myproject.MyController$_index_closure1_closure2_closure4$$EOHirVeS
|   477 | doRequest      in groovyx.net.http.HTTPBuilder
|   417 | doRequest . .  in     ''
|   349 | request        in     ''
|    43 | doCall . . . . in myproject.MyController$_index_closure1$$EOHirVeS
|   477 | doRequest      in groovyx.net.http.HTTPBuilder
|   268 | get . . . . .  in     ''
|    31 | index          in myproject.MyController$$EOHirVeS
|   895 | runTask . . .  in java.util.concurrent.ThreadPoolExecutor$Worker
|   918 | run            in     ''
^   680 | run . . . . .  in java.lang.Thread

Partially Rendered Output:

[{"keyWithNullValue":{"array":false,"class":"net.sf.json.JSONNull"
tim_yates
  • 167,322
  • 27
  • 342
  • 338
Adam
  • 43,763
  • 16
  • 104
  • 144
  • Don't you hate it when someone asks a question and no feedback how he solved the issue or if the solutions provided where right... – Al-Punk Mar 20 '14 at 16:25
  • 1
    @Armand Sorry. I checked my code and found what I ended up doing. I added an answer here. – Adam Mar 21 '14 at 20:20

2 Answers2

7

I used the following code to render JSONNull as an empty string.

grails.converters.JSON.registerObjectMarshaller(JSONNull, { return "" })
Adam
  • 43,763
  • 16
  • 104
  • 144
  • I came across the exact problem today, and thanks to this answer, was able to solve it really quickly. A question I have is: Where do you register the object marshaller as above: just before you render the JSON? Does it have to be done for every method that does so if you expect JSONNulls in the response? – ashes May 05 '14 at 06:04
  • @ashes, I put it in the controller before the response. But, I imagine you could put it someplace more central, I'm not sure where. – Adam May 05 '14 at 17:52
1

I think you can fix it by specifying the below one in BootStrap

JSONObject.NULL.metaClass.asBoolean = {-> false} 

Have a look at: how to get a real null value instead of a JSONObject.NULL value when parsing JSON in grails

Community
  • 1
  • 1
TP_JAVA
  • 1,002
  • 5
  • 23
  • 49