diff --git a/jmespath-gson/src/main/java/io/burt/jmespath/gson/GsonRuntime.java b/jmespath-gson/src/main/java/io/burt/jmespath/gson/GsonRuntime.java index 07ac9959..9eb9a820 100644 --- a/jmespath-gson/src/main/java/io/burt/jmespath/gson/GsonRuntime.java +++ b/jmespath-gson/src/main/java/io/burt/jmespath/gson/GsonRuntime.java @@ -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; @@ -34,24 +35,37 @@ public JsonElement parseString(String str) { return parser.parse(str); } + private static class JsonArrayListWrapper extends AbstractList { + 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 toList(JsonElement value) { - List list; - if (value.isJsonObject()) { - list = new ArrayList<>(value.getAsJsonObject().size()); + if (value.isJsonArray()) { + return new JsonArrayListWrapper(value.getAsJsonArray()); + } else if (value.isJsonObject()) { + List list = new ArrayList<>(value.getAsJsonObject().size()); for(Map.Entry 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 diff --git a/jmespath-jackson/src/main/java/io/burt/jmespath/jackson/JacksonRuntime.java b/jmespath-jackson/src/main/java/io/burt/jmespath/jackson/JacksonRuntime.java index b6b95cbd..3b05037e 100644 --- a/jmespath-jackson/src/main/java/io/burt/jmespath/jackson/JacksonRuntime.java +++ b/jmespath-jackson/src/main/java/io/burt/jmespath/jackson/JacksonRuntime.java @@ -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; @@ -43,9 +44,29 @@ public JsonNode parseString(String string) { } } + private static class ArrayNodeListWrapper extends AbstractList { + 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 toList(JsonNode value) { - if (value.isArray() || value.isObject()) { + if (value.isArray()) { + return new ArrayNodeListWrapper((ArrayNode) value); + } else if (value.isObject()) { List elements = new ArrayList<>(value.size()); for (JsonNode element : value) { elements.add(element);