From 3d2f7e07ec2ac19ffff334ce87b4a308a09cba89 Mon Sep 17 00:00:00 2001 From: PHJ2000 <105151063+PHJ2000@users.noreply.github.com> Date: Mon, 5 Oct 2026 18:17:59 +0900 Subject: [PATCH] Preserve inherited properties without local entries --- src/main/java/org/json/Property.java | 2 +- .../java/org/json/junit/PropertyTest.java | 29 ++++++++++++++++++- 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/json/Property.java b/src/main/java/org/json/Property.java index ba6c56967..bc5df3895 100644 --- a/src/main/java/org/json/Property.java +++ b/src/main/java/org/json/Property.java @@ -30,7 +30,7 @@ public static JSONObject toJSONObject(java.util.Properties properties) throws JS // can't use the new constructor for Android support // JSONObject jo = new JSONObject(properties == null ? 0 : properties.size()); JSONObject jo = new JSONObject(); - if (properties != null && !properties.isEmpty()) { + if (properties != null) { Enumeration enumProperties = properties.propertyNames(); while(enumProperties.hasMoreElements()) { String name = (String)enumProperties.nextElement(); diff --git a/src/test/java/org/json/junit/PropertyTest.java b/src/test/java/org/json/junit/PropertyTest.java index eee482fbf..268ec0a3f 100644 --- a/src/test/java/org/json/junit/PropertyTest.java +++ b/src/test/java/org/json/junit/PropertyTest.java @@ -17,6 +17,33 @@ */ public class PropertyTest { + @Test + public void shouldIncludeInheritedPropertiesWithoutLocalEntries() { + Properties defaults = new Properties(); + defaults.setProperty("timeout", "30"); + Properties properties = new Properties(new Properties(defaults)); + + JSONObject jsonObject = Property.toJSONObject(properties); + + assertEquals(1, jsonObject.length()); + assertEquals("30", jsonObject.getString("timeout")); + } + + @Test + public void shouldPreferLocalPropertiesOverDefaults() { + Properties defaults = new Properties(); + defaults.setProperty("timeout", "30"); + defaults.setProperty("host", "localhost"); + Properties properties = new Properties(defaults); + properties.setProperty("timeout", "60"); + + JSONObject jsonObject = Property.toJSONObject(properties); + + assertEquals(2, jsonObject.length()); + assertEquals("60", jsonObject.getString("timeout")); + assertEquals("localhost", jsonObject.getString("host")); + } + /** * JSONObject from null properties object should * result in an empty JSONObject. @@ -99,4 +126,4 @@ public void shouldHandleJSONProperty() { assertTrue("property objects should match", properties.equals(jsonProperties)); } -} \ No newline at end of file +}