开发者

Why does the Groovy JSONBuilder escape slashes in URLs?

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


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"
    }
}'''


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜