1
0
Fork 0
mirror of https://github.com/ethauvin/JSON-java.git synced 2025-06-17 07:50:52 -07:00

investigate XML.toString() behavior with null JSONObject values

This commit is contained in:
stleary 2015-07-17 22:04:01 -05:00
parent b06182cb73
commit 16fa69c0f6

View file

@ -1498,7 +1498,8 @@ public class JSONObjectTest {
* These are three literal name tokens: ... * These are three literal name tokens: ...
* null * null
* *
* There seems to be no best practice to follow, it's all about what we want the code to do. * There seems to be no best practice to follow, it's all about what we
* want the code to do.
*/ */
// add JSONObject.NULL then convert to string in the manner of XML.toString() // add JSONObject.NULL then convert to string in the manner of XML.toString()
@ -1506,14 +1507,17 @@ public class JSONObjectTest {
Object obj = JSONObject.NULL; Object obj = JSONObject.NULL;
jsonObjectJONull.put("key", obj); jsonObjectJONull.put("key", obj);
Object value = jsonObjectJONull.opt("key"); Object value = jsonObjectJONull.opt("key");
assertTrue("opt() JSONObject.NULL should find JSONObject.NULL", obj.equals(value)); assertTrue("opt() JSONObject.NULL should find JSONObject.NULL",
obj.equals(value));
value = jsonObjectJONull.get("key"); value = jsonObjectJONull.get("key");
assertTrue("get() JSONObject.NULL should find JSONObject.NULL", obj.equals(value)); assertTrue("get() JSONObject.NULL should find JSONObject.NULL",
obj.equals(value));
if (value == null) { if (value == null) {
value = ""; value = "";
} }
String string = value instanceof String ? (String)value : null; String string = value instanceof String ? (String)value : null;
assertTrue("XML toString() should convert JSONObject.NULL to null", string == null); assertTrue("XML toString() should convert JSONObject.NULL to null",
string == null);
// now try it with null // now try it with null
JSONObject jsonObjectNull = new JSONObject(); JSONObject jsonObjectNull = new JSONObject();
@ -1536,11 +1540,12 @@ public class JSONObjectTest {
* if the key val is "content", then value.toString() will be * if the key val is "content", then value.toString() will be
* called. This will evaluate to "null" for JSONObject.NULL, * called. This will evaluate to "null" for JSONObject.NULL,
* and the empty string for null. * and the empty string for null.
* But if the key is anything else, then JSONObject.NULL will be emitted as <key>null</key> * But if the key is anything else, then JSONObject.NULL will be emitted
* and null will be emitted as "" * as <key>null</key> and null will be emitted as ""
*/ */
String sJONull = XML.toString(jsonObjectJONull); String sJONull = XML.toString(jsonObjectJONull);
assertTrue("JSONObject.NULL should emit a null value", "<key>null</key>".equals(sJONull)); assertTrue("JSONObject.NULL should emit a null value",
"<key>null</key>".equals(sJONull));
String sNull = XML.toString(jsonObjectNull); String sNull = XML.toString(jsonObjectNull);
assertTrue("null should emit an empty string", "".equals(sNull)); assertTrue("null should emit an empty string", "".equals(sNull));
} }