简体   繁体   中英

How to properly escape backslashes in JSON (using GSON for deserialization)

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:

{

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

}

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:

Caused by: com.google.gson.ParseException: Encountered "\"" at line 1, column 582.
Was expecting one of:
<DIGITS> ...
"null" ...
"NaN" ...
"Infinity" ...
<BOOLEAN> ...
<SINGLE_QUOTE_LITERAL> ...
<DOUBLE_QUOTE_LITERAL> ...
"{" ...
"[" ...
"-" ...

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)

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.

I can manually modify the value in the string to escape the backslash, ie "user":"testd\\\\cest.user" , 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:

Gson gson = new GsonBuilder().disableHtmlEscaping().create();

But that also has no effect. Can someone explain why Gson seems to be attempting to interpret the backslash in the user field?

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.

I'm using Gson 1.3, and cannot update it.

Looks like this is a bug and was fixed in GSON 1.6:

https://code.google.com/p/google-gson/issues/detail?id=264

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM