Skip to content

Commit c9c1140

Browse files
committed
Fixed a bug involving a JSONObject's value being a single other JSONObject, rather than a list.
1 parent 3d09770 commit c9c1140

File tree

5 files changed

+37
-8
lines changed

5 files changed

+37
-8
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ Add this dependency to your `pom.xml`:
1919
<dependency>
2020
<groupId>com.github.stevenlagoy</groupId>
2121
<artifactId>json-java-objectifier</artifactId>
22-
<version>1.0.4</version>
22+
<version>1.0.5</version>
2323
</dependency>
2424
```
2525

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<modelVersion>4.0.0</modelVersion>
55
<groupId>com.github.stevenlagoy</groupId>
66
<artifactId>json-java-objectifier</artifactId>
7-
<version>1.0.4</version>
7+
<version>1.0.5</version>
88
<packaging>jar</packaging>
99
<name>JSON Java Objectifier</name>
1010
<description>Java tools for converting JSON into typed object structures</description>

src/main/java/core/JSONObject.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ public class JSONObject implements Iterable<Object> {
5656
*/
5757
private static boolean isValidJsonType(Object value) {
5858
return (
59+
value == null ||
5960
value instanceof String ||
6061
value instanceof Number ||
6162
value instanceof JSONObject ||
@@ -172,13 +173,15 @@ public Boolean getAsBoolean() {
172173
/** Returns the value of this JSONObject as another JSONObject, or {@code null} if unable. */
173174
public JSONObject getAsObject() {
174175

175-
if (value instanceof JSONObject) return (JSONObject) value;
176+
return this;
176177

177-
if (value instanceof List<?> && type.equals(JSONObject.class)) {
178-
return this;
179-
}
178+
// if (value instanceof JSONObject) return (JSONObject) value;
180179

181-
return null;
180+
// if (value instanceof List<?> && type.equals(JSONObject.class)) {
181+
// return this;
182+
// }
183+
184+
// return null;
182185
}
183186

184187
/** Returns the value of this JSONObject as a List, or throws a ClassCastException if unable. */

src/main/java/core/JSONStringifier.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public static String stringifyObject(JSONObject object) {
7474

7575
if (value == null)
7676
sb.append("null");
77-
else if (type != null && type.equals(JSONObject.class)) {
77+
else if (type != null && type.equals(JSONObject.class) && value instanceof List) {
7878
@SuppressWarnings("unchecked")
7979
List<JSONObject> objects = (List<JSONObject>) value;
8080
sb.append("{");
@@ -85,6 +85,11 @@ else if (type != null && type.equals(JSONObject.class)) {
8585
}
8686
sb.append("}");
8787
}
88+
else if (type != null && value instanceof JSONObject json) {
89+
sb.append("{");
90+
sb.append(json.toString());
91+
sb.append("}");
92+
}
8893
else if (type != null)
8994
sb.append(stringifyValue(value, type));
9095
else

src/tests/java/StringifierTests.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,25 @@ public void stringifyValue() {
4040
assertEquals("{\"key1\" : \"value1\"}{\"key2\" : \"value2\"}", result);
4141
}
4242

43+
@Test
44+
public void stringifyNestedObjects() {
45+
JSONObject json = new JSONObject("root", List.of(new JSONObject("inner_1", "value 1"), new JSONObject("inner_2", "value_2")));
46+
String expected = "\"root\" : {\n" +
47+
"\t\"inner_1\" : \"value 1\",\n" +
48+
"\t\"inner_2\" : \"value_2\"\n" +
49+
"}";
50+
String actual = json.toString();
51+
assertEquals(expected, actual);
52+
}
53+
54+
@Test
55+
public void stringifyNestedSingleObject() {
56+
JSONObject json = new JSONObject("parent", new JSONObject("child", null));
57+
String expected = "\"parent\" : {\n" +
58+
"\t\"child\" : null\n" +
59+
"}";
60+
String actual = json.toString();
61+
assertEquals(expected, actual);
62+
}
63+
4364
}

0 commit comments

Comments
 (0)