Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Switch off UTF escaping in JsonBuilder

I'm trying to use JsonBuilder in groovy servlet (extending HttpServlet) Here is a snippet:

 public void doGet(HttpServletRequest request, HttpServletResponse response) {
     response.setContentType('text/plain')
     response.setCharacterEncoding('utf-8')

     def pw = response.getWriter()
     pw.println(new JsonBuilder(['city': 'Москва']))
     pw.println([сity: 'Москва'])
  }

The output is

{"city":"\u041C\u043E\u0441\u043A\u0432\u0430"}
{сity=Москва}

I just don't know nothing about UTF escaping in JsonBuilder, googling also did not give my anything valuable. So I guess I'm stuck.

Does anybody know how to get the output for json exactly in same form as we get the output for regular groovy object?

like image 486
shabunc Avatar asked Mar 21 '12 14:03

shabunc


1 Answers

I have encountered the same issue, and the above methods didn't work. However this did: http://groovy.codehaus.org/gapi/groovy/json/StringEscapeUtils.html

StringEscapeUtils.unescapeJavaScript(JsonOutput.toJson('Москва'))
like image 68
Tegi Avatar answered Sep 17 '22 12:09

Tegi