Skip to content

Commit babc520

Browse files
authored
Merge pull request stleary#1084 from zemiles/master
Fix reserved childNodes attribute check in JSONML
2 parents 18e8ef6 + d38bff8 commit babc520

2 files changed

Lines changed: 63 additions & 4 deletions

File tree

‎src/main/java/org/json/JSONML.java‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
*/
1515
public class JSONML {
1616

17+
private static final String CHILD_NODES = "childNodes";
18+
1719
/**
1820
* Constructs a new JSONML object.
1921
* @deprecated (Utility class cannot be instantiated)
@@ -205,7 +207,7 @@ private static Object parse(
205207
// attribute = value
206208

207209
attribute = (String)token;
208-
if (!arrayForm && ("tagName".equals(attribute) || "childNode".equals(attribute))) {
210+
if (!arrayForm && ("tagName".equals(attribute) || CHILD_NODES.equals(attribute))) {
209211
throw x.syntaxError("Reserved attribute.");
210212
}
211213
token = x.nextToken();
@@ -256,7 +258,7 @@ private static Object parse(
256258
}
257259
tagName = null;
258260
if (!arrayForm && newja.length() > 0) {
259-
newjo.put("childNodes", newja);
261+
newjo.put(CHILD_NODES, newja);
260262
}
261263
if (ja == null) {
262264
if (arrayForm) {
@@ -648,7 +650,7 @@ public static String toString(JSONObject jo) throws JSONException {
648650

649651
// Don't use the new entrySet API to maintain Android support
650652
for (final String key : jo.keySet()) {
651-
if (!"tagName".equals(key) && !"childNodes".equals(key)) {
653+
if (!"tagName".equals(key) && !CHILD_NODES.equals(key)) {
652654
XML.noSpace(key);
653655
value = jo.opt(key);
654656
if (value != null) {
@@ -664,7 +666,7 @@ public static String toString(JSONObject jo) throws JSONException {
664666

665667
//Emit content in body
666668

667-
ja = jo.optJSONArray("childNodes");
669+
ja = jo.optJSONArray(CHILD_NODES);
668670
if (ja == null) {
669671
sb.append('/');
670672
sb.append('>');

‎src/test/java/org/json/junit/JSONMLTest.java‎

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1052,4 +1052,61 @@ public void testValidXMLToJSONObjectStillWorks() {
10521052
}
10531053
}
10541054

1055+
/**
1056+
* Tests that the childNodes attribute is reserved in object-form parsing.
1057+
*/
1058+
@Test
1059+
public void testToJSONObjectRejectsReservedChildNodesAttribute() {
1060+
String xmlStr = "<p childNodes=\"metadata\">text</p>";
1061+
for (boolean keepStrings : new boolean[] {false, true}) {
1062+
JSONException exception = assertThrows(JSONException.class,
1063+
() -> JSONML.toJSONObject(xmlStr, keepStrings));
1064+
assertTrue(exception.getMessage().startsWith("Reserved attribute."));
1065+
}
1066+
}
1067+
1068+
/**
1069+
* Tests that the ordinary childNode attribute is preserved in object-form parsing.
1070+
*/
1071+
@Test
1072+
public void testToJSONObjectPreservesChildNodeAttribute() {
1073+
String xmlStr = "<p childNode=\"metadata\">text</p>";
1074+
for (boolean keepStrings : new boolean[] {false, true}) {
1075+
JSONObject jsonObject = JSONML.toJSONObject(xmlStr, keepStrings);
1076+
assertEquals("p", jsonObject.getString("tagName"));
1077+
assertEquals("metadata", jsonObject.getString("childNode"));
1078+
assertEquals("text", jsonObject.getJSONArray("childNodes").getString(0));
1079+
}
1080+
}
1081+
1082+
/**
1083+
* Tests that the tagName attribute remains reserved in object-form parsing.
1084+
*/
1085+
@Test
1086+
public void testToJSONObjectRejectsReservedTagNameAttribute() {
1087+
String xmlStr = "<p tagName=\"metadata\">text</p>";
1088+
for (boolean keepStrings : new boolean[] {false, true}) {
1089+
JSONException exception = assertThrows(JSONException.class,
1090+
() -> JSONML.toJSONObject(xmlStr, keepStrings));
1091+
assertTrue(exception.getMessage().startsWith("Reserved attribute."));
1092+
}
1093+
}
1094+
1095+
/**
1096+
* Tests that childNode and childNodes attributes are preserved in array-form parsing.
1097+
*/
1098+
@Test
1099+
public void testToJSONArrayPreservesChildNodeAttributes() {
1100+
String xmlStr = "<p childNode=\"singular\" childNodes=\"plural\">text</p>";
1101+
for (boolean keepStrings : new boolean[] {false, true}) {
1102+
JSONArray jsonArray = JSONML.toJSONArray(xmlStr, keepStrings);
1103+
assertEquals(3, jsonArray.length());
1104+
assertEquals("p", jsonArray.getString(0));
1105+
JSONObject attributes = jsonArray.getJSONObject(1);
1106+
assertEquals("singular", attributes.getString("childNode"));
1107+
assertEquals("plural", attributes.getString("childNodes"));
1108+
assertEquals("text", jsonArray.getString(2));
1109+
}
1110+
}
1111+
10551112
}

0 commit comments

Comments
 (0)