title: How to properly escape backslashes in JSON (using GSON for deserialization)
body: <p>I've seen similar questions, but none seem to address the exact issue I'm encountering. I'm dealing with the deserialization of JSON strings, that look like this:</p>
  
  <p>{</p>
  
  <pre><code>"dst_longitude":0,
  "alert_type":"watchlist",
  "dst_location":"unknown",
  "app":"testapp",
  "suppression_end_time":1309561548,
  "dstip":"",
  "app_session_id":44412,
  "client_bytes":0,
  "numbytes":0,
  "fromlogs":"yes",
  "dst_region":"unknown",
  "conn_duration":0,
  "dst_country":"unknown",
  "resp_cnt":8,
  "device":"unknown",
  "src_zipcode":"unknown",
  "req_cnt":8,
  "policy":"policy1",
  "server_bytes":0,
  "dstport":443,
  "type":"nspolicy",
  "access_method":"sophos",
  "dsthost":"dsttst",
  "dst_latitude":0,
  "timestamp":1309561548,
  "src_region":"unknown",
  "acked":"false",
  "suppression_start_time":1409561333,
  "alert":"yes",
  "user":"testd\cest.user",
  "srcip":"10.10.10.10",
  "org":"",
  "src_country":"unknown",
  "src_location":"unknown",
  "appcategory":"testapp",
  "src_latitude":0,
  "count":8,
  "dst_zipcode":"unknown",
  "url":"",
  "ccl":"medium",
  "alert_name":"testalert",
  "src_longitude":0,
  "_id":"544fd22eba91345ef252b21b"
  ,"os":"unknown",
  "browser":"unknown"
  </code></pre>
  
  <p>}</p>
  
  <p>The problem appears to be in the backslash that separates the 'user' field into the domain and username tokens. Gson abruptly stops the parsing attempt and shows this error:</p>
  
  <pre><code>Caused by: com.google.gson.ParseException: Encountered "\"" at line 1, column 582.
  Was expecting one of:
  &lt;DIGITS&gt; ...
  "null" ...
  "NaN" ...
  "Infinity" ...
  &lt;BOOLEAN&gt; ...
  &lt;SINGLE_QUOTE_LITERAL&gt; ...
  &lt;DOUBLE_QUOTE_LITERAL&gt; ...
  "{" ...
  "[" ...
  "-" ...
  
  at com.google.gson.JsonParserJavacc.generateParseException(JsonParserJavacc.java:658)
  at com.google.gson.JsonParserJavacc.jj_consume_token(JsonParserJavacc.java:540)
  at com.google.gson.JsonParserJavacc.JsonValue(JsonParserJavacc.java:182)
  at com.google.gson.JsonParserJavacc.Pair(JsonParserJavacc.java:89)
  </code></pre>
  
  <p>The error is somewhat strange... It actually points to the first character in the 'user' field's value as the problem, which is claims is an escaped quote. The leading character in that field, while although a quote, is not escaped, as you can see above. Through experimentation I was able to discover that the backslash in the middle of the value is actually what is causing the error.</p>
  
  <p>I can manually modify the value in the string to escape the backslash, ie <code>"user":"testd\\cest.user"</code>, but that is not really a solution for me, as I don't control the data-source in any way (I'm just testing some sample data through a unit test at this point). I've also tried modifying my Gson creation to disable html escaping, ie:</p>
  
  <pre><code>Gson gson = new GsonBuilder().disableHtmlEscaping().create();
  </code></pre>
  
  <p>But that also has no effect. Can someone explain why Gson seems to be attempting to interpret the backslash in the user field?</p>
  
  <p>I can upload the code that I use to deserialize the JSON, but it isn't very complex or interesting, and it works for values that do not contain backslashes in the fields, so I don't think my code has any actual problems.</p>
  
  <p>I'm using Gson 1.3, and cannot update it.</p>