Skip to content

Commit 5e9b4f4

Browse files
authored
Merge pull request #1087 from PHJ2000/fix-properties-defaults-only
Fix Property enumeration without local entries
2 parents 6527427 + 3d2f7e0 commit 5e9b4f4

2 files changed

Lines changed: 29 additions & 2 deletions

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public static JSONObject toJSONObject(java.util.Properties properties) throws JS
3030
// can't use the new constructor for Android support
3131
// JSONObject jo = new JSONObject(properties == null ? 0 : properties.size());
3232
JSONObject jo = new JSONObject();
33-
if (properties != null && !properties.isEmpty()) {
33+
if (properties != null) {
3434
Enumeration<?> enumProperties = properties.propertyNames();
3535
while(enumProperties.hasMoreElements()) {
3636
String name = (String)enumProperties.nextElement();

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

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,33 @@
1717
*/
1818
public class PropertyTest {
1919

20+
@Test
21+
public void shouldIncludeInheritedPropertiesWithoutLocalEntries() {
22+
Properties defaults = new Properties();
23+
defaults.setProperty("timeout", "30");
24+
Properties properties = new Properties(new Properties(defaults));
25+
26+
JSONObject jsonObject = Property.toJSONObject(properties);
27+
28+
assertEquals(1, jsonObject.length());
29+
assertEquals("30", jsonObject.getString("timeout"));
30+
}
31+
32+
@Test
33+
public void shouldPreferLocalPropertiesOverDefaults() {
34+
Properties defaults = new Properties();
35+
defaults.setProperty("timeout", "30");
36+
defaults.setProperty("host", "localhost");
37+
Properties properties = new Properties(defaults);
38+
properties.setProperty("timeout", "60");
39+
40+
JSONObject jsonObject = Property.toJSONObject(properties);
41+
42+
assertEquals(2, jsonObject.length());
43+
assertEquals("60", jsonObject.getString("timeout"));
44+
assertEquals("localhost", jsonObject.getString("host"));
45+
}
46+
2047
/**
2148
* JSONObject from null properties object should
2249
* result in an empty JSONObject.
@@ -99,4 +126,4 @@ public void shouldHandleJSONProperty() {
99126
assertTrue("property objects should match",
100127
properties.equals(jsonProperties));
101128
}
102-
}
129+
}

0 commit comments

Comments
 (0)