Browse Source

Fixing a bug with a 1 character path

pull/93/head
hunterpayne 10 years ago
parent
commit
b1d8f8f33a
  1. 23
      json-path/src/main/java/com/jayway/jsonpath/internal/token/TokenStack.java
  2. 7
      json-path/src/test/java/com/jayway/jsonpath/JacksonTest.java

23
json-path/src/main/java/com/jayway/jsonpath/internal/token/TokenStack.java

@ -25,6 +25,7 @@ public class TokenStack
protected Map<TokenStackElement, Path> matchedPaths;
private TokenStackElement curr;
private Path rootMatch;
public TokenStack(Configuration conf)
{
@ -32,6 +33,7 @@ public class TokenStack
paths = new ArrayList<Path>();
matchedPaths = new HashMap<TokenStackElement, Path>();
elements = new Stack<TokenStackElement>();
rootMatch = null;
}
public Stack<TokenStackElement> 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();
}

7
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) {

Loading…
Cancel
Save