diff --git a/src/main/java/io/vertx/core/json/JsonArray.java b/src/main/java/io/vertx/core/json/JsonArray.java index 4d9bcc60862..d2d5b6e0a60 100644 --- a/src/main/java/io/vertx/core/json/JsonArray.java +++ b/src/main/java/io/vertx/core/json/JsonArray.java @@ -19,6 +19,7 @@ import java.util.*; import java.util.function.Function; import java.util.stream.Stream; +import java.util.stream.StreamSupport; import static io.vertx.core.json.impl.JsonUtil.*; import static java.time.format.DateTimeFormatter.ISO_INSTANT; @@ -52,7 +53,7 @@ public JsonArray(String json) { if (json == null) { throw new NullPointerException(); } - fromJson(json); + list = Json.CODEC.fromString(json, List.class); if (list == null) { throw new DecodeException("Invalid JSON array: " + json); } @@ -62,7 +63,14 @@ public JsonArray(String json) { * Create an empty instance */ public JsonArray() { - list = new ArrayList<>(); + list = new ArrayList<>();// empty, see ArrayList.DEFAULTCAPACITY_EMPTY_ELEMENTDATA + } + + /** + * Create an empty instance with initialCapacity + */ + public JsonArray(int initialCapacity) { + list = new ArrayList<>(initialCapacity); } /** @@ -77,6 +85,21 @@ public JsonArray(List list) { this.list = list; } + /** + * Create an instance from a collections. If the collection is a list, it is not copied. + * + * @param collection collection to copy or use as the underlying backing list + */ + public JsonArray(Collection collection) { + if (collection instanceof List) { + list = (List) collection; + } else if (collection == null) { + throw new NullPointerException(); + } else { + list = new ArrayList(collection); + } + } + /** * Create an instance from a Buffer of JSON. * @@ -86,7 +109,7 @@ public JsonArray(Buffer buf) { if (buf == null) { throw new NullPointerException(); } - fromBuffer(buf); + list = Json.CODEC.fromBuffer(buf, List.class); if (list == null) { throw new DecodeException("Invalid JSON array: " + buf); } @@ -100,12 +123,13 @@ public JsonArray(Buffer buf) { */ public static JsonArray of(Object... values) { // implicit nullcheck of values - if (values.length == 0) { + int len = values.length; + if (len == 0) { return new JsonArray(); } - JsonArray arr = new JsonArray(new ArrayList<>(values.length)); - for(int i = 0; i< values.length; ++i) { + JsonArray arr = new JsonArray(new ArrayList<>(len)); + for (int i = 0; i < len; ++i) { arr.add(values[i]); } @@ -616,7 +640,13 @@ public JsonArray copy(Function cloner) { * @return a Stream */ public Stream stream() { - return asStream(iterator()); + // JsonUtil.asStream(iterator()) is too generic + return StreamSupport.stream(spliterator(), false); + } + + @Override + public Spliterator spliterator() { + return Spliterators.spliterator(iterator(), list.size(), Spliterator.ORDERED); } @Override @@ -704,17 +734,10 @@ public int readFromBuffer(int pos, Buffer buffer) { int length = buffer.getInt(pos); int start = pos + 4; Buffer buf = buffer.getBuffer(start, start + length); - fromBuffer(buf); + list = Json.CODEC.fromBuffer(buf, List.class); return pos + length + 4; } - private void fromJson(String json) { - list = Json.CODEC.fromString(json, List.class); - } - - private void fromBuffer(Buffer buf) { - list = Json.CODEC.fromBuffer(buf, List.class); - } private static class Iter implements Iterator { diff --git a/src/test/java/io/vertx/core/json/JsonArrayTest.java b/src/test/java/io/vertx/core/json/JsonArrayTest.java index 79dcb6640e2..47c922d293a 100644 --- a/src/test/java/io/vertx/core/json/JsonArrayTest.java +++ b/src/test/java/io/vertx/core/json/JsonArrayTest.java @@ -1449,4 +1449,38 @@ public void testJsonArrayOfArgs() { public void testJsonArrayOfEmpty() { assertEquals(new JsonArray(), JsonArray.of()); } + + @Test + public void testPR() { + JsonArray ja = new JsonArray(JsonArray.of(null, null).stream() + .collect(Collectors.toSet())); + assertEquals("[null]", ja.toString()); + assertEquals("[null]", ja.toBuffer().toString()); + + ja.remove(null); + assertEquals(0, ja.size()); + + ja = new JsonArray(new ArrayList<>(Arrays.asList("a", null, Instant.MIN, Arrays.asList(1,2)))); + assertEquals("[\"a\",null,\"-1000000000-01-01T00:00:00Z\",[1,2]]", ja.toString()); + + Buffer buf = Buffer.buffer(); + buf.appendLong(Long.MIN_VALUE); + ja.writeToBuffer(buf); + JsonArray ja2 = new JsonArray(); + ja2.readFromBuffer(8, buf); + assertEquals(ja, ja2); + + // test remove + assertEquals(4, ja.size()); + + ja.remove(null); + assertEquals(3, ja.size()); + + ja.remove(Instant.MIN); + assertEquals(2, ja.size()); + + ja.remove(Arrays.asList(1,2)); + assertEquals(1, ja.size()); + assertEquals("[\"a\"]", ja.toString()); + } }