diff --git a/src/main/java/org/json/JSONML.java b/src/main/java/org/json/JSONML.java index 6ec9970..27fe2d5 100644 --- a/src/main/java/org/json/JSONML.java +++ b/src/main/java/org/json/JSONML.java @@ -205,7 +205,7 @@ public class JSONML { // attribute = value attribute = (String)token; - if (!arrayForm && ("tagName".equals(attribute) || "childNode".equals(attribute))) { + if (!arrayForm && ("tagName".equals(attribute) || "childNodes".equals(attribute))) { throw x.syntaxError("Reserved attribute."); } token = x.nextToken(); diff --git a/src/test/java/org/json/junit/JSONMLTest.java b/src/test/java/org/json/junit/JSONMLTest.java index 93a6821..c83f73e 100644 --- a/src/test/java/org/json/junit/JSONMLTest.java +++ b/src/test/java/org/json/junit/JSONMLTest.java @@ -1052,4 +1052,61 @@ public class JSONMLTest { } } + /** + * Tests that the childNodes attribute is reserved in object-form parsing. + */ + @Test + public void testToJSONObjectRejectsReservedChildNodesAttribute() { + String xmlStr = "
text
"; + for (boolean keepStrings : new boolean[] {false, true}) { + JSONException exception = assertThrows(JSONException.class, + () -> JSONML.toJSONObject(xmlStr, keepStrings)); + assertTrue(exception.getMessage().startsWith("Reserved attribute.")); + } + } + + /** + * Tests that the ordinary childNode attribute is preserved in object-form parsing. + */ + @Test + public void testToJSONObjectPreservesChildNodeAttribute() { + String xmlStr = "text
"; + for (boolean keepStrings : new boolean[] {false, true}) { + JSONObject jsonObject = JSONML.toJSONObject(xmlStr, keepStrings); + assertEquals("p", jsonObject.getString("tagName")); + assertEquals("metadata", jsonObject.getString("childNode")); + assertEquals("text", jsonObject.getJSONArray("childNodes").getString(0)); + } + } + + /** + * Tests that the tagName attribute remains reserved in object-form parsing. + */ + @Test + public void testToJSONObjectRejectsReservedTagNameAttribute() { + String xmlStr = "text
"; + for (boolean keepStrings : new boolean[] {false, true}) { + JSONException exception = assertThrows(JSONException.class, + () -> JSONML.toJSONObject(xmlStr, keepStrings)); + assertTrue(exception.getMessage().startsWith("Reserved attribute.")); + } + } + + /** + * Tests that childNode and childNodes attributes are preserved in array-form parsing. + */ + @Test + public void testToJSONArrayPreservesChildNodeAttributes() { + String xmlStr = "text
"; + for (boolean keepStrings : new boolean[] {false, true}) { + JSONArray jsonArray = JSONML.toJSONArray(xmlStr, keepStrings); + assertEquals(3, jsonArray.length()); + assertEquals("p", jsonArray.getString(0)); + JSONObject attributes = jsonArray.getJSONObject(1); + assertEquals("singular", attributes.getString("childNode")); + assertEquals("plural", attributes.getString("childNodes")); + assertEquals("text", jsonArray.getString(2)); + } + } + }