From f4f44f61fff968950b37f232ab47a57252d93bd8 Mon Sep 17 00:00:00 2001 From: Xavi Aracil Date: Fri, 25 Nov 2022 13:55:08 +0100 Subject: [PATCH] Return empty list when node is an object --- .../inspect/vc/util/JsonNodeUtil.java | 21 +++++++++++-------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/inspector-vc/src/main/java/org/oneedtech/inspect/vc/util/JsonNodeUtil.java b/inspector-vc/src/main/java/org/oneedtech/inspect/vc/util/JsonNodeUtil.java index aaf6083..630e43a 100644 --- a/inspector-vc/src/main/java/org/oneedtech/inspect/vc/util/JsonNodeUtil.java +++ b/inspector-vc/src/main/java/org/oneedtech/inspect/vc/util/JsonNodeUtil.java @@ -17,11 +17,11 @@ import com.fasterxml.jackson.databind.node.ArrayNode; public class JsonNodeUtil { public static List asNodeList(JsonNode root, String jsonPath, JsonPathEvaluator evaluator) { - List list = new ArrayList<>(); - ArrayNode array = evaluator.eval(jsonPath, root); + List list = new ArrayList<>(); + ArrayNode array = evaluator.eval(jsonPath, root); for(JsonNode node : array) { if(!(node instanceof ArrayNode)) { - list.add(node); + list.add(node); } else { ArrayNode values = (ArrayNode) node; for(JsonNode value : values) { @@ -31,28 +31,31 @@ public class JsonNodeUtil { } return list; } - + public static List asStringList(JsonNode node) { if(!(node instanceof ArrayNode)) { + if (node.isObject()) { + return List.of(); + } return List.of(node.asText()); } else { - ArrayNode arrayNode = (ArrayNode)node; + ArrayNode arrayNode = (ArrayNode)node; return StreamSupport .stream(arrayNode.spliterator(), false) .map(n->n.asText().strip()) - .collect(Collectors.toList()); + .collect(Collectors.toList()); } } - + public static List asNodeList(JsonNode node) { if(node == null) return null; if(!(node instanceof ArrayNode)) { return List.of(node); } else { - ArrayNode arrayNode = (ArrayNode)node; + ArrayNode arrayNode = (ArrayNode)node; return StreamSupport .stream(arrayNode.spliterator(), false) - .collect(Collectors.toList()); + .collect(Collectors.toList()); } } }