diff --git a/json-assert/pom.xml b/json-assert/pom.xml
index 00791520..6e9a81d7 100644
--- a/json-assert/pom.xml
+++ b/json-assert/pom.xml
@@ -30,6 +30,10 @@
http://maven.apache.org
+
+ commons-jxpath
+ commons-jxpath
+
com.googlecode.json-simple
diff --git a/json-assert/src/main/java/com/jayway/jsonassert/JSONAssert.java b/json-assert/src/main/java/com/jayway/jsonassert/JSONAssert.java
index e8bd8c48..2b2a124d 100644
--- a/json-assert/src/main/java/com/jayway/jsonassert/JSONAssert.java
+++ b/json-assert/src/main/java/com/jayway/jsonassert/JSONAssert.java
@@ -1,7 +1,7 @@
package com.jayway.jsonassert;
-import com.jayway.jsonassert.impl.JSONAsserterImpl;
-import com.jayway.jsonassert.impl.JSONReaderImpl;
+import com.jayway.jsonassert.impl.JsonAsserterImpl;
+import com.jayway.jsonassert.impl.JsonReaderImpl;
import java.io.IOException;
import java.io.Reader;
@@ -12,7 +12,7 @@ import java.text.ParseException;
* Date: 1/24/11
* Time: 9:31 PM
*/
-public class JSONAssert {
+public class JsonAssert {
/**
* Creates a JSONReader
@@ -21,8 +21,8 @@ public class JSONAssert {
* @return a new reader
* @throws ParseException
*/
- public static JSONReader parse(String jsonDoc) throws ParseException {
- return JSONReaderImpl.parse(jsonDoc);
+ public static JsonPath parse(String jsonDoc) throws ParseException {
+ return JsonReaderImpl.parse(jsonDoc);
}
/**
@@ -33,7 +33,7 @@ public class JSONAssert {
* @throws ParseException document could not pe parsed
* @throws IOException
*/
- public static JSONReader parse(Reader reader) throws ParseException, IOException {
+ public static JsonPath parse(Reader reader) throws ParseException, IOException {
return parse(reader, false);
}
@@ -45,10 +45,10 @@ public class JSONAssert {
* @throws ParseException document could not pe parsed
* @throws IOException
*/
- public static JSONReader parse(Reader reader, boolean closeReader) throws ParseException, IOException {
- JSONReader jsonReader = null;
+ public static JsonPath parse(Reader reader, boolean closeReader) throws ParseException, IOException {
+ JsonPath jsonReader = null;
try {
- jsonReader = JSONReaderImpl.parse(reader);
+ jsonReader = JsonReaderImpl.parse(reader);
} finally {
if(closeReader){
try {
@@ -66,8 +66,8 @@ public class JSONAssert {
* @return a JSON asserter initialized with the provided document
* @throws ParseException when the given JSON could not be parsed
*/
- public static JSONAsserter with(String json) throws ParseException {
- return new JSONAsserterImpl(JSONReaderImpl.parse(json));
+ public static JsonAsserter with(String json) throws ParseException {
+ return new JsonAsserterImpl(JsonReaderImpl.parse(json));
}
/**
@@ -77,8 +77,8 @@ public class JSONAssert {
* @return a JSON asserter initialized with the provided document
* @throws ParseException when the given JSON could not be parsed
*/
- public static JSONAsserter with(Reader reader) throws ParseException, IOException {
- return new JSONAsserterImpl(JSONReaderImpl.parse(reader));
+ public static JsonAsserter with(Reader reader) throws ParseException, IOException {
+ return new JsonAsserterImpl(JsonReaderImpl.parse(reader));
}
}
diff --git a/json-assert/src/main/java/com/jayway/jsonassert/JSONAsserter.java b/json-assert/src/main/java/com/jayway/jsonassert/JSONAsserter.java
index 18414d7a..27b28c3f 100644
--- a/json-assert/src/main/java/com/jayway/jsonassert/JSONAsserter.java
+++ b/json-assert/src/main/java/com/jayway/jsonassert/JSONAsserter.java
@@ -3,19 +3,18 @@ package com.jayway.jsonassert;
import org.hamcrest.Matcher;
/**
- * Created by IntelliJ IDEA.
* User: kallestenflo
* Date: 1/24/11
* Time: 9:22 PM
*/
-public interface JSONAsserter {
+public interface JsonAsserter {
/**
- * Gives access to the {@link JSONReader} used to base the assertions on
+ * Gives access to the {@link JsonPath} used to base the assertions on
*
* @return the underlying reader
*/
- JSONReader reader();
+ JsonPath reader();
/**
* Asserts that object specified by path satisfies the condition specified by matcher.
@@ -32,7 +31,7 @@ public interface JSONAsserter {
* @param the static type accepted by the matcher
* @return this to allow fluent assertion chains
*/
- JSONAsserter assertThat(String path, Matcher matcher);
+ JsonAsserter assertThat(String path, Matcher matcher);
/**
* Asserts that object specified by path is equal to the expected value.
@@ -43,7 +42,7 @@ public interface JSONAsserter {
* @param the static type that should be returned by the path
* @return this to allow fluent assertion chains
*/
- JSONAsserter assertEquals(String path, T expected);
+ JsonAsserter assertEquals(String path, T expected);
/**
* Asserts that object specified by path is null. If it is not, an AssertionError
@@ -52,7 +51,7 @@ public interface JSONAsserter {
* @param path the json path specifying the value that should be null
* @return this to allow fluent assertion chains
*/
- JSONAsserter assertNull(String path);
+ JsonAsserter assertNull(String path);
/**
* Asserts that object specified by path is NOT null. If it is, an AssertionError
@@ -61,7 +60,7 @@ public interface JSONAsserter {
* @param path the json path specifying the value that should be NOT null
* @return this to allow fluent assertion chains
*/
- JSONAsserter assertNotNull(String path);
+ JsonAsserter assertNotNull(String path);
/**
* Syntactic sugar to allow chaining assertions with a separating and() statement
@@ -73,5 +72,5 @@ public interface JSONAsserter {
*
* @return this to allow fluent assertion chains
*/
- JSONAsserter and();
+ JsonAsserter and();
}
diff --git a/json-assert/src/main/java/com/jayway/jsonassert/JSONReader.java b/json-assert/src/main/java/com/jayway/jsonassert/JsonPath.java
similarity index 73%
rename from json-assert/src/main/java/com/jayway/jsonassert/JSONReader.java
rename to json-assert/src/main/java/com/jayway/jsonassert/JsonPath.java
index 41e14481..35e014bf 100644
--- a/json-assert/src/main/java/com/jayway/jsonassert/JSONReader.java
+++ b/json-assert/src/main/java/com/jayway/jsonassert/JsonPath.java
@@ -4,12 +4,19 @@ import java.util.List;
import java.util.Map;
/**
- * Created by IntelliJ IDEA.
* User: kallestenflo
* Date: 1/24/11
* Time: 9:29 PM
*/
-public interface JSONReader {
+public interface JsonPath {
+
+ /**
+ * Get a new reader with the given path as root
+ *
+ * @param path to extract a reader for
+ * @return new reader
+ */
+ JsonPath getReader(String path);
/**
* @param path
@@ -25,9 +32,9 @@ public interface JSONReader {
/**
* @param path
- * @return
+ * @returnÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊ
*/
- Object get(String path);
+ T get(String path);
/**
* @param path
@@ -64,5 +71,5 @@ public interface JSONReader {
* @param path
* @return
*/
- Map getMap(String path);
+ Map getMap(String path);
}
diff --git a/json-assert/src/main/java/com/jayway/jsonassert/impl/JSONAsserterImpl.java b/json-assert/src/main/java/com/jayway/jsonassert/impl/JSONAsserterImpl.java
index 4b9d19a5..80c9a5c3 100644
--- a/json-assert/src/main/java/com/jayway/jsonassert/impl/JSONAsserterImpl.java
+++ b/json-assert/src/main/java/com/jayway/jsonassert/impl/JSONAsserterImpl.java
@@ -1,8 +1,8 @@
package com.jayway.jsonassert.impl;
-import com.jayway.jsonassert.JSONAsserter;
-import com.jayway.jsonassert.JSONReader;
+import com.jayway.jsonassert.JsonAsserter;
+import com.jayway.jsonassert.JsonPath;
import org.hamcrest.Matcher;
import org.hamcrest.MatcherAssert;
@@ -13,9 +13,9 @@ import static org.hamcrest.Matchers.*;
* Date: 1/21/11
* Time: 3:43 PM
*/
-public class JSONAsserterImpl implements JSONAsserter {
+public class JsonAsserterImpl implements JsonAsserter {
- private final JSONReader reader;
+ private final JsonPath reader;
/**
@@ -23,21 +23,22 @@ public class JSONAsserterImpl implements JSONAsserter {
*
* @param reader initialized with the JSON document to be asserted upon
*/
- public JSONAsserterImpl(JSONReader reader) {
+ public JsonAsserterImpl(JsonPath reader) {
this.reader = reader;
}
/**
* {@inheritDoc}
*/
- public JSONReader reader() {
+ public JsonPath reader() {
return reader;
}
/**
* {@inheritDoc}
*/
- public JSONAsserter assertThat(String path, Matcher matcher) {
+ @SuppressWarnings("unchecked")
+ public JsonAsserter assertThat(String path, Matcher matcher) {
MatcherAssert.assertThat((T) reader.get(path), matcher);
return this;
}
@@ -45,28 +46,28 @@ public class JSONAsserterImpl implements JSONAsserter {
/**
* {@inheritDoc}
*/
- public JSONAsserter assertEquals(String path, T expected) {
+ public JsonAsserter assertEquals(String path, T expected) {
return assertThat(path, equalTo(expected));
}
/**
* {@inheritDoc}
*/
- public JSONAsserter assertNull(String path) {
+ public JsonAsserter assertNull(String path) {
return assertThat(path, nullValue());
}
/**
* {@inheritDoc}
*/
- public JSONAsserter assertNotNull(String path) {
+ public JsonAsserter assertNotNull(String path) {
return assertThat(path, notNullValue());
}
/**
* {@inheritDoc}
*/
- public JSONAsserter and() {
+ public JsonAsserter and() {
return this;
}
diff --git a/json-assert/src/main/java/com/jayway/jsonassert/impl/JSONPath.java b/json-assert/src/main/java/com/jayway/jsonassert/impl/JSONPath.java
deleted file mode 100644
index 07a8130d..00000000
--- a/json-assert/src/main/java/com/jayway/jsonassert/impl/JSONPath.java
+++ /dev/null
@@ -1,40 +0,0 @@
-package com.jayway.jsonassert.impl;
-
-import java.util.LinkedList;
-import java.util.Queue;
-
-/**
- * User: kalle stenflo
- * Date: 1/23/11
- * Time: 1:52 PM
- */
-class JSONPath {
-
- private final Queue path;
-
- JSONPath(String path) {
- this.path = compilePathFragments(path);
- }
-
- boolean hasMoreFragments(){
- return !path.isEmpty();
- }
-
- JSONPathFragment nextFragment(){
- return path.poll();
- }
-
- private Queue compilePathFragments(String path) {
- //TODO: this needs some attention but will work for now
- Queue processed = new LinkedList();
- for (String fragment : path.split("[\\.|\\[]")) {
- if (fragment.trim().length() > 0) {
-
- String compiledFragment = fragment.endsWith("]") ? "[" + fragment : fragment;
-
- processed.add(new JSONPathFragment(compiledFragment));
- }
- }
- return processed;
- }
-}
diff --git a/json-assert/src/main/java/com/jayway/jsonassert/impl/JSONPathFragment.java b/json-assert/src/main/java/com/jayway/jsonassert/impl/JSONPathFragment.java
deleted file mode 100644
index 0def3f30..00000000
--- a/json-assert/src/main/java/com/jayway/jsonassert/impl/JSONPathFragment.java
+++ /dev/null
@@ -1,69 +0,0 @@
-package com.jayway.jsonassert.impl;
-
-import java.util.regex.Matcher;
-import java.util.regex.Pattern;
-
-/**
- * User: kalle stenflo
- * Date: 1/23/11
- * Time: 1:55 PM
- */
-class JSONPathFragment {
-
- private static final Pattern ARRAY_POSITION_PATTER = Pattern.compile("\\[(\\d*)\\]");
- private static final Pattern GROOVY_POSITION_PATTER = Pattern.compile("get\\((\\d*)\\)");
-
- private static final Pattern ARRAY_WILDCARD_PATTER = Pattern.compile("\\[\\*\\]");
- private static final Pattern GROOVY_WILDCARD_PATTER = Pattern.compile("get\\(\\*\\)");
-
- private final String fragment;
-
- JSONPathFragment(String fragment) {
- this.fragment = fragment;
- }
-
- String value() {
- return fragment;
- }
-
- String appendToPath(String path){
- StringBuilder builder = new StringBuilder(path);
-
- if(ARRAY_POSITION_PATTER.matcher(fragment).matches()){
- builder.append("[").append(getArrayIndex()).append("]");
- }
- else if(GROOVY_POSITION_PATTER.matcher(fragment).matches()){
- builder.append(path.isEmpty()?"":".").append("get(").append(getArrayIndex()).append(")");
- }
- else if(ARRAY_WILDCARD_PATTER.matcher(fragment).matches()){
- builder.append("[*]");
- }
- else if(GROOVY_WILDCARD_PATTER.matcher(fragment).matches()){
- builder.append(path.isEmpty()?"":".").append("get(*)");
- }
- else {
- builder.append(path.isEmpty()?"":".").append(fragment);
- }
- return builder.toString();
- }
-
- boolean isArrayIndex() {
- return ARRAY_POSITION_PATTER.matcher(fragment).matches() || GROOVY_POSITION_PATTER.matcher(fragment).matches();
- }
-
- boolean isArrayWildcard() {
- return ARRAY_WILDCARD_PATTER.matcher(fragment).matches() || GROOVY_WILDCARD_PATTER.matcher(fragment).matches();
- }
-
- int getArrayIndex() {
- Matcher matcher = ARRAY_POSITION_PATTER.matcher(fragment);
- if (matcher.matches()) {
- return Integer.parseInt(matcher.group(1));
- }
- matcher = GROOVY_POSITION_PATTER.matcher(fragment);
- if (matcher.matches()) {
- return Integer.parseInt(matcher.group(1));
- }
- throw new IllegalArgumentException("not an array index fragment");
- }
-}
diff --git a/json-assert/src/main/java/com/jayway/jsonassert/impl/JSONReaderImpl.java b/json-assert/src/main/java/com/jayway/jsonassert/impl/JSONReaderImpl.java
index 59dca68c..d61e51a7 100644
--- a/json-assert/src/main/java/com/jayway/jsonassert/impl/JSONReaderImpl.java
+++ b/json-assert/src/main/java/com/jayway/jsonassert/impl/JSONReaderImpl.java
@@ -1,7 +1,7 @@
package com.jayway.jsonassert.impl;
import com.jayway.jsonassert.InvalidPathException;
-import com.jayway.jsonassert.JSONReader;
+import com.jayway.jsonassert.JsonPath;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
@@ -20,7 +20,7 @@ import static org.apache.commons.lang.Validate.notNull;
* Date: 1/20/11
* Time: 4:27 PM
*/
-public class JSONReaderImpl implements JSONReader {
+public class JsonReaderImpl implements JsonPath {
private static final JSONParser JSON_PARSER = new JSONParser();
@@ -29,9 +29,9 @@ public class JSONReaderImpl implements JSONReader {
private String currentPath;
- public static synchronized JSONReader parse(Reader reader) throws java.text.ParseException, IOException {
+ public static synchronized JsonPath parse(Reader reader) throws java.text.ParseException, IOException {
try {
- return new JSONReaderImpl(JSON_PARSER.parse(reader));
+ return new JsonReaderImpl(JSON_PARSER.parse(reader));
} catch (IOException e) {
throw e;
} catch (ParseException e) {
@@ -39,19 +39,32 @@ public class JSONReaderImpl implements JSONReader {
}
}
- public static synchronized JSONReader parse(String jsonDoc) throws java.text.ParseException {
+ public static synchronized JsonPath parse(String jsonDoc) throws java.text.ParseException {
try {
- return new JSONReaderImpl(JSON_PARSER.parse(jsonDoc));
+ return new JsonReaderImpl(JSON_PARSER.parse(jsonDoc));
} catch (ParseException e) {
throw new java.text.ParseException(e.getMessage(), e.getPosition());
}
}
- private JSONReaderImpl(Object root) {
+ private JsonReaderImpl(Object root) {
notNull(root, "root object can not be null");
this.root = root;
}
+ /**
+ * {@inheritDoc}
+ */
+ public JsonPath getReader(String path) {
+
+ Object jsonObject = get(path);
+
+ if (!isArray(jsonObject) && !isDocument(jsonObject)) {
+ throw new InvalidPathException("path points to a leaf that is not a JSON document or Array");
+ }
+
+ return new JsonReaderImpl(jsonObject);
+ }
/**
* {@inheritDoc}
@@ -76,8 +89,9 @@ public class JSONReaderImpl implements JSONReader {
/**
* {@inheritDoc}
*/
- public Object get(String path) {
- return getByPath(Object.class, path);
+ @SuppressWarnings("unchecked")
+ public T get(String path) {
+ return (T) getByPath(Object.class, path);
}
/**
@@ -118,7 +132,7 @@ public class JSONReaderImpl implements JSONReader {
/**
* {@inheritDoc}
*/
- public Map getMap(String path) {
+ public Map getMap(String path) {
return getByPath(Map.class, path);
}
@@ -129,7 +143,7 @@ public class JSONReaderImpl implements JSONReader {
//
//------------------------------------------------------------
-
+ /*
private T getByPath(Class clazz, String stringPath) {
currentPath = "";
Object current = this.root;
@@ -137,20 +151,87 @@ public class JSONReaderImpl implements JSONReader {
while (path.hasMoreFragments()) {
- JSONPathFragment fragment = path.nextFragment();
+ JSONPathFragment fragment = path.poll();
currentPath = fragment.appendToPath(currentPath);
if (fragment.isArrayIndex()) {
current = toArray(current).get(fragment.getArrayIndex());
} else if (fragment.isArrayWildcard()) {
- current = getContainerValue(current, path.nextFragment());
+ current = getContainerValue(current, path.poll());
} else {
current = getContainerValue(current, fragment);
}
}
return clazz.cast(current);
}
+ */
+
+ ///*
+ private T getByPath(Class clazz, String stringPath) {
+ currentPath = "";
+ Object current = this.root;
+ Path path = new Path(stringPath);
+
+ while (path.hasMoreFragments()) {
+
+ PathFragment fragment = path.poll();
+
+ currentPath = fragment.appendToPath(currentPath);
+
+ if (fragment.isArrayIndex()) {
+ current = toArray(current).get(fragment.getArrayIndex());
+ } else if (fragment.isArrayWildcard()) {
+ current = getContainerValue(current, path.poll());
+ } else {
+
+ System.out.println("FRAGMENT " + fragment.toString());
+
+ current = getContainerValue(current, fragment);
+
+ if (isArray(current) && path.hasMoreFragments() && !path.peek().isArrayIndex() && !path.peek().isArrayWildcard()) {
+
+ JSONArray array = new JSONArray();
+
+ for (Object o : toArray(current)) {
+ String newPath = path.toString();
+
+ System.out.println("NEW PATH " + newPath);
+
+ if (isDocument(o) || isArray(o)) {
+
+ JsonReaderImpl sub = new JsonReaderImpl(o);
+
+ Object o1 = sub.get(newPath);
+ //if(o instanceof Collection){
+ array.add(o1);
+ //}
+ //else {
+ // array.addAll(l)
+ //}
+
+
+ } else {
+ System.out.println("hhhhhhh");
+ array.add(o);
+ }
+ }
+ current = array;
+ break;
+ }
+ }
+ }
+ return clazz.cast(current);
+ }
+
+ //*/
+ private boolean isArray(Object obj) {
+ return (obj instanceof JSONArray);
+ }
+
+ private boolean isDocument(Object obj) {
+ return (obj instanceof JSONObject);
+ }
private JSONArray toArray(Object array) {
return (JSONArray) array;
@@ -167,14 +248,14 @@ public class JSONReaderImpl implements JSONReader {
* will be returned in a List
*
* @param container a json document or array
- * @param fragment the field to extract from the document alt. the documents contained in the array
+ * @param fragment the field to extract from the document alt. the documents contained in the array
* @return a single field value or a List of fields
*/
- private Object getContainerValue(Object container, JSONPathFragment fragment) {
+ private Object getContainerValue(Object container, PathFragment fragment) {
Object result;
if (container instanceof JSONArray) {
- List list = new LinkedList();
+ List