Browse Source

Added Jackson and JsonSmart support to TokenStack splitter. Gson not supported.

pull/93/head
Steve White 10 years ago
parent
commit
342904c0ef
  1. 67
      json-path/src/main/java/com/jayway/jsonpath/internal/token/TokenStack.java
  2. 14
      json-path/src/test/java/com/jayway/jsonpath/JacksonTest_Split.java

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

@ -28,6 +28,8 @@ public class TokenStack
private TokenStackElement curr; private TokenStackElement curr;
private Path rootMatch; private Path rootMatch;
private Class jsonArrayType;
private Class jsonObjectType;
public TokenStack(Configuration conf) public TokenStack(Configuration conf)
{ {
@ -37,6 +39,9 @@ public class TokenStack
elements = new Stack<TokenStackElement>(); elements = new Stack<TokenStackElement>();
objStack = new Stack<Object>(); objStack = new Stack<Object>();
rootMatch = null; rootMatch = null;
this.jsonArrayType = this.getNewJsonArray().getClass();
this.jsonObjectType = this.getNewJsonObject().getClass();
} }
public Stack<TokenStackElement> getStack() public Stack<TokenStackElement> getStack()
@ -59,7 +64,8 @@ public class TokenStack
} }
/** /**
* reads from stream and notifies the callback of matched registered paths * reads from stream and notifies the callback of matched registered paths, with results.
* Note: GSON not supported
*/ */
public void read(JsonParser parser, EvaluationCallback callback, boolean getParents) public void read(JsonParser parser, EvaluationCallback callback, boolean getParents)
throws Exception throws Exception
@ -68,18 +74,7 @@ public class TokenStack
Object obj = null; Object obj = null;
boolean needsPathCheck = false; boolean needsPathCheck = false;
/*
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;
}
}
}
*/
while (parser.nextToken() != null) { while (parser.nextToken() != null) {
//log.debug("type/name/val: " + parser.getCurrentToken() + " " + parser.getCurrentName() + " " + parser.getText()); //log.debug("type/name/val: " + parser.getCurrentToken() + " " + parser.getCurrentName() + " " + parser.getText());
boolean saveMatch = false; boolean saveMatch = false;
@ -97,7 +92,7 @@ public class TokenStack
needsPathCheck = true; needsPathCheck = true;
elements.push(curr); elements.push(curr);
obj = stackPush(parser.getCurrentName(),new JSONArray()); obj = stackPush(parser.getCurrentName(),getNewJsonArray());
break; break;
} }
case END_ARRAY: case END_ARRAY:
@ -110,13 +105,13 @@ public class TokenStack
if (elements.empty()) curr = null; if (elements.empty()) curr = null;
else curr = elements.peek(); else curr = elements.peek();
obj = stackPop(callback, curr, JSONArray.class, match); obj = stackPop(callback, curr, getJsonArrayType(), match);
break; break;
} }
case VALUE_EMBEDDED_OBJECT: case VALUE_EMBEDDED_OBJECT:
case START_OBJECT: case START_OBJECT:
{ {
obj = stackPush(parser.getCurrentName(), new JSONObject()); obj = stackPush(parser.getCurrentName(), getNewJsonObject());
if (isArray(curr)) { if (isArray(curr)) {
if (((ArrayToken)curr).getValue() != null && if (((ArrayToken)curr).getValue() != null &&
@ -176,7 +171,7 @@ public class TokenStack
if (elements.empty()) curr = null; if (elements.empty()) curr = null;
else curr = elements.peek(); else curr = elements.peek();
obj = stackPop(callback, curr, JSONObject.class, null); obj = stackPop(callback, curr, this.getJsonObjectType(), null);
break; break;
} }
case FIELD_NAME: case FIELD_NAME:
@ -252,7 +247,7 @@ public class TokenStack
if (rootMatch != null && elements.empty() && getParents) { if (rootMatch != null && elements.empty() && getParents) {
if (isArray(curr)) { if (isArray(curr)) {
obj = new JSONArray(); obj = this.getNewJsonArray();
} }
callback.resultFoundExit(parser.getCurrentToken(), obj, rootMatch); callback.resultFoundExit(parser.getCurrentToken(), obj, rootMatch);
rootMatch = null; rootMatch = null;
@ -260,6 +255,22 @@ public class TokenStack
} }
} }
private Object getNewJsonObject() {
return conf.jsonProvider().createMap();
}
private Object getJsonArrayType() {
return this.jsonArrayType;
}
private Object getNewJsonArray() {
return conf.jsonProvider().createArray();
}
private Class<? extends Object> getJsonObjectType() {
return this.jsonObjectType;
}
private Object stackPush(String key, Object jsObj) throws Exception { private Object stackPush(String key, Object jsObj) throws Exception {
if (jsObj == null) { if (jsObj == null) {
return null; return null;
@ -268,10 +279,10 @@ public class TokenStack
if (this.objStack.size() > 0) { if (this.objStack.size() > 0) {
Object obj = this.objStack.peek(); Object obj = this.objStack.peek();
//log.trace("PP : Push[" + this.objStack.size() + "] " + jsObj.getClass().getSimpleName() + " -> " + obj.getClass()); //log.trace("PP : Push[" + this.objStack.size() + "] " + jsObj.getClass().getSimpleName() + " -> " + obj.getClass());
if (obj.getClass() == JSONArray.class) { if (obj.getClass() == getJsonArrayType()) {
((JSONArray)obj).add(jsObj); ((List)obj).add(jsObj);
} else if (obj.getClass() == JSONObject.class) { } else if (obj.getClass() == getJsonObjectType()) {
((JSONObject)obj).put(key, jsObj); ((HashMap<String,Object>)obj).put(key, jsObj);
} else { } else {
throw new Exception("Unhandled type: " + obj.getClass()); throw new Exception("Unhandled type: " + obj.getClass());
} }
@ -323,9 +334,9 @@ public class TokenStack
public String objShow(Object obj) { public String objShow(Object obj) {
if (obj != null) { if (obj != null) {
Class cls = obj.getClass(); Class cls = obj.getClass();
if (cls == JSONObject.class) { if (cls == this.getJsonObjectType()) {
return "JSONObject: " + ((JSONObject)obj).toString(); return "JSONObject: " + ((JSONObject)obj).toString();
} else if (cls == JSONArray.class) { } else if (cls == this.getJsonArrayType()) {
return "JSONArray: " + Arrays.toString(((JSONArray)obj).toArray()); return "JSONArray: " + Arrays.toString(((JSONArray)obj).toArray());
} }
} }
@ -338,11 +349,11 @@ public class TokenStack
} }
Class objInCls = objIn.getClass(); Class objInCls = objIn.getClass();
if (objInCls == JSONObject.class) { if (objInCls == this.getJsonObjectType()) {
JSONObject obj = (JSONObject)objIn; HashMap obj = (HashMap)objIn;
obj.put(((ObjectToken)el).key, value); obj.put(((ObjectToken)el).key, value);
} else if (objInCls == JSONArray.class) { } else if (objInCls == this.getJsonArrayType()) {
JSONArray obj = (JSONArray)objIn; ArrayList obj = (ArrayList)objIn;
obj.add(value); obj.add(value);
} else { } else {
throw new Exception("Unhandled type: " + objInCls); throw new Exception("Unhandled type: " + objInCls);

14
json-path/src/test/java/com/jayway/jsonpath/JacksonTest_Split.java

@ -2,10 +2,12 @@ package com.jayway.jsonpath;
import static org.junit.Assert.*; import static org.junit.Assert.*;
import com.fasterxml.jackson.core.JsonFactory; import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonParseException;
import com.jayway.jsonpath.internal.Path; import com.jayway.jsonpath.internal.Path;
import com.jayway.jsonpath.internal.PathCompiler; import com.jayway.jsonpath.internal.PathCompiler;
import com.jayway.jsonpath.internal.token.TokenStack; import com.jayway.jsonpath.internal.token.TokenStack;
import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -20,12 +22,20 @@ public class JacksonTest_Split extends BaseTest implements EvaluationCallback {
private List<Object> results = new ArrayList<Object>(); private List<Object> results = new ArrayList<Object>();
@Test @Test
public void jsonTest() throws Exception { public void json_Test() throws Exception {
jsonSplit_Test(JACKSON_CONFIGURATION);
results.clear();
jsonSplit_Test(JSON_SMART_CONFIGURATION);
results.clear();
}
private void jsonSplit_Test(Configuration jsonProviderCfg) throws JsonParseException, IOException, Exception {
String res = "json_opsview1.json"; String res = "json_opsview1.json";
try (InputStream stream = getClass().getClassLoader().getResourceAsStream(res)) { try (InputStream stream = getClass().getClassLoader().getResourceAsStream(res)) {
Path path = PathCompiler.compile("$.list[*]"); Path path = PathCompiler.compile("$.list[*]");
TokenStack stack = new TokenStack(JACKSON_CONFIGURATION); TokenStack stack = new TokenStack(jsonProviderCfg);
JsonFactory factory = new JsonFactory(); JsonFactory factory = new JsonFactory();
stack.registerPath(path); stack.registerPath(path);

Loading…
Cancel
Save