Skip to content

Commit 5d7704d

Browse files
committed
#211: Handle Collections and Maps containing JsonValues
JsonArrayBuilder and JsonObjectBuilder add methods handle JsonValues and JsonArrayBuilder and JsonObjectBuilder instances, but creating builders using a Collection or Map that contains these values fails, because MapUtil does not handle them and throws an IllegalArgumentException saying that the types are not supported. This commit includes tests that create builders with such values and changes to MapUtil to handle them. (It also refactors MapUtil to avoid unnecessary excessive nesting.) Signed-off-by: Todd O'Bryan <[email protected]> Also-by: Lukas Jungmann <[email protected]>
1 parent aab9d6e commit 5d7704d

File tree

3 files changed

+120
-42
lines changed

3 files changed

+120
-42
lines changed

impl/src/main/java/org/glassfish/json/MapUtil.java

Lines changed: 29 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -38,51 +38,38 @@ private MapUtil() {
3838
}
3939

4040
static JsonValue handle(Object value, BufferPool bufferPool) {
41-
4241
if (value == null) {
4342
return JsonValue.NULL;
44-
}
45-
46-
if (value instanceof BigDecimal) {
43+
} else if (value instanceof JsonValue) {
44+
return (JsonValue) value;
45+
} else if (value instanceof JsonArrayBuilder) {
46+
return ((JsonArrayBuilder) value).build();
47+
} else if (value instanceof JsonObjectBuilder) {
48+
return ((JsonObjectBuilder) value).build();
49+
} else if (value instanceof BigDecimal) {
4750
return JsonNumberImpl.getJsonNumber((BigDecimal) value);
48-
} else {
49-
if (value instanceof BigInteger) {
50-
return JsonNumberImpl.getJsonNumber((BigInteger) value);
51-
} else {
52-
if ( value instanceof Boolean) {
53-
Boolean b = (Boolean) value;
54-
return b ? JsonValue.TRUE : JsonValue.FALSE;
55-
} else {
56-
if (value instanceof Double) {
57-
return JsonNumberImpl.getJsonNumber((Double) value);
58-
} else {
59-
if (value instanceof Integer) {
60-
return JsonNumberImpl.getJsonNumber((Integer) value);
61-
} else {
62-
if (value instanceof Long) {
63-
return JsonNumberImpl.getJsonNumber((Long) value);
64-
} else {
65-
if (value instanceof String) {
66-
return new JsonStringImpl((String) value);
67-
} else {
68-
if (value instanceof Collection) {
69-
@SuppressWarnings("unchecked")
70-
Collection<?> collection = (Collection<?>) value;
71-
JsonArrayBuilder jsonArrayBuilder = new JsonArrayBuilderImpl(collection, bufferPool);
72-
return jsonArrayBuilder.build();
73-
} else {
74-
if (value instanceof Map) {
75-
@SuppressWarnings("unchecked")
76-
JsonObjectBuilder object = new JsonObjectBuilderImpl((Map<String, Object>) value, bufferPool);
77-
return object.build();
78-
}
79-
}
80-
}
81-
}
82-
}
83-
}
84-
}
85-
}
51+
} else if (value instanceof BigInteger) {
52+
return JsonNumberImpl.getJsonNumber((BigInteger) value);
53+
} else if (value instanceof Boolean) {
54+
Boolean b = (Boolean) value;
55+
return b ? JsonValue.TRUE : JsonValue.FALSE;
56+
} else if (value instanceof Double) {
57+
return JsonNumberImpl.getJsonNumber((Double) value);
58+
} else if (value instanceof Integer) {
59+
return JsonNumberImpl.getJsonNumber((Integer) value);
60+
} else if (value instanceof Long) {
61+
return JsonNumberImpl.getJsonNumber((Long) value);
62+
} else if (value instanceof String) {
63+
return new JsonStringImpl((String) value);
64+
} else if (value instanceof Collection) {
65+
@SuppressWarnings("unchecked")
66+
Collection<?> collection = (Collection<?>) value;
67+
JsonArrayBuilder jsonArrayBuilder = new JsonArrayBuilderImpl(collection, bufferPool);
68+
return jsonArrayBuilder.build();
69+
} else if (value instanceof Map) {
70+
@SuppressWarnings("unchecked")
71+
JsonObjectBuilder object = new JsonObjectBuilderImpl((Map<String, Object>) value, bufferPool);
72+
return object.build();
8673
}
8774

8875
throw new IllegalArgumentException(String.format("Type %s is not supported.", value.getClass()));

impl/src/test/java/org/glassfish/json/tests/JsonArrayTest.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import jakarta.json.*;
2222
import java.io.StringReader;
2323
import java.io.StringWriter;
24+
import java.util.ArrayList;
2425
import java.util.List;
2526

2627
/**
@@ -42,6 +43,8 @@ public void testArrayEquals() throws Exception {
4243
.add(Integer.MIN_VALUE)
4344
.add(Long.MIN_VALUE)
4445
.add(Double.MIN_VALUE)
46+
.add(Json.createArrayBuilder().add("abc"))
47+
.add(Json.createObjectBuilder().add("one", 1))
4548
.build();
4649

4750
StringWriter sw = new StringWriter();
@@ -56,6 +59,34 @@ public void testArrayEquals() throws Exception {
5659
assertEquals(expected, actual);
5760
}
5861

62+
public void testArrayEqualsUsingCollection() {
63+
List<Object> list = new ArrayList<>();
64+
list.add(JsonValue.TRUE);
65+
list.add(JsonValue.FALSE);
66+
list.add(JsonValue.NULL);
67+
list.add(Integer.MAX_VALUE);
68+
list.add(Long.MAX_VALUE);
69+
list.add(Double.MAX_VALUE);
70+
list.add(Integer.MIN_VALUE);
71+
list.add(Long.MIN_VALUE);
72+
list.add(Double.MIN_VALUE);
73+
list.add(Json.createArrayBuilder().add("abc"));
74+
list.add(Json.createObjectBuilder().add("one", 1));
75+
76+
JsonArray expected = Json.createArrayBuilder(list).build();
77+
78+
StringWriter sw = new StringWriter();
79+
JsonWriter writer = Json.createWriter(sw);
80+
writer.writeArray(expected);
81+
writer.close();
82+
83+
JsonReader reader = Json.createReader(new StringReader(sw.toString()));
84+
JsonArray actual = reader.readArray();
85+
reader.close();
86+
87+
assertEquals(expected, actual);
88+
}
89+
5990
public void testStringValue() throws Exception {
6091
JsonArray array = Json.createArrayBuilder()
6192
.add("John")

impl/src/test/java/org/glassfish/json/tests/JsonObjectTest.java

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@
1818

1919
import junit.framework.TestCase;
2020

21+
import java.io.StringReader;
22+
import java.io.StringWriter;
23+
import java.util.HashMap;
24+
import java.util.Map;
25+
2126
import jakarta.json.*;
2227

2328
/**
@@ -146,6 +151,61 @@ public void testRemove() {
146151
}
147152
}
148153

154+
public void testObjectBuilderWithVariousValues() {
155+
JsonObject expected = Json.createObjectBuilder()
156+
.add("a", JsonValue.TRUE)
157+
.add("b", JsonValue.FALSE)
158+
.add("c", JsonValue.NULL)
159+
.add("d", Integer.MAX_VALUE)
160+
.add("e", Long.MAX_VALUE)
161+
.add("f", Double.MAX_VALUE)
162+
.add("g", Integer.MIN_VALUE)
163+
.add("h", Long.MIN_VALUE)
164+
.add("i", Double.MIN_VALUE)
165+
.add("j", Json.createArrayBuilder().add("abc"))
166+
.add("k", Json.createObjectBuilder().add("one", 1))
167+
.build();
168+
169+
StringWriter sw = new StringWriter();
170+
JsonWriter writer = Json.createWriter(sw);
171+
writer.writeObject(expected);
172+
writer.close();
173+
174+
JsonReader reader = Json.createReader(new StringReader(sw.toString()));
175+
JsonObject actual = reader.readObject();
176+
reader.close();
177+
178+
assertEquals(expected, actual);
179+
}
180+
181+
public void testObjectBuilderWithMap() {
182+
Map<String, Object> map = new HashMap<>();
183+
map.put("a", JsonValue.TRUE);
184+
map.put("b", JsonValue.FALSE);
185+
map.put("c", JsonValue.NULL);
186+
map.put("d", Integer.MAX_VALUE);
187+
map.put("e", Long.MAX_VALUE);
188+
map.put("f", Double.MAX_VALUE);
189+
map.put("g", Integer.MIN_VALUE);
190+
map.put("h", Long.MIN_VALUE);
191+
map.put("i", Double.MIN_VALUE);
192+
map.put("j", Json.createArrayBuilder().add("abc"));
193+
map.put("k", Json.createObjectBuilder().add("one", 1));
194+
195+
JsonObject expected = Json.createObjectBuilder(map).build();
196+
197+
StringWriter sw = new StringWriter();
198+
JsonWriter writer = Json.createWriter(sw);
199+
writer.writeObject(expected);
200+
writer.close();
201+
202+
JsonReader reader = Json.createReader(new StringReader(sw.toString()));
203+
JsonObject actual = reader.readObject();
204+
reader.close();
205+
206+
assertEquals(expected, actual);
207+
}
208+
149209
public void testObjectBuilderNpe() {
150210
try {
151211
JsonObject obj = Json.createObjectBuilder().add(null, 1).build();

0 commit comments

Comments
 (0)