Skip to content

Commit 263a54f

Browse files
committed
Add JsonArray#collector for Stream<@nullable JsonElement>
1 parent 286843d commit 263a54f

3 files changed

Lines changed: 55 additions & 0 deletions

File tree

gson/src/main/java/com/google/gson/JsonArray.java

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

1919
import com.google.errorprone.annotations.CanIgnoreReturnValue;
2020
import com.google.gson.internal.NonNullElementWrapperList;
21+
import org.jspecify.annotations.Nullable;
22+
2123
import java.math.BigDecimal;
2224
import java.math.BigInteger;
2325
import java.util.ArrayList;
2426
import java.util.Iterator;
2527
import java.util.List;
28+
import java.util.stream.Collector;
2629

2730
/**
2831
* A class representing an array type in JSON. An array is a list of {@link JsonElement}s each of
@@ -40,6 +43,25 @@
4043
* @author Joel Leitch
4144
*/
4245
public final class JsonArray extends JsonElement implements Iterable<JsonElement> {
46+
private static final Collector<@Nullable JsonElement, JsonArray, JsonArray> COLLECTOR =
47+
Collector.of(
48+
JsonArray::new,
49+
JsonArray::add,
50+
(left, right) -> {
51+
left.addAll(right);
52+
return left;
53+
});
54+
55+
/**
56+
* Returns a collector that accumulates {@link JsonElement}s into a {@code JsonArray}.
57+
*
58+
* @return a collector that accumulates {@link JsonElement}s into a {@code JsonArray}
59+
* @since $next-version$
60+
*/
61+
public static Collector<@Nullable JsonElement, ?, JsonArray> collector() {
62+
return COLLECTOR;
63+
}
64+
4365
private final ArrayList<JsonElement> elements;
4466

4567
/** Creates an empty JsonArray. */

gson/src/test/java/com/google/gson/JsonArrayTest.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import com.google.common.testing.EqualsTester;
2323
import com.google.gson.common.MoreAsserts;
2424
import java.math.BigInteger;
25+
import java.util.stream.Stream;
2526
import org.junit.Test;
2627

2728
/**
@@ -354,4 +355,29 @@ public void testToString() {
354355
array.add(nestedObject);
355356
assertThat(array.toString()).isEqualTo("[null,NaN,\"a\\u0000\",[\"\\\"\"],{\"n\\u0000\":1}]");
356357
}
358+
359+
@Test
360+
public void testCollector() {
361+
JsonArray array = Stream.of(new JsonPrimitive(1), new JsonArray(), null, new JsonObject())
362+
.collect(JsonArray.collector());
363+
assertThat(array).hasSize(4);
364+
assertThat(array.get(0).getAsInt()).isEqualTo(1);
365+
assertThat(array.get(1).isJsonArray()).isTrue();
366+
assertThat(array.get(2).isJsonNull()).isTrue();
367+
assertThat(array.get(3).isJsonObject()).isTrue();
368+
}
369+
370+
@Test
371+
public void testCollectorParallel() {
372+
JsonArray array = Stream.iterate(0, i -> i + 1)
373+
.limit(20)
374+
.parallel()
375+
.map(JsonPrimitive::new)
376+
.collect(JsonArray.collector());
377+
378+
assertThat(array).hasSize(20);
379+
for (int i = 0; i < 20; i++) {
380+
assertThat(array.get(i).getAsInt()).isEqualTo(i);
381+
}
382+
}
357383
}

pom.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,13 @@
8181

8282
<dependencyManagement>
8383
<dependencies>
84+
<dependency>
85+
<groupId>org.jspecify</groupId>
86+
<artifactId>jspecify</artifactId>
87+
<version>1.0.0</version>
88+
<scope>provided</scope>
89+
</dependency>
90+
8491
<dependency>
8592
<groupId>junit</groupId>
8693
<artifactId>junit</artifactId>

0 commit comments

Comments
 (0)