Skip to content
This repository has been archived by the owner on Oct 31, 2024. It is now read-only.

Commit

Permalink
Wrap arrays in List wrappers instead of creating copies in Adapter#to…
Browse files Browse the repository at this point in the history
…List

This is a neat performance improvement with no real cost. Instead of creating a new list and adding all elements to it, a new list wrapper is created that accesses the underlying array object directly.

I haven't figured out if it's possible to do the same for the object-to-list case.
  • Loading branch information
iconara committed Aug 23, 2019
1 parent 6e3d505 commit 9edf6ca
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 12 deletions.
36 changes: 25 additions & 11 deletions jmespath-gson/src/main/java/io/burt/jmespath/gson/GsonRuntime.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import io.burt.jmespath.JmesPathType;
import io.burt.jmespath.RuntimeConfiguration;

import java.util.AbstractList;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
Expand All @@ -34,24 +35,37 @@ public JsonElement parseString(String str) {
return parser.parse(str);
}

private static class JsonArrayListWrapper extends AbstractList<JsonElement> {
private final JsonArray array;

JsonArrayListWrapper(JsonArray array) {
this.array = array;
}

@Override
public JsonElement get(int index) {
return array.get(index);
}

@Override
public int size() {
return array.size();
}
}

@Override
public List<JsonElement> toList(JsonElement value) {
List<JsonElement> list;
if (value.isJsonObject()) {
list = new ArrayList<>(value.getAsJsonObject().size());
if (value.isJsonArray()) {
return new JsonArrayListWrapper(value.getAsJsonArray());
} else if (value.isJsonObject()) {
List<JsonElement> list = new ArrayList<>(value.getAsJsonObject().size());
for(Map.Entry<String, JsonElement> entry : value.getAsJsonObject().entrySet()) {
list.add(entry.getValue());
}
return list;
} else {
return Collections.emptyList();
}
if (value.isJsonArray()) {
list = new ArrayList<>(value.getAsJsonArray().size());
for (JsonElement e : value.getAsJsonArray()) {
list.add(e);
}
return list;
}
return Collections.emptyList();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.burt.jmespath.jackson;

import java.util.AbstractList;
import java.util.List;
import java.util.ArrayList;
import java.util.Collection;
Expand Down Expand Up @@ -43,9 +44,29 @@ public JsonNode parseString(String string) {
}
}

private static class ArrayNodeListWrapper extends AbstractList<JsonNode> {
private final ArrayNode array;

ArrayNodeListWrapper(ArrayNode array) {
this.array = array;
}

@Override
public JsonNode get(int index) {
return array.get(index);
}

@Override
public int size() {
return array.size();
}
}

@Override
public List<JsonNode> toList(JsonNode value) {
if (value.isArray() || value.isObject()) {
if (value.isArray()) {
return new ArrayNodeListWrapper((ArrayNode) value);
} else if (value.isObject()) {
List<JsonNode> elements = new ArrayList<>(value.size());
for (JsonNode element : value) {
elements.add(element);
Expand Down

0 comments on commit 9edf6ca

Please sign in to comment.