Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GSON issue with String

    String s = "m\\"+"/m\\/m/m/m/m/m";      LinkedHashMap<String, String> hm = new LinkedHashMap<>();      hm.put("test", s);      System.out.println(hm+"  Hash map = "+hm.toString()); 

Fine Output is {test=m\/m\/m/m/m/m/m} Hash map = {test=m\/m\/m/m/m/m/m}

    String s2 = new Gson().toJson(hm.toString());      System.out.println("Json result is "+s2); 

Not Fine Output is Json result is "{test\u003dm\\/m\\/m/m/m/m/m}"

Is GSON going mad or is it something that I am doing wrong? What is happening to with Back Slashes and from where does this u003d appears? I knew that there exists a bug of this nature a long time ago but it was resolved. How can I resolve this issue? Thanks in advance.

like image 611
Fahad Ishaque Avatar asked May 15 '13 06:05

Fahad Ishaque


1 Answers

The = sign is encoded to \u003d. Hence you need to use disableHtmlEscaping().

You can use

Gson gson = new GsonBuilder().disableHtmlEscaping().create(); String s2 = gson.toJson(hm.toString()); 

For \/ turning into \\/ issue, the solution is

s2.replace("\\\\", "\\"); 
like image 170
AllTooSir Avatar answered Sep 20 '22 04:09

AllTooSir