Skip to content

Commit 6cca292

Browse files
committed
investigate XML.toString() behavior with null JSONObject values
1 parent ab143af commit 6cca292

1 file changed

Lines changed: 83 additions & 0 deletions

File tree

JSONObjectTest.java

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1469,6 +1469,89 @@ public void equals() {
14691469
assertTrue("Same JSONObject should be equal to itself",
14701470
aJsonObject.equals(aJsonObject));
14711471
}
1472+
1473+
@Test
1474+
public void jsonObjectNullOperations() {
1475+
/**
1476+
* The Javadoc for JSONObject.NULL states:
1477+
* "JSONObject.NULL is equivalent to the value that JavaScript calls null,
1478+
* whilst Java's null is equivalent to the value that JavaScript calls
1479+
* undefined."
1480+
*
1481+
* Standard ECMA-262 6th Edition / June 2015 (included to help explain the javadoc):
1482+
* undefined value: primitive value used when a variable has not been assigned a value
1483+
* Undefined type: type whose sole value is the undefined value
1484+
* null value: primitive value that represents the intentional absence of any object value
1485+
* Null type: type whose sole value is the null value
1486+
* Java SE8 language spec (included to help explain the javadoc):
1487+
* The Kinds of Types and Values ...
1488+
* There is also a special null type, the type of the expression null, which has no name.
1489+
* Because the null type has no name, it is impossible to declare a variable of the null
1490+
* type or to cast to the null type. The null reference is the only possible value of an
1491+
* expression of null type. The null reference can always be assigned or cast to any reference type.
1492+
* In practice, the programmer can ignore the null type and just pretend that null is merely
1493+
* a special literal that can be of any reference type.
1494+
* Extensible Markup Language (XML) 1.0 Fifth Edition / 26 November 2008
1495+
* No mention of null
1496+
* ECMA-404 1st Edition / October 2013:
1497+
* JSON Text ...
1498+
* These are three literal name tokens: ...
1499+
* null
1500+
*
1501+
* There seems to be no best practice, it's all about what we want the code to do.
1502+
* In the code we see that JSONObject.NULL is tranformed into null
1503+
*/
1504+
1505+
// add JSONObject.NULL then convert to string in the manner of XML.toString()
1506+
JSONObject jsonObjectJONull = new JSONObject();
1507+
Object obj = JSONObject.NULL;
1508+
jsonObjectJONull.put("key", obj);
1509+
Object value = jsonObjectJONull.opt("key");
1510+
assertTrue("opt() JSONObject.NULL should find JSONObject.NULL", obj.equals(value));
1511+
value = jsonObjectJONull.get("key");
1512+
assertTrue("get() JSONObject.NULL should find JSONObject.NULL", obj.equals(value));
1513+
if (value == null) {
1514+
value = "";
1515+
}
1516+
String string = value instanceof String ? (String)value : null;
1517+
assertTrue("XML toString() should convert JSONObject.NULL to null", string == null);
1518+
1519+
// now try it with null
1520+
JSONObject jsonObjectNull = new JSONObject();
1521+
obj = null;
1522+
jsonObjectNull.put("key", obj);
1523+
value = jsonObjectNull.opt("key");
1524+
assertTrue("opt() null should find null", value == null);;
1525+
if (value == null) {
1526+
value = "";
1527+
}
1528+
string = value instanceof String ? (String)value : null;
1529+
assertTrue("should convert null to empty string", "".equals(string));
1530+
try {
1531+
value = jsonObjectNull.get("key");
1532+
assertTrue("get() null should throw exception", false);
1533+
} catch (Exception ignored) {}
1534+
1535+
/**
1536+
* XML.toString() then goes on to do something with the value
1537+
* if the key val is "content", then value.toString() will be
1538+
* called. This will evaluate to "null" for JSONObject.NULL,
1539+
* and the empty string for null.
1540+
* But if the key is anything else, then JSONObject.NULL will be emitted as <key>null</key>
1541+
* and null will be emitted as ""
1542+
*/
1543+
String sJONull = XML.toString(jsonObjectJONull);
1544+
assertTrue("JSONObject.NULL should emit a null value", "<key>null</key>".equals(sJONull));
1545+
String sNull = XML.toString(jsonObjectNull);
1546+
assertTrue("null should emit an empty string", "".equals(sNull));
1547+
}
1548+
1549+
/**
1550+
*
1551+
*/
1552+
private void nullOperations(Object value) {
1553+
}
1554+
14721555
}
14731556

14741557

0 commit comments

Comments
 (0)