From b1d8f8f33aa597dbef3067bfe9a441f5fdc5f1b0 Mon Sep 17 00:00:00 2001 From: hunterpayne Date: Wed, 8 Jul 2015 17:24:51 -0700 Subject: [PATCH] Fixing a bug with a 1 character path --- .../jsonpath/internal/token/TokenStack.java | 23 ++++++++++++++++--- .../java/com/jayway/jsonpath/JacksonTest.java | 7 ++++++ 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/json-path/src/main/java/com/jayway/jsonpath/internal/token/TokenStack.java b/json-path/src/main/java/com/jayway/jsonpath/internal/token/TokenStack.java index 10bd78d6..fa0fd425 100644 --- a/json-path/src/main/java/com/jayway/jsonpath/internal/token/TokenStack.java +++ b/json-path/src/main/java/com/jayway/jsonpath/internal/token/TokenStack.java @@ -25,6 +25,7 @@ public class TokenStack protected Map matchedPaths; private TokenStackElement curr; + private Path rootMatch; public TokenStack(Configuration conf) { @@ -32,6 +33,7 @@ public class TokenStack paths = new ArrayList(); matchedPaths = new HashMap(); elements = new Stack(); + rootMatch = null; } public Stack getStack() @@ -56,6 +58,16 @@ public class TokenStack assert(callback != null); boolean needsPathCheck = false; + if (null == curr && elements.empty()) { + // check for $ patterns + for (Path path : paths) { + if (path.checkForMatch(this)) { + //if (saveMatch) matchedPaths.put(curr, path); + callback.resultFound(path); + rootMatch = path; + } + } + } while (parser.nextToken() != null) { boolean saveMatch = false; switch (parser.getCurrentToken()) { @@ -90,8 +102,8 @@ public class TokenStack if (curr != null && curr.getType() == TokenType.ARRAY_TOKEN) { if (((ArrayToken)curr).getValue() != null && - matchedPaths.containsKey(curr)) { - + matchedPaths.containsKey(curr)) + { Path match = matchedPaths.remove(curr); callback.resultFoundExit(match); @@ -188,6 +200,11 @@ public class TokenStack needsPathCheck = false; } } + + if (rootMatch != null && elements.empty()) { + callback.resultFoundExit(rootMatch); + rootMatch = null; + } } public String toString() @@ -200,7 +217,7 @@ public class TokenStack sb.append(elem.toString()); } sb.append(" "); - sb.append(elements.peek().getValue()); + if (!elements.empty()) sb.append(elements.peek().getValue()); return sb.toString(); } diff --git a/json-path/src/test/java/com/jayway/jsonpath/JacksonTest.java b/json-path/src/test/java/com/jayway/jsonpath/JacksonTest.java index 9b3267be..53188db5 100644 --- a/json-path/src/test/java/com/jayway/jsonpath/JacksonTest.java +++ b/json-path/src/test/java/com/jayway/jsonpath/JacksonTest.java @@ -64,6 +64,7 @@ public class JacksonTest extends BaseTest implements EvaluationCallback { protected int floatMatch = 0; protected Path intPath = null; protected int intMatch = 0; + protected Path rootPath = null; @Test public void streamingTest() throws Exception { @@ -76,6 +77,7 @@ public class JacksonTest extends BaseTest implements EvaluationCallback { PathCompiler.compile("$..completed_tasks[0:].resources.cpus"); intPath = PathCompiler.compile("$..completed_tasks[0:].resources.mem"); + rootPath = PathCompiler.compile("$"); stack = new TokenStack(JACKSON_CONFIGURATION); recorder = new CallbackRecorder(); @@ -90,9 +92,12 @@ public class JacksonTest extends BaseTest implements EvaluationCallback { stack.registerPath(idPath); stack.registerPath(floatPath); stack.registerPath(intPath); + stack.registerPath(rootPath); stack.read(factory.createJsonParser(stream), this); Thread.sleep(1000); + assert(recorder.getResults().get(count++). + equals(new CallbackRecorder.CallbackEvent(rootPath, false))); assert(recorder.getResults().get(count++). equals(new CallbackRecorder.CallbackEvent(path, false))); assert(recorder.getResults().get(count++). @@ -598,6 +603,8 @@ public class JacksonTest extends BaseTest implements EvaluationCallback { equals(new CallbackRecorder.CallbackEvent(intPath, false))); assert(recorder.getResults().get(count++). equals(new CallbackRecorder.CallbackEvent(path, true))); + assert(recorder.getResults().get(count++). + equals(new CallbackRecorder.CallbackEvent(rootPath, true))); } public void resultFound(Path path) {