From 53da5ce2a96bb5aef7316becf60269240cfe386f Mon Sep 17 00:00:00 2001 From: marilynel Date: Sun, 6 Apr 2025 11:04:33 -0700 Subject: [PATCH] adjusted keepstrings behavior to reflect changes in keepBooleanAsString & keepNumberAsString --- .../java/org/json/XMLParserConfiguration.java | 3 +++ .../org/json/junit/XMLConfigurationTest.java | 25 +++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/src/main/java/org/json/XMLParserConfiguration.java b/src/main/java/org/json/XMLParserConfiguration.java index cbcb1de..de84b90 100644 --- a/src/main/java/org/json/XMLParserConfiguration.java +++ b/src/main/java/org/json/XMLParserConfiguration.java @@ -224,6 +224,7 @@ public class XMLParserConfiguration extends ParserConfiguration { @Override public XMLParserConfiguration withKeepStrings(final boolean newVal) { XMLParserConfiguration newConfig = this.clone(); + newConfig.keepStrings = newVal; newConfig.keepNumberAsString = newVal; newConfig.keepBooleanAsString = newVal; return newConfig; @@ -241,6 +242,7 @@ public class XMLParserConfiguration extends ParserConfiguration { public XMLParserConfiguration withKeepNumberAsString(final boolean newVal) { XMLParserConfiguration newConfig = this.clone(); newConfig.keepNumberAsString = newVal; + newConfig.keepStrings = newConfig.keepBooleanAsString && newConfig.keepNumberAsString; return newConfig; } @@ -256,6 +258,7 @@ public class XMLParserConfiguration extends ParserConfiguration { public XMLParserConfiguration withKeepBooleanAsString(final boolean newVal) { XMLParserConfiguration newConfig = this.clone(); newConfig.keepBooleanAsString = newVal; + newConfig.keepStrings = newConfig.keepBooleanAsString && newConfig.keepNumberAsString; return newConfig; } diff --git a/src/test/java/org/json/junit/XMLConfigurationTest.java b/src/test/java/org/json/junit/XMLConfigurationTest.java index 44f486f..938c7c8 100755 --- a/src/test/java/org/json/junit/XMLConfigurationTest.java +++ b/src/test/java/org/json/junit/XMLConfigurationTest.java @@ -794,6 +794,31 @@ public class XMLConfigurationTest { Util.compareActualVsExpectedJsonObjects(actualJsonOutput,expected); } + /** + * Test keepStrings behavior when setting keepBooleanAsString, keepNumberAsString + */ + @Test + public void test_keepStringBehavior() { + XMLParserConfiguration xpc = new XMLParserConfiguration().withKeepStrings(true); + assertEquals(xpc.isKeepStrings(), true); + + xpc = xpc.withKeepBooleanAsString(true); + xpc = xpc.withKeepNumberAsString(false); + assertEquals(xpc.isKeepStrings(), false); + + xpc = xpc.withKeepBooleanAsString(false); + xpc = xpc.withKeepNumberAsString(true); + assertEquals(xpc.isKeepStrings(), false); + + xpc = xpc.withKeepBooleanAsString(true); + xpc = xpc.withKeepNumberAsString(true); + assertEquals(xpc.isKeepStrings(), true); + + xpc = xpc.withKeepBooleanAsString(false); + xpc = xpc.withKeepNumberAsString(false); + assertEquals(xpc.isKeepStrings(), false); + } + /** * JSON string cannot be reverted to original xml. */