Browse Source

Fixing a bug with multiple json objects being sent to the parser in series

pull/93/head
hunterpayne 10 years ago
parent
commit
2719bdb660
  1. 27
      json-path/src/main/java/com/jayway/jsonpath/internal/token/TokenStack.java
  2. 4
      json-path/src/test/java/com/jayway/jsonpath/CallbackRecorder.java

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

@ -58,16 +58,18 @@ public class TokenStack
assert(callback != null); assert(callback != null);
boolean needsPathCheck = false; boolean needsPathCheck = false;
/*
if (null == curr && elements.empty()) { if (null == curr && elements.empty()) {
// check for $ patterns // check for $ patterns
for (Path path : paths) { for (Path path : paths) {
if (path.checkForMatch(this)) { if (path.checkForMatch(this)) {
//if (saveMatch) matchedPaths.put(curr, path); matchedPaths.put(curr, path);
callback.resultFound(path); callback.resultFound(path);
rootMatch = path; rootMatch = path;
} }
} }
} }
*/
while (parser.nextToken() != null) { while (parser.nextToken() != null) {
boolean saveMatch = false; boolean saveMatch = false;
switch (parser.getCurrentToken()) { switch (parser.getCurrentToken()) {
@ -100,7 +102,6 @@ public class TokenStack
case START_OBJECT: case START_OBJECT:
{ {
if (curr != null && curr.getType() == TokenType.ARRAY_TOKEN) { if (curr != null && curr.getType() == TokenType.ARRAY_TOKEN) {
if (((ArrayToken)curr).getValue() != null && if (((ArrayToken)curr).getValue() != null &&
matchedPaths.containsKey(curr)) matchedPaths.containsKey(curr))
{ {
@ -113,6 +114,15 @@ public class TokenStack
callback.resultFound(match); callback.resultFound(match);
} }
} }
} else if (null == curr && elements.empty()) {
// check for $ patterns
for (Path path : paths) {
if (path.checkForMatch(this)) {
matchedPaths.put(curr, path);
callback.resultFound(path);
rootMatch = path;
}
}
} }
if (curr != null) { if (curr != null) {
@ -129,9 +139,16 @@ public class TokenStack
} }
case END_OBJECT: case END_OBJECT:
{ {
Path match = matchedPaths.remove(curr); if (!"$".equals(curr)) {
if (match != null) { Path match = matchedPaths.remove(curr);
callback.resultFoundExit(match); if (match != null) {
callback.resultFoundExit(match);
}
} else {
Path match = matchedPaths.get("$");
if (match != null) {
callback.resultFoundExit(match);
}
} }
elements.pop(); elements.pop();
if (elements.empty()) curr = null; if (elements.empty()) curr = null;

4
json-path/src/test/java/com/jayway/jsonpath/CallbackRecorder.java

@ -31,6 +31,10 @@ public class CallbackRecorder implements EvaluationCallback {
} }
return false; return false;
} }
public String toString() {
return path + " isexit=" + exit;
}
} }
private List<CallbackEvent> results; private List<CallbackEvent> results;

Loading…
Cancel
Save