diff --git a/json-path-assert/src/main/java/com/jayway/jsonassert/JsonAssert.java b/json-path-assert/src/main/java/com/jayway/jsonassert/JsonAssert.java
index 22277d2b..999b2c68 100644
--- a/json-path-assert/src/main/java/com/jayway/jsonassert/JsonAssert.java
+++ b/json-path-assert/src/main/java/com/jayway/jsonassert/JsonAssert.java
@@ -4,6 +4,7 @@ package com.jayway.jsonassert;
import com.jayway.jsonassert.impl.JsonAsserterImpl;
import com.jayway.jsonassert.impl.matcher.*;
import com.jayway.jsonpath.spi.JsonProvider;
+import com.jayway.jsonpath.spi.JsonProviderFactory;
import org.hamcrest.Matcher;
import java.io.*;
@@ -18,7 +19,7 @@ import java.util.Map;
*/
public class JsonAssert {
- private static JsonProvider jsonProvider = JsonProvider.getInstance();
+ private static JsonProvider jsonProvider = JsonProviderFactory.getInstance();
public static void setJsonProvider(JsonProvider jsonProvider) {
JsonAssert.jsonProvider = jsonProvider;
diff --git a/json-path/pom.xml b/json-path/pom.xml
index 86e83de8..d94b0baf 100644
--- a/json-path/pom.xml
+++ b/json-path/pom.xml
@@ -40,10 +40,15 @@
commons-lang
+
+ commons-io
+ commons-io
+
+
org.codehaus.jacksonjackson-mapper-asl
- provided
+ true
@@ -66,13 +71,13 @@
-
-
-
- org.apache.felix
- maven-bundle-plugin
- true
-
-
-
+
+
+
+ org.apache.felix
+ maven-bundle-plugin
+ true
+
+
+
diff --git a/json-path/src/main/java/com/jayway/jsonpath/InvalidJsonException.java b/json-path/src/main/java/com/jayway/jsonpath/InvalidJsonException.java
index 5c2aae5d..a324c377 100644
--- a/json-path/src/main/java/com/jayway/jsonpath/InvalidJsonException.java
+++ b/json-path/src/main/java/com/jayway/jsonpath/InvalidJsonException.java
@@ -1,12 +1,23 @@
+/*
+ * Copyright 2011 the original author or authors.
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
package com.jayway.jsonpath;
/**
- * Created by IntelliJ IDEA.
- * User: kallestenflo
- * Date: 11/8/11
- * Time: 4:01 PM
+ * @author Kalle Stenflo
*/
-public class InvalidJsonException extends RuntimeException{
+public class InvalidJsonException extends RuntimeException {
public InvalidJsonException() {
}
diff --git a/json-path/src/main/java/com/jayway/jsonpath/InvalidPathException.java b/json-path/src/main/java/com/jayway/jsonpath/InvalidPathException.java
index e95e7431..c28cb8b5 100644
--- a/json-path/src/main/java/com/jayway/jsonpath/InvalidPathException.java
+++ b/json-path/src/main/java/com/jayway/jsonpath/InvalidPathException.java
@@ -1,9 +1,21 @@
+/*
+ * Copyright 2011 the original author or authors.
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
package com.jayway.jsonpath;
/**
- * User: kalle stenflo
- * Date: 1/24/11
- * Time: 10:09 AM
+ * @author Kalle Stenflo
*/
public class InvalidPathException extends RuntimeException {
diff --git a/json-path/src/main/java/com/jayway/jsonpath/JsonModel.java b/json-path/src/main/java/com/jayway/jsonpath/JsonModel.java
index 73af18fc..778eb03c 100644
--- a/json-path/src/main/java/com/jayway/jsonpath/JsonModel.java
+++ b/json-path/src/main/java/com/jayway/jsonpath/JsonModel.java
@@ -1,63 +1,152 @@
+/*
+ * Copyright 2011 the original author or authors.
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
package com.jayway.jsonpath;
-import com.jayway.jsonpath.spi.JsonProvider;
+import com.jayway.jsonpath.spi.JsonProviderFactory;
+import org.codehaus.jackson.map.ObjectMapper;
+import org.codehaus.jackson.map.type.CollectionType;
+
+import java.util.List;
+import java.util.Set;
/**
- * Created by IntelliJ IDEA.
- * User: kallestenflo
- * Date: 11/8/11
- * Time: 7:40 PM
+ * @author Kalle Stenflo
*/
public class JsonModel {
+ private static ObjectMapper objectMapper;
+
private Object jsonObject;
- private JsonProvider jsonProvider;
+ // --------------------------------------------------------
+ //
+ // Constructors
+ //
+ // --------------------------------------------------------
public JsonModel(String json) {
- this.jsonProvider = JsonProvider.getInstance();
- this.jsonObject = jsonProvider.parse(json);
+ this(JsonProviderFactory.getInstance().parse(json));
}
private JsonModel(Object jsonObject) {
- this.jsonProvider = JsonProvider.getInstance();
this.jsonObject = jsonObject;
}
- public static JsonModel create(String json) {
- return new JsonModel(json);
- }
-
+ @SuppressWarnings({"unchecked"})
public T get(String jsonPath) {
- JsonPath path = JsonPath.compile(jsonProvider, jsonPath);
+ JsonPath path = JsonPath.compile(jsonPath);
return (T) get(path);
}
+ @SuppressWarnings({"unchecked"})
public T get(JsonPath jsonPath) {
return (T) jsonPath.read(jsonObject);
}
+ public String getJson() {
+ return JsonProviderFactory.getInstance().toJson(jsonObject);
+ }
+
+ public String getJson(String jsonPath) {
+ return JsonProviderFactory.getInstance().toJson(get(jsonPath));
+ }
+
public String getJson(JsonPath jsonPath) {
- return jsonProvider.toJson(jsonPath.read(jsonObject));
+ return JsonProviderFactory.getInstance().toJson(get(jsonPath));
}
- public JsonModel getModel(String jsonPath) {
- JsonPath path = JsonPath.compile(jsonProvider, jsonPath);
- return getModel(path);
+ public JsonModel getSubModel(String jsonPath) {
+ JsonPath path = JsonPath.compile(jsonPath);
+ return getSubModel(path);
}
- public JsonModel getModel(JsonPath jsonPath) {
+ public JsonModel getSubModel(JsonPath jsonPath) {
Object subModel = jsonPath.read(jsonObject);
-
return new JsonModel(subModel);
}
- public JsonProvider getJsonProvider() {
- return jsonProvider;
+
+ public MappingModelReader map(final String jsonPath) {
+
+ return new MappingModelReader() {
+
+ private ObjectMapper objectMapper = JsonModel.getObjectMapper();
+
+ @Override
+ public List toListOf(Class targetClass) {
+ Object model = JsonModel.this.get(jsonPath);
+ CollectionType colType = objectMapper.getTypeFactory().constructCollectionType(List.class, targetClass);
+ return objectMapper.convertValue(model, colType);
+ }
+
+ @Override
+ public Set toSetOf(Class targetClass) {
+ Object model = JsonModel.this.get(jsonPath);
+ CollectionType colType = objectMapper.getTypeFactory().constructCollectionType(Set.class, targetClass);
+ return objectMapper.convertValue(model, colType);
+ }
+
+ @Override
+ public T to(Class targetClass) {
+ Object model = JsonModel.this.get(jsonPath);
+ return objectMapper.convertValue(model, targetClass);
+ }
+ };
+ }
+
+ // --------------------------------------------------------
+ //
+ // Static factory methods
+ //
+ // --------------------------------------------------------
+ public static JsonModel create(String json) {
+ return new JsonModel(json);
+ }
+
+ public static JsonModel create(Object jsonObject) {
+ return new JsonModel(jsonObject);
}
-
- public String getJson(){
- return jsonProvider.toJson(jsonObject);
+
+
+ // --------------------------------------------------------
+ //
+ // Support interfaces
+ //
+ // --------------------------------------------------------
+ public interface MappingModelReader {
+ List toListOf(Class targetClass);
+
+ Set toSetOf(Class targetClass);
+
+ T to(Class targetClass);
}
+
+ // --------------------------------------------------------
+ //
+ // Private helpers
+ //
+ // --------------------------------------------------------
+ private static ObjectMapper getObjectMapper() {
+ if (JsonModel.objectMapper == null) {
+ synchronized (JsonModel.class) {
+ JsonModel.objectMapper = new ObjectMapper();
+ }
+ }
+ return JsonModel.objectMapper;
+ }
+
+
}
diff --git a/json-path/src/main/java/com/jayway/jsonpath/JsonPath.java b/json-path/src/main/java/com/jayway/jsonpath/JsonPath.java
index d600f384..494ac769 100644
--- a/json-path/src/main/java/com/jayway/jsonpath/JsonPath.java
+++ b/json-path/src/main/java/com/jayway/jsonpath/JsonPath.java
@@ -1,19 +1,33 @@
+/*
+ * Copyright 2011 the original author or authors.
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
package com.jayway.jsonpath;
-import com.jayway.jsonpath.reader.PathToken;
-import com.jayway.jsonpath.reader.PathTokenizer;
-import com.jayway.jsonpath.reader.filter.Filter;
-import com.jayway.jsonpath.spi.JsonProvider;
+import com.jayway.jsonpath.internal.PathToken;
+import com.jayway.jsonpath.internal.PathTokenizer;
+import com.jayway.jsonpath.internal.filter.Filter;
+import com.jayway.jsonpath.spi.JsonProviderFactory;
+import org.apache.commons.io.IOUtils;
+import java.io.*;
+import java.net.URL;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;
/**
- * User: kalle stenflo
- * Date: 2/2/11
- * Time: 1:03 PM
*
* JsonPath is to JSON what XPATH is to XML, a simple way to extract parts of a given document. JsonPath is
* available in many programming languages such as Javascript, Python and PHP.
@@ -71,6 +85,8 @@ import java.util.regex.Pattern;
*
* String author = JsonPath.read(json, "$.store.book[1].author")
*
+ *
+ * @author Kalle Stenflo
*/
public class JsonPath {
@@ -78,29 +94,14 @@ public class JsonPath {
private PathTokenizer tokenizer;
- private JsonProvider jsonProvider;
-
-
- /**
- * Creates a new JsonPath.
- *
- * @param jsonPath the path statement
- */
private JsonPath(String jsonPath) {
- this(JsonProvider.getInstance(), jsonPath);
- }
-
-
- private JsonPath(JsonProvider jsonProvider, String jsonPath) {
if (jsonPath == null ||
jsonPath.trim().isEmpty() ||
- jsonPath.matches("new ") ||
jsonPath.matches("[^\\?\\+\\=\\-\\*\\/\\!]\\(")) {
throw new InvalidPathException("Invalid path");
}
- this.jsonProvider = jsonProvider;
- this.tokenizer = new PathTokenizer(jsonPath, jsonProvider);
+ this.tokenizer = new PathTokenizer(jsonPath);
}
public String getPath() {
@@ -128,55 +129,115 @@ public class JsonPath {
* @return true if path is definite (points to single item)
*/
public boolean isPathDefinite() {
- //return !getPath().replaceAll("\"[^\"\\\\\\n\r]*\"", "").matches(".*(\\.\\.|\\*|\\[[\\\\/]|\\?|,|:\\s?\\]|\\[\\s?:|>|\\(|<|=|\\+).*");
-
String preparedPath = getPath().replaceAll("\"[^\"\\\\\\n\r]*\"", "");
return !DEFINITE_PATH_PATTERN.matcher(preparedPath).matches();
-
}
/**
- * Applies this container path to the provided object
+ * Applies this JsonPath to the provided json document.
+ * Note that the document must either a {@link List} or a {@link Map}
*
- * @param container a container Object
- * @param
+ * @param json a container Object ({@link List} or {@link Map})
+ * @param expected return type
* @return list of objects matched by the given path
*/
- public T read(Object container) {
- if (!(container instanceof Map) && !(container instanceof List)) {
+ @SuppressWarnings({"unchecked"})
+ public T read(Object json) {
+ if (!(json instanceof Map) && !(json instanceof List)) {
throw new IllegalArgumentException("Invalid container object");
}
- Object result = container;
+ Object result = json;
boolean inArrayContext = false;
for (PathToken pathToken : tokenizer) {
Filter filter = pathToken.getFilter();
- result = filter.filter(result, jsonProvider, inArrayContext);
+ result = filter.filter(result, JsonProviderFactory.getInstance(), inArrayContext);
if (!inArrayContext) {
inArrayContext = filter.isArrayFilter();
}
- //result = pathToken.filter(result, jsonProvider);
}
return (T) result;
}
/**
- * Applies this json path to the provided object
+ * Applies this JsonPath to the provided json string
*
* @param json a json string
- * @param
+ * @param expected return type
* @return list of objects matched by the given path
*/
+ @SuppressWarnings({"unchecked"})
public T read(String json) {
- return (T) read(jsonProvider.parse(json));
+ return (T) read(JsonProviderFactory.getInstance().parse(json));
+ }
+
+ /**
+ * Applies this JsonPath to the provided json URL
+ *
+ * @param jsonURL url to read from
+ * @param expected return type
+ * @return list of objects matched by the given path
+ * @throws IOException
+ */
+ @SuppressWarnings({"unchecked"})
+ public T read(URL jsonURL) throws IOException {
+ BufferedReader in = null;
+ try {
+ in = new BufferedReader(new InputStreamReader(jsonURL.openStream()));
+ return (T) read(JsonProviderFactory.getInstance().parse(in));
+ } finally {
+ IOUtils.closeQuietly(in);
+ }
}
/**
- * Compiles a JsonPath from the given string
+ * Applies this JsonPath to the provided json file
+ *
+ * @param jsonFile file to read from
+ * @param expected return type
+ * @return list of objects matched by the given path
+ * @throws IOException
+ */
+ @SuppressWarnings({"unchecked"})
+ public T read(File jsonFile) throws IOException {
+ FileInputStream fis = null;
+ try {
+ fis = new FileInputStream(jsonFile);
+ return (T) read(JsonProviderFactory.getInstance().parse(fis));
+ } finally {
+ IOUtils.closeQuietly(fis);
+ }
+ }
+
+ /**
+ * Applies this JsonPath to the provided json input stream
+ *
+ * @param jsonInputStream input stream to read from
+ * @param expected return type
+ * @return list of objects matched by the given path
+ * @throws IOException
+ */
+ @SuppressWarnings({"unchecked"})
+ public T read(InputStream jsonInputStream) throws IOException {
+ try {
+ return (T) read(JsonProviderFactory.getInstance().parse(jsonInputStream));
+ } finally {
+ IOUtils.closeQuietly(jsonInputStream);
+ }
+ }
+
+ // --------------------------------------------------------
+ //
+ // Static factory methods
+ //
+ // --------------------------------------------------------
+
+ /**
+ * Compiles a JsonPath
*
* @param jsonPath to compile
* @return compiled JsonPath
@@ -185,18 +246,22 @@ public class JsonPath {
return new JsonPath(jsonPath);
}
- public static JsonPath compile(JsonProvider provider, String jsonPath) {
- return new JsonPath(provider, jsonPath);
- }
+
+ // --------------------------------------------------------
+ //
+ // Static utility functions
+ //
+ // --------------------------------------------------------
/**
* Creates a new JsonPath and applies it to the provided Json string
*
* @param json a json string
* @param jsonPath the json path
- * @param
+ * @param expected return type
* @return list of objects matched by the given path
*/
+ @SuppressWarnings({"unchecked"})
public static T read(String json, String jsonPath) {
return (T) compile(jsonPath).read(json);
}
@@ -206,11 +271,51 @@ public class JsonPath {
*
* @param json a json object
* @param jsonPath the json path
- * @param
+ * @param expected return type
* @return list of objects matched by the given path
*/
+ @SuppressWarnings({"unchecked"})
public static T read(Object json, String jsonPath) {
return (T) compile(jsonPath).read(json);
}
+ /**
+ * Creates a new JsonPath and applies it to the provided Json object
+ *
+ * @param jsonURL url pointing to json doc
+ * @param jsonPath the json path
+ * @param expected return type
+ * @return list of objects matched by the given path
+ */
+ @SuppressWarnings({"unchecked"})
+ public static T read(URL jsonURL, String jsonPath) throws IOException {
+ return (T) compile(jsonPath).read(jsonURL);
+ }
+
+ /**
+ * Creates a new JsonPath and applies it to the provided Json object
+ *
+ * @param jsonFile json file
+ * @param jsonPath the json path
+ * @param expected return type
+ * @return list of objects matched by the given path
+ */
+ @SuppressWarnings({"unchecked"})
+ public static T read(File jsonFile, String jsonPath) throws IOException {
+ return (T) compile(jsonPath).read(jsonFile);
+ }
+
+ /**
+ * Creates a new JsonPath and applies it to the provided Json object
+ *
+ * @param jsonInputStream json input stream
+ * @param jsonPath the json path
+ * @param expected return type
+ * @return list of objects matched by the given path
+ */
+ @SuppressWarnings({"unchecked"})
+ public static T read(InputStream jsonInputStream, String jsonPath) throws IOException {
+ return (T) compile(jsonPath).read(jsonInputStream);
+ }
+
}
diff --git a/json-path/src/main/java/com/jayway/jsonpath/internal/PathToken.java b/json-path/src/main/java/com/jayway/jsonpath/internal/PathToken.java
new file mode 100644
index 00000000..48edae04
--- /dev/null
+++ b/json-path/src/main/java/com/jayway/jsonpath/internal/PathToken.java
@@ -0,0 +1,43 @@
+/*
+ * Copyright 2011 the original author or authors.
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.jayway.jsonpath.internal;
+
+import com.jayway.jsonpath.internal.filter.Filter;
+import com.jayway.jsonpath.internal.filter.FilterFactory;
+import com.jayway.jsonpath.spi.JsonProvider;
+
+/**
+ * @author Kalle Stenflo
+ */
+public class PathToken {
+
+ private String fragment;
+
+ public PathToken(String fragment) {
+ this.fragment = fragment;
+ }
+
+ public Filter getFilter(){
+ return FilterFactory.createFilter(fragment);
+ }
+
+ public Object filter(Object model, JsonProvider jsonProvider){
+ return FilterFactory.createFilter(fragment).filter(model, jsonProvider);
+ }
+
+ public String getFragment() {
+ return fragment;
+ }
+}
diff --git a/json-path/src/main/java/com/jayway/jsonpath/reader/PathTokenizer.java b/json-path/src/main/java/com/jayway/jsonpath/internal/PathTokenizer.java
similarity index 88%
rename from json-path/src/main/java/com/jayway/jsonpath/reader/PathTokenizer.java
rename to json-path/src/main/java/com/jayway/jsonpath/internal/PathTokenizer.java
index eb5ee88e..efe0c7d1 100644
--- a/json-path/src/main/java/com/jayway/jsonpath/reader/PathTokenizer.java
+++ b/json-path/src/main/java/com/jayway/jsonpath/internal/PathTokenizer.java
@@ -1,17 +1,27 @@
-package com.jayway.jsonpath.reader;
+/*
+ * Copyright 2011 the original author or authors.
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.jayway.jsonpath.internal;
import com.jayway.jsonpath.InvalidPathException;
-import com.jayway.jsonpath.spi.JsonProvider;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
/**
- * Created by IntelliJ IDEA.
- * User: kallestenflo
- * Date: 11/4/11
- * Time: 9:53 PM
+ * @author Kalle Stenflo
*/
public class PathTokenizer implements Iterable {
@@ -20,7 +30,7 @@ public class PathTokenizer implements Iterable {
private int index = 0;
private List pathTokens = new LinkedList();
- public PathTokenizer(String jsonPath, JsonProvider jsonProvider) {
+ public PathTokenizer(String jsonPath) {
if (!jsonPath.startsWith("$") && !jsonPath.startsWith("$[")) {
jsonPath = "$." + jsonPath;
@@ -111,7 +121,6 @@ public class PathTokenizer implements Iterable {
StringBuilder sb = new StringBuilder();
while (!isEmpty() && (!isStopChar(peek(), stopChars))) {
-
if (peek() == '(') {
do {
sb.append(poll());
diff --git a/json-path/src/main/java/com/jayway/jsonpath/reader/filter/ArrayEvalFilter.java b/json-path/src/main/java/com/jayway/jsonpath/internal/filter/ArrayEvalFilter.java
similarity index 78%
rename from json-path/src/main/java/com/jayway/jsonpath/reader/filter/ArrayEvalFilter.java
rename to json-path/src/main/java/com/jayway/jsonpath/internal/filter/ArrayEvalFilter.java
index 39ce8ea7..15c3297b 100644
--- a/json-path/src/main/java/com/jayway/jsonpath/reader/filter/ArrayEvalFilter.java
+++ b/json-path/src/main/java/com/jayway/jsonpath/internal/filter/ArrayEvalFilter.java
@@ -1,7 +1,21 @@
-package com.jayway.jsonpath.reader.filter;
+/*
+ * Copyright 2011 the original author or authors.
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.jayway.jsonpath.internal.filter;
import com.jayway.jsonpath.InvalidPathException;
-import com.jayway.jsonpath.reader.filter.eval.ExpressionEvaluator;
+import com.jayway.jsonpath.internal.filter.eval.ExpressionEvaluator;
import com.jayway.jsonpath.spi.JsonProvider;
import java.util.List;
@@ -10,14 +24,11 @@ import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
- * Created by IntelliJ IDEA.
- * User: kallestenflo
- * Date: 11/5/11
- * Time: 12:35 AM
+ * @author Kalle Stenflo
*/
public class ArrayEvalFilter extends Filter {
- public static final Pattern PATTERN = Pattern.compile("(.*?)\\s?([=<>]+)\\s?(.*)");
+ private static final Pattern PATTERN = Pattern.compile("(.*?)\\s?([=<>]+)\\s?(.*)");
public ArrayEvalFilter(String condition) {
super(condition);
diff --git a/json-path/src/main/java/com/jayway/jsonpath/reader/filter/ArrayIndexFilter.java b/json-path/src/main/java/com/jayway/jsonpath/internal/filter/ArrayIndexFilter.java
similarity index 71%
rename from json-path/src/main/java/com/jayway/jsonpath/reader/filter/ArrayIndexFilter.java
rename to json-path/src/main/java/com/jayway/jsonpath/internal/filter/ArrayIndexFilter.java
index 840a731a..49039c1b 100644
--- a/json-path/src/main/java/com/jayway/jsonpath/reader/filter/ArrayIndexFilter.java
+++ b/json-path/src/main/java/com/jayway/jsonpath/internal/filter/ArrayIndexFilter.java
@@ -1,14 +1,25 @@
-package com.jayway.jsonpath.reader.filter;
+/*
+ * Copyright 2011 the original author or authors.
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.jayway.jsonpath.internal.filter;
import com.jayway.jsonpath.spi.JsonProvider;
import java.util.List;
/**
- * Created by IntelliJ IDEA.
- * User: kallestenflo
- * Date: 11/4/11
- * Time: 11:25 PM
+ * @author Kalle Stenflo
*/
public class ArrayIndexFilter extends Filter {
public ArrayIndexFilter(String condition) {
diff --git a/json-path/src/main/java/com/jayway/jsonpath/reader/filter/FieldFilter.java b/json-path/src/main/java/com/jayway/jsonpath/internal/filter/FieldFilter.java
similarity index 77%
rename from json-path/src/main/java/com/jayway/jsonpath/reader/filter/FieldFilter.java
rename to json-path/src/main/java/com/jayway/jsonpath/internal/filter/FieldFilter.java
index 82f940c3..623d44c0 100644
--- a/json-path/src/main/java/com/jayway/jsonpath/reader/filter/FieldFilter.java
+++ b/json-path/src/main/java/com/jayway/jsonpath/internal/filter/FieldFilter.java
@@ -1,4 +1,18 @@
-package com.jayway.jsonpath.reader.filter;
+/*
+ * Copyright 2011 the original author or authors.
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.jayway.jsonpath.internal.filter;
import com.jayway.jsonpath.spi.JsonProvider;
@@ -6,10 +20,7 @@ import java.util.List;
import java.util.Map;
/**
- * Created by IntelliJ IDEA.
- * User: kallestenflo
- * Date: 11/4/11
- * Time: 10:17 PM
+ * @author Kalle Stenflo
*/
public class FieldFilter extends Filter {
diff --git a/json-path/src/main/java/com/jayway/jsonpath/internal/filter/Filter.java b/json-path/src/main/java/com/jayway/jsonpath/internal/filter/Filter.java
new file mode 100644
index 00000000..2b6d5f5e
--- /dev/null
+++ b/json-path/src/main/java/com/jayway/jsonpath/internal/filter/Filter.java
@@ -0,0 +1,53 @@
+/*
+ * Copyright 2011 the original author or authors.
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.jayway.jsonpath.internal.filter;
+
+import com.jayway.jsonpath.spi.JsonProvider;
+
+/**
+ * @author Kalle Stenflo
+ */
+public abstract class Filter {
+
+ final String condition;
+
+ Filter(String condition) {
+ this.condition = condition;
+ }
+
+
+ String trim(String str, int front, int end) {
+ String res = str;
+
+ if (front > 0) {
+ res = str.substring(front);
+ }
+ if (end > 0) {
+ res = res.substring(0, res.length() - end);
+ }
+ return res;
+ }
+
+ public Object filter(Object obj, JsonProvider jsonProvider, boolean inArrayContext){
+ return filter(obj, jsonProvider);
+ }
+
+ public abstract Object filter(Object obj, JsonProvider jsonProvider);
+
+
+
+ public abstract boolean isArrayFilter();
+
+}
diff --git a/json-path/src/main/java/com/jayway/jsonpath/reader/filter/FilterFactory.java b/json-path/src/main/java/com/jayway/jsonpath/internal/filter/FilterFactory.java
similarity index 70%
rename from json-path/src/main/java/com/jayway/jsonpath/reader/filter/FilterFactory.java
rename to json-path/src/main/java/com/jayway/jsonpath/internal/filter/FilterFactory.java
index 60070b78..7ca88bc1 100644
--- a/json-path/src/main/java/com/jayway/jsonpath/reader/filter/FilterFactory.java
+++ b/json-path/src/main/java/com/jayway/jsonpath/internal/filter/FilterFactory.java
@@ -1,10 +1,21 @@
-package com.jayway.jsonpath.reader.filter;
+/*
+ * Copyright 2011 the original author or authors.
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.jayway.jsonpath.internal.filter;
/**
- * Created by IntelliJ IDEA.
- * User: kallestenflo
- * Date: 11/4/11
- * Time: 10:13 PM
+ * @author Kalle Stenflo
*/
public class FilterFactory {
diff --git a/json-path/src/main/java/com/jayway/jsonpath/reader/filter/HasFieldFilter.java b/json-path/src/main/java/com/jayway/jsonpath/internal/filter/HasFieldFilter.java
similarity index 62%
rename from json-path/src/main/java/com/jayway/jsonpath/reader/filter/HasFieldFilter.java
rename to json-path/src/main/java/com/jayway/jsonpath/internal/filter/HasFieldFilter.java
index 52e448f8..bccf223f 100644
--- a/json-path/src/main/java/com/jayway/jsonpath/reader/filter/HasFieldFilter.java
+++ b/json-path/src/main/java/com/jayway/jsonpath/internal/filter/HasFieldFilter.java
@@ -1,4 +1,18 @@
-package com.jayway.jsonpath.reader.filter;
+/*
+ * Copyright 2011 the original author or authors.
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.jayway.jsonpath.internal.filter;
import com.jayway.jsonpath.spi.JsonProvider;
@@ -6,10 +20,7 @@ import java.util.List;
import java.util.Map;
/**
- * Created by IntelliJ IDEA.
- * User: kallestenflo
- * Date: 11/5/11
- * Time: 12:17 AM
+ * @author Kalle Stenflo
*/
public class HasFieldFilter extends Filter {
diff --git a/json-path/src/main/java/com/jayway/jsonpath/internal/filter/PassthroughFilter.java b/json-path/src/main/java/com/jayway/jsonpath/internal/filter/PassthroughFilter.java
new file mode 100644
index 00000000..1881081e
--- /dev/null
+++ b/json-path/src/main/java/com/jayway/jsonpath/internal/filter/PassthroughFilter.java
@@ -0,0 +1,40 @@
+/*
+ * Copyright 2011 the original author or authors.
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.jayway.jsonpath.internal.filter;
+
+import com.jayway.jsonpath.spi.JsonProvider;
+
+/**
+ * @author Kalle Stenflo
+ */
+public class PassthroughFilter extends Filter {
+
+
+ private boolean isArrayFilter;
+
+ public PassthroughFilter(String condition, boolean isArrayFilter) {
+ super(condition);
+ this.isArrayFilter = isArrayFilter;
+ }
+
+ public Object filter(Object obj, JsonProvider jsonProvider) {
+ return obj;
+ }
+
+ @Override
+ public boolean isArrayFilter() {
+ return isArrayFilter;
+ }
+}
diff --git a/json-path/src/main/java/com/jayway/jsonpath/reader/filter/ScanFilter.java b/json-path/src/main/java/com/jayway/jsonpath/internal/filter/ScanFilter.java
similarity index 62%
rename from json-path/src/main/java/com/jayway/jsonpath/reader/filter/ScanFilter.java
rename to json-path/src/main/java/com/jayway/jsonpath/internal/filter/ScanFilter.java
index 49ae50ea..37472be6 100644
--- a/json-path/src/main/java/com/jayway/jsonpath/reader/filter/ScanFilter.java
+++ b/json-path/src/main/java/com/jayway/jsonpath/internal/filter/ScanFilter.java
@@ -1,4 +1,18 @@
-package com.jayway.jsonpath.reader.filter;
+/*
+ * Copyright 2011 the original author or authors.
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.jayway.jsonpath.internal.filter;
import com.jayway.jsonpath.spi.JsonProvider;
@@ -6,10 +20,7 @@ import com.jayway.jsonpath.spi.JsonProvider;
import java.util.List;
/**
- * Created by IntelliJ IDEA.
- * User: kallestenflo
- * Date: 11/7/11
- * Time: 12:31 PM
+ * @author Kalle Stenflo
*/
public class ScanFilter extends Filter {
diff --git a/json-path/src/main/java/com/jayway/jsonpath/reader/filter/WildcardFilter.java b/json-path/src/main/java/com/jayway/jsonpath/internal/filter/WildcardFilter.java
similarity index 55%
rename from json-path/src/main/java/com/jayway/jsonpath/reader/filter/WildcardFilter.java
rename to json-path/src/main/java/com/jayway/jsonpath/internal/filter/WildcardFilter.java
index 04177710..3034ab9d 100644
--- a/json-path/src/main/java/com/jayway/jsonpath/reader/filter/WildcardFilter.java
+++ b/json-path/src/main/java/com/jayway/jsonpath/internal/filter/WildcardFilter.java
@@ -1,14 +1,25 @@
-package com.jayway.jsonpath.reader.filter;
+/*
+ * Copyright 2011 the original author or authors.
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.jayway.jsonpath.internal.filter;
import com.jayway.jsonpath.spi.JsonProvider;
import java.util.List;
/**
- * Created by IntelliJ IDEA.
- * User: kallestenflo
- * Date: 11/7/11
- * Time: 1:59 PM
+ * @author Kalle Stenflo
*/
public class WildcardFilter extends Filter {
diff --git a/json-path/src/main/java/com/jayway/jsonpath/reader/filter/eval/ExpressionEvaluator.java b/json-path/src/main/java/com/jayway/jsonpath/internal/filter/eval/ExpressionEvaluator.java
similarity index 82%
rename from json-path/src/main/java/com/jayway/jsonpath/reader/filter/eval/ExpressionEvaluator.java
rename to json-path/src/main/java/com/jayway/jsonpath/internal/filter/eval/ExpressionEvaluator.java
index 82aac5a1..91928bcc 100644
--- a/json-path/src/main/java/com/jayway/jsonpath/reader/filter/eval/ExpressionEvaluator.java
+++ b/json-path/src/main/java/com/jayway/jsonpath/internal/filter/eval/ExpressionEvaluator.java
@@ -1,13 +1,24 @@
-package com.jayway.jsonpath.reader.filter.eval;
+/*
+ * Copyright 2011 the original author or authors.
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.jayway.jsonpath.internal.filter.eval;
/**
- * User: kalle stenflo
- * Date: 2/4/11
- * Time: 9:21 PM
+ * @author Kalle Stenflo
*/
public class ExpressionEvaluator {
-
public static boolean eval(T actual, String comparator, String expected) {
comparator = comparator.trim();
diff --git a/json-path/src/main/java/com/jayway/jsonpath/reader/PathToken.java b/json-path/src/main/java/com/jayway/jsonpath/reader/PathToken.java
deleted file mode 100644
index cdacd552..00000000
--- a/json-path/src/main/java/com/jayway/jsonpath/reader/PathToken.java
+++ /dev/null
@@ -1,32 +0,0 @@
-package com.jayway.jsonpath.reader;
-
-import com.jayway.jsonpath.reader.filter.Filter;
-import com.jayway.jsonpath.reader.filter.FilterFactory;
-import com.jayway.jsonpath.spi.JsonProvider;
-
-/**
- * Created by IntelliJ IDEA.
- * User: kallestenflo
- * Date: 11/4/11
- * Time: 10:00 PM
- */
-public class PathToken {
-
- private String fragment;
-
- public PathToken(String fragment) {
- this.fragment = fragment;
- }
-
- public Filter getFilter(){
- return FilterFactory.createFilter(fragment);
- }
-
- public Object filter(Object model, JsonProvider jsonProvider){
- return FilterFactory.createFilter(fragment).filter(model, jsonProvider);
- }
-
- public String getFragment() {
- return fragment;
- }
-}
diff --git a/json-path/src/main/java/com/jayway/jsonpath/reader/filter/Filter.java b/json-path/src/main/java/com/jayway/jsonpath/reader/filter/Filter.java
deleted file mode 100644
index 72bbf99e..00000000
--- a/json-path/src/main/java/com/jayway/jsonpath/reader/filter/Filter.java
+++ /dev/null
@@ -1,42 +0,0 @@
-package com.jayway.jsonpath.reader.filter;
-
-import com.jayway.jsonpath.spi.JsonProvider;
-
-/**
- * Created by IntelliJ IDEA.
- * User: kallestenflo
- * Date: 11/4/11
- * Time: 10:14 PM
- */
-public abstract class Filter {
-
- protected final String condition;
-
- public Filter(String condition) {
- this.condition = condition;
- }
-
-
- protected String trim(String str, int front, int end) {
- String res = str;
-
- if (front > 0) {
- res = str.substring(front);
- }
- if (end > 0) {
- res = res.substring(0, res.length() - end);
- }
- return res;
- }
-
- public Object filter(Object obj, JsonProvider jsonProvider, boolean inArrayContext){
- return filter(obj, jsonProvider);
- }
-
- public abstract Object filter(Object obj, JsonProvider jsonProvider);
-
-
-
- public abstract boolean isArrayFilter();
-
-}
diff --git a/json-path/src/main/java/com/jayway/jsonpath/reader/filter/PassthroughFilter.java b/json-path/src/main/java/com/jayway/jsonpath/reader/filter/PassthroughFilter.java
deleted file mode 100644
index 14020874..00000000
--- a/json-path/src/main/java/com/jayway/jsonpath/reader/filter/PassthroughFilter.java
+++ /dev/null
@@ -1,29 +0,0 @@
-package com.jayway.jsonpath.reader.filter;
-
-import com.jayway.jsonpath.spi.JsonProvider;
-
-/**
- * Created by IntelliJ IDEA.
- * User: kallestenflo
- * Date: 11/4/11
- * Time: 10:15 PM
- */
-public class PassthroughFilter extends Filter {
-
-
- private boolean isArrayFilter;
-
- public PassthroughFilter(String condition, boolean isArrayFilter) {
- super(condition);
- this.isArrayFilter = isArrayFilter;
- }
-
- public Object filter(Object obj, JsonProvider jsonProvider) {
- return obj;
- }
-
- @Override
- public boolean isArrayFilter() {
- return isArrayFilter;
- }
-}
diff --git a/json-path/src/main/java/com/jayway/jsonpath/spi/JsonProvider.java b/json-path/src/main/java/com/jayway/jsonpath/spi/JsonProvider.java
index cc95eb42..b3d674aa 100644
--- a/json-path/src/main/java/com/jayway/jsonpath/spi/JsonProvider.java
+++ b/json-path/src/main/java/com/jayway/jsonpath/spi/JsonProvider.java
@@ -1,34 +1,45 @@
+/*
+ * Copyright 2011 the original author or authors.
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
package com.jayway.jsonpath.spi;
import com.jayway.jsonpath.InvalidJsonException;
-import com.jayway.jsonpath.spi.impl.JacksonProvider;
-import com.jayway.jsonpath.spi.impl.JsonSmartProvider;
+import java.io.InputStream;
+import java.io.Reader;
import java.util.List;
import java.util.Map;
/**
- * Created by IntelliJ IDEA.
- * User: kallestenflo
- * Date: 11/8/11
- * Time: 3:51 PM
+ * @author Kalle Stenflo
*/
-public abstract class JsonProvider {
+public interface JsonProvider {
+
+ Mode getMode();
+
+ Object parse(String json) throws InvalidJsonException;
- public abstract Mode getMode();
+ Object parse(Reader jsonReader) throws InvalidJsonException;
- public abstract Object parse(String json) throws InvalidJsonException;
+ Object parse(InputStream jsonStream) throws InvalidJsonException;
- public abstract String toJson(Object obj);
+ String toJson(Object obj);
- public abstract Map createMap();
+ Map createMap();
- public abstract List