Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JsonArray optimizations #5002

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 38 additions & 15 deletions src/main/java/io/vertx/core/json/JsonArray.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand All @@ -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);
}

/**
Expand All @@ -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.
*
Expand All @@ -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);
}
Expand All @@ -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]);
}

Expand Down Expand Up @@ -616,7 +640,13 @@ public JsonArray copy(Function<Object, ?> cloner) {
* @return a Stream
*/
public Stream<Object> stream() {
return asStream(iterator());
// JsonUtil.asStream(iterator()) is too generic
return StreamSupport.stream(spliterator(), false);
}

@Override
public Spliterator<Object> spliterator() {
return Spliterators.spliterator(iterator(), list.size(), Spliterator.ORDERED);
}

@Override
Expand Down Expand Up @@ -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<Object> {

Expand Down
34 changes: 34 additions & 0 deletions src/test/java/io/vertx/core/json/JsonArrayTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
Loading