Reputation: 23
I am writing a Groovy script that needs to POST JSON to a URL. I have noticed a problem were all elements in my JSON that contains a '/' are changed to '\/' by the JSON Builder. Is there a way to stop this?
This is using Groovy 1.8. Here is a simple example and its output:
def json = new JsonBuilder()
json.reply {
result 'http://google.ie/testing'
}
println json.toString()
Output -> {"reply":{"result":"http:\/\/google.ie\/testing"}}
Thanks
Upvotes: 2
Views: 4146
Reputation: 66943
Why does the Groovy JSONBuilder escape slashes in URLs?
An excerpt of the interesting points made in http://groups.google.com/group/opensocial-and-gadgets-spec/browse_thread/thread/1642ec0bed9b95ba/21956eed23f04e13?pli=1 on this topic:
Arne Roomann-Kurrik: Per the JSON spec, escaping '/' is optional.
Mike Samuel: The solidus is among the set of characters that MAY be escaped so that it is safe to embed the JSON substring </script>
in HTML as <\/script>
. (Half of this quote is by Andrea Ercolino.)
Kevin Brown: This is primarily due to buggy javascript parsers that treat // as a comment when it's in a string.
Upvotes: 1
Reputation: 171104
Just had a look, and groovy.json.JsonOuput.toJson(string)
encodes forward slash as '\\/'
.
You can use toPrettyString
and it doesn't do it however:
def json = new groovy.json.JsonBuilder()
json.reply {
result 'http://google.ie/testing'
}
assert json.toPrettyString() == '''{
"reply": {
"result": "http://google.ie/testing"
}
}'''
Upvotes: 2