[json-lib-user] A Not-Null String with literal value 'null' not handled correctly - BUG in JSON-lib
Brought to you by:
aalmiray
From: dev t. <a.d...@gm...> - 2013-10-09 09:46:12
|
A recent issue in our app came up where user submitted string value 'null' is being stored into the data base as '"null"'. Basically double quotes are being added at the start and end of the String. Investigation revealed that JSON-lib doesn't seem to handle the 'null' string value correctly. This is exhibited by the following test methods. @Test public void shouldHandleNullStringInJsonFormattedString() { String jsonTest = "[\"null\",\"aValue\"]"; assertTrue(jsonTest.contains("\"null\"")); assertFalse(jsonTest.contains("\"\\\"null\\\"\"")); String convertedBack = JSONSerializer.toJSON(jsonTest).toString(); assertFalse(convertedBack.contains("\"\\\"null\\\"\"")); } @Test public void shouldHandleNullStringLiteral() { JSONArray jsonArray1 = JSONArray.fromObject(Arrays.asList(null,"b")); JSONArray jsonArray2 = JSONArray.fromObject(Arrays.asList(JSONNull.getInstance(),"b")); JSONArray jsonArray3 = JSONArray.fromObject(Arrays.asList("null","b")); assertEquals("[null,\"b\"]", jsonArray1.toString()); assertEquals("[null,\"b\"]", jsonArray2.toString()); assertEquals("[\"null\",\"b\"]", jsonArray3.toString()); } basically when browser sends ["null", "aValue"] to server, JSON-lib changes it to ["\"null\"", "aValue"]. Also from the server side we are unable to construct a JSON formatted String like ["null", "b"] using JSONArray. JSON-lib does not seem to handle these two basic scenarios properly. JSONUtils.mayBeJSON method treats 'null' string as a possible JSON value. Is this correct ? The issue seems to stem from this logic |