Browse Source

Refactored code structure.

Added licence heade
Continued SPI work
Added File, InputStream, URL readers
pull/7/head
Kalle Stenflo 13 years ago
parent
commit
242b724baf
  1. 3
      json-path-assert/src/main/java/com/jayway/jsonassert/JsonAssert.java
  2. 25
      json-path/pom.xml
  3. 21
      json-path/src/main/java/com/jayway/jsonpath/InvalidJsonException.java
  4. 18
      json-path/src/main/java/com/jayway/jsonpath/InvalidPathException.java
  5. 139
      json-path/src/main/java/com/jayway/jsonpath/JsonModel.java
  6. 191
      json-path/src/main/java/com/jayway/jsonpath/JsonPath.java
  7. 43
      json-path/src/main/java/com/jayway/jsonpath/internal/PathToken.java
  8. 25
      json-path/src/main/java/com/jayway/jsonpath/internal/PathTokenizer.java
  9. 25
      json-path/src/main/java/com/jayway/jsonpath/internal/filter/ArrayEvalFilter.java
  10. 21
      json-path/src/main/java/com/jayway/jsonpath/internal/filter/ArrayIndexFilter.java
  11. 21
      json-path/src/main/java/com/jayway/jsonpath/internal/filter/FieldFilter.java
  12. 53
      json-path/src/main/java/com/jayway/jsonpath/internal/filter/Filter.java
  13. 21
      json-path/src/main/java/com/jayway/jsonpath/internal/filter/FilterFactory.java
  14. 21
      json-path/src/main/java/com/jayway/jsonpath/internal/filter/HasFieldFilter.java
  15. 40
      json-path/src/main/java/com/jayway/jsonpath/internal/filter/PassthroughFilter.java
  16. 21
      json-path/src/main/java/com/jayway/jsonpath/internal/filter/ScanFilter.java
  17. 21
      json-path/src/main/java/com/jayway/jsonpath/internal/filter/WildcardFilter.java
  18. 21
      json-path/src/main/java/com/jayway/jsonpath/internal/filter/eval/ExpressionEvaluator.java
  19. 32
      json-path/src/main/java/com/jayway/jsonpath/reader/PathToken.java
  20. 42
      json-path/src/main/java/com/jayway/jsonpath/reader/filter/Filter.java
  21. 29
      json-path/src/main/java/com/jayway/jsonpath/reader/filter/PassthroughFilter.java
  22. 67
      json-path/src/main/java/com/jayway/jsonpath/spi/JsonProvider.java
  23. 17
      json-path/src/main/java/com/jayway/jsonpath/spi/JsonProviderFactory.java
  24. 21
      json-path/src/main/java/com/jayway/jsonpath/spi/Mode.java
  25. 80
      json-path/src/main/java/com/jayway/jsonpath/spi/impl/AbstractJsonProvider.java
  26. 41
      json-path/src/main/java/com/jayway/jsonpath/spi/impl/JacksonProvider.java
  27. 43
      json-path/src/main/java/com/jayway/jsonpath/spi/impl/JsonSmartProvider.java
  28. 5
      json-path/src/test/java/com/jayway/jsonpath/ComplianceTest.java
  29. 8
      json-path/src/test/java/com/jayway/jsonpath/ExpressionEvalTest.java
  30. 30
      json-path/src/test/java/com/jayway/jsonpath/IsDefinitePathTest.java
  31. 2
      json-path/src/test/java/com/jayway/jsonpath/IssuesTest.java
  32. 148
      json-path/src/test/java/com/jayway/jsonpath/JsonModelMapperTest.java
  33. 6
      json-path/src/test/java/com/jayway/jsonpath/JsonModelTest.java
  34. 60
      json-path/src/test/java/com/jayway/jsonpath/JsonPathExtendedTest.java
  35. 7
      json-path/src/test/java/com/jayway/jsonpath/JsonPathTest.java
  36. 2
      json-path/src/test/java/com/jayway/jsonpath/JsonProviderTest.java
  37. 45
      json-path/src/test/java/com/jayway/jsonpath/ParserTest.java
  38. 38
      json-path/src/test/java/com/jayway/jsonpath/PathTest.java
  39. 2
      json-path/src/test/java/com/jayway/jsonpath/util/ScriptEngineJsonPath.java
  40. 51
      pom.xml

3
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;

25
json-path/pom.xml

@ -40,10 +40,15 @@
<artifactId>commons-lang</artifactId>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<scope>provided</scope>
<optional>true</optional>
</dependency>
<!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
@ -66,13 +71,13 @@
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<extensions>true</extensions>
</plugin>
</plugins>
</build>
<build>
<plugins>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<extensions>true</extensions>
</plugin>
</plugins>
</build>
</project>

21
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() {
}

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

139
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> T get(String jsonPath) {
JsonPath path = JsonPath.compile(jsonProvider, jsonPath);
JsonPath path = JsonPath.compile(jsonPath);
return (T) get(path);
}
@SuppressWarnings({"unchecked"})
public <T> 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 <T> List<T> toListOf(Class<T> targetClass) {
Object model = JsonModel.this.get(jsonPath);
CollectionType colType = objectMapper.getTypeFactory().constructCollectionType(List.class, targetClass);
return objectMapper.convertValue(model, colType);
}
@Override
public <T> Set<T> toSetOf(Class<T> targetClass) {
Object model = JsonModel.this.get(jsonPath);
CollectionType colType = objectMapper.getTypeFactory().constructCollectionType(Set.class, targetClass);
return objectMapper.convertValue(model, colType);
}
@Override
public <T> T to(Class<T> 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 {
<T> List<T> toListOf(Class<T> targetClass);
<T> Set<T> toSetOf(Class<T> targetClass);
<T> T to(Class<T> targetClass);
}
// --------------------------------------------------------
//
// Private helpers
//
// --------------------------------------------------------
private static ObjectMapper getObjectMapper() {
if (JsonModel.objectMapper == null) {
synchronized (JsonModel.class) {
JsonModel.objectMapper = new ObjectMapper();
}
}
return JsonModel.objectMapper;
}
}

191
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
* <p/>
* 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;
* <code>
* String author = JsonPath.read(json, "$.store.book[1].author")
* </code>
*
* @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 <T>
* @param json a container Object ({@link List} or {@link Map})
* @param <T> expected return type
* @return list of objects matched by the given path
*/
public <T> T read(Object container) {
if (!(container instanceof Map) && !(container instanceof List)) {
@SuppressWarnings({"unchecked"})
public <T> 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 <T>
* @param <T> expected return type
* @return list of objects matched by the given path
*/
@SuppressWarnings({"unchecked"})
public <T> 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 <T> expected return type
* @return list of objects matched by the given path
* @throws IOException
*/
@SuppressWarnings({"unchecked"})
public <T> 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 <T> expected return type
* @return list of objects matched by the given path
* @throws IOException
*/
@SuppressWarnings({"unchecked"})
public <T> 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 <T> expected return type
* @return list of objects matched by the given path
* @throws IOException
*/
@SuppressWarnings({"unchecked"})
public <T> 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 <T>
* @param <T> expected return type
* @return list of objects matched by the given path
*/
@SuppressWarnings({"unchecked"})
public static <T> 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 <T>
* @param <T> expected return type
* @return list of objects matched by the given path
*/
@SuppressWarnings({"unchecked"})
public static <T> 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 <T> expected return type
* @return list of objects matched by the given path
*/
@SuppressWarnings({"unchecked"})
public static <T> 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 <T> expected return type
* @return list of objects matched by the given path
*/
@SuppressWarnings({"unchecked"})
public static <T> 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 <T> expected return type
* @return list of objects matched by the given path
*/
@SuppressWarnings({"unchecked"})
public static <T> T read(InputStream jsonInputStream, String jsonPath) throws IOException {
return (T) compile(jsonPath).read(jsonInputStream);
}
}

43
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;
}
}

25
json-path/src/main/java/com/jayway/jsonpath/reader/PathTokenizer.java → 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<PathToken> {
@ -20,7 +30,7 @@ public class PathTokenizer implements Iterable<PathToken> {
private int index = 0;
private List<PathToken> pathTokens = new LinkedList<PathToken>();
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<PathToken> {
StringBuilder sb = new StringBuilder();
while (!isEmpty() && (!isStopChar(peek(), stopChars))) {
if (peek() == '(') {
do {
sb.append(poll());

25
json-path/src/main/java/com/jayway/jsonpath/reader/filter/ArrayEvalFilter.java → 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);

21
json-path/src/main/java/com/jayway/jsonpath/reader/filter/ArrayIndexFilter.java → 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) {

21
json-path/src/main/java/com/jayway/jsonpath/reader/filter/FieldFilter.java → 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 {

53
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();
}

21
json-path/src/main/java/com/jayway/jsonpath/reader/filter/FilterFactory.java → 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 {

21
json-path/src/main/java/com/jayway/jsonpath/reader/filter/HasFieldFilter.java → 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 {

40
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;
}
}

21
json-path/src/main/java/com/jayway/jsonpath/reader/filter/ScanFilter.java → 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 {

21
json-path/src/main/java/com/jayway/jsonpath/reader/filter/WildcardFilter.java → 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 {

21
json-path/src/main/java/com/jayway/jsonpath/reader/filter/eval/ExpressionEvaluator.java → 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 <T> boolean eval(T actual, String comparator, String expected) {
comparator = comparator.trim();

32
json-path/src/main/java/com/jayway/jsonpath/reader/PathToken.java

@ -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;
}
}

42
json-path/src/main/java/com/jayway/jsonpath/reader/filter/Filter.java

@ -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();
}

29
json-path/src/main/java/com/jayway/jsonpath/reader/filter/PassthroughFilter.java

@ -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;
}
}

67
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<String, Object> createMap();
Map<String, Object> createMap();
public abstract List<Object> createList();
List<Object> createList();
public static JsonProvider getInstance(){
return new JsonSmartProvider();
//return new JacksonProvider();
}
/**
* checks if object is <code>instanceof</code> <code>java.util.List</code> or <code>java.util.Map</code>
@ -36,9 +47,7 @@ public abstract class JsonProvider {
* @param obj object to check
* @return true if List or Map
*/
public boolean isContainer(Object obj) {
return (isList(obj) || isMap(obj));
}
boolean isContainer(Object obj);
/**
* checks if object is <code>instanceof</code> <code>java.util.List</code>
@ -46,9 +55,7 @@ public abstract class JsonProvider {
* @param obj object to check
* @return true if List
*/
public boolean isList(Object obj) {
return (obj instanceof List);
}
boolean isList(Object obj);
/**
* Converts give object to a List
@ -56,9 +63,7 @@ public abstract class JsonProvider {
* @param list
* @return
*/
public List<Object> toList(Object list) {
return (List<Object>) list;
}
List<Object> toList(Object list);
/**
@ -67,9 +72,7 @@ public abstract class JsonProvider {
* @param map
* @return
*/
public Map<String, Object> toMap(Object map) {
return (Map<String, Object>) map;
}
Map<String, Object> toMap(Object map);
/**
* Extracts a value from a Map
@ -78,9 +81,7 @@ public abstract class JsonProvider {
* @param key
* @return
*/
public Object getMapValue(Object map, String key) {
return toMap(map).get(key);
}
Object getMapValue(Object map, String key);
/**
* checks if object is <code>instanceof</code> <code>java.util.Map</code>
@ -88,9 +89,7 @@ public abstract class JsonProvider {
* @param obj object to check
* @return true if Map
*/
public boolean isMap(Object obj) {
return (obj instanceof Map);
}
boolean isMap(Object obj);
}

17
json-path/src/main/java/com/jayway/jsonpath/spi/JsonProviderFactory.java

@ -0,0 +1,17 @@
package com.jayway.jsonpath.spi;
import com.jayway.jsonpath.spi.impl.JsonSmartProvider;
/**
* Created by IntelliJ IDEA.
* User: kallestenflo
* Date: 3/2/12
* Time: 9:45 PM
*/
public abstract class JsonProviderFactory {
public static JsonProvider getInstance() {
return new JsonSmartProvider();
}
}

21
json-path/src/main/java/com/jayway/jsonpath/spi/Mode.java

@ -1,12 +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.spi;
/**
* Created by IntelliJ IDEA.
* User: kallestenflo
* Date: 11/8/11
* Time: 7:22 PM
* @author Kalle Stenflo
*/
public enum Mode {

80
json-path/src/main/java/com/jayway/jsonpath/spi/impl/AbstractJsonProvider.java

@ -0,0 +1,80 @@
package com.jayway.jsonpath.spi.impl;
import com.jayway.jsonpath.spi.JsonProvider;
import java.util.List;
import java.util.Map;
/**
* Created by IntelliJ IDEA.
* User: kallestenflo
* Date: 3/2/12
* Time: 9:56 PM
*/
public abstract class AbstractJsonProvider implements JsonProvider {
/**
* checks if object is <code>instanceof</code> <code>java.util.List</code> or <code>java.util.Map</code>
*
* @param obj object to check
* @return true if List or Map
*/
public boolean isContainer(Object obj) {
return (isList(obj) || isMap(obj));
}
/**
* checks if object is <code>instanceof</code> <code>java.util.List</code>
*
* @param obj object to check
* @return true if List
*/
public boolean isList(Object obj) {
return (obj instanceof List);
}
/**
* Converts give object to a List
*
* @param list
* @return
*/
@SuppressWarnings({"unchecked"})
public List<Object> toList(Object list) {
return (List<Object>) list;
}
/**
* Converts given object to a Map
*
* @param map
* @return
*/
@SuppressWarnings({"unchecked"})
public Map<String, Object> toMap(Object map) {
return (Map<String, Object>) map;
}
/**
* Extracts a value from a Map
*
* @param map
* @param key
* @return
*/
public Object getMapValue(Object map, String key) {
return toMap(map).get(key);
}
/**
* checks if object is <code>instanceof</code> <code>java.util.Map</code>
*
* @param obj object to check
* @return true if Map
*/
public boolean isMap(Object obj) {
return (obj instanceof Map);
}
}

41
json-path/src/main/java/com/jayway/jsonpath/spi/impl/JacksonProvider.java

@ -1,3 +1,17 @@
/*
* 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.impl;
import com.jayway.jsonpath.InvalidJsonException;
@ -6,18 +20,17 @@ import com.jayway.jsonpath.spi.Mode;
import org.codehaus.jackson.map.ObjectMapper;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
/**
* Created by IntelliJ IDEA.
* User: kallestenflo
* Date: 11/8/11
* Time: 10:34 PM
* @author Kalle Stenflo
*/
public class JacksonProvider extends JsonProvider {
public class JacksonProvider extends AbstractJsonProvider {
@Override
public Mode getMode() {
return Mode.STRICT;
@ -32,6 +45,24 @@ public class JacksonProvider extends JsonProvider {
}
}
@Override
public Object parse(Reader jsonReader) throws InvalidJsonException {
try {
return new ObjectMapper().readValue(jsonReader, Object.class);
} catch (IOException e) {
throw new InvalidJsonException(e);
}
}
@Override
public Object parse(InputStream jsonStream) throws InvalidJsonException {
try {
return new ObjectMapper().readValue(jsonStream, Object.class);
} catch (IOException e) {
throw new InvalidJsonException(e);
}
}
@Override
public String toJson(Object obj) {
throw new UnsupportedOperationException();

43
json-path/src/main/java/com/jayway/jsonpath/spi/impl/JsonSmartProvider.java

@ -1,3 +1,17 @@
/*
* 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.impl;
import com.jayway.jsonpath.InvalidJsonException;
@ -9,16 +23,17 @@ import net.minidev.json.JSONObject;
import net.minidev.json.parser.JSONParser;
import net.minidev.json.parser.ParseException;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.List;
import java.util.Map;
/**
* Created by IntelliJ IDEA.
* User: kallestenflo
* Date: 11/8/11
* Time: 3:56 PM
* @author Kalle Stenflo
*/
public class JsonSmartProvider extends JsonProvider {
public class JsonSmartProvider extends AbstractJsonProvider {
private Mode mode;
@ -49,6 +64,24 @@ public class JsonSmartProvider extends JsonProvider {
}
}
@Override
public Object parse(Reader jsonReader) throws InvalidJsonException {
try {
return parser.parse(jsonReader);
} catch (ParseException e) {
throw new InvalidJsonException(e);
}
}
@Override
public Object parse(InputStream jsonStream) throws InvalidJsonException {
try {
return parser.parse(new InputStreamReader(jsonStream));
} catch (ParseException e) {
throw new InvalidJsonException(e);
}
}
@Override
public String toJson(Object obj) {
if(!(obj instanceof JSONAware)){

5
json-path/src/test/java/com/jayway/jsonpath/ComplianceTests.java → json-path/src/test/java/com/jayway/jsonpath/ComplianceTest.java

@ -1,7 +1,5 @@
package com.jayway.jsonpath;
import com.jayway.jsonpath.reader.PathToken;
import com.jayway.jsonpath.reader.PathTokenizer;
import org.hamcrest.Matchers;
import org.junit.Test;
@ -15,7 +13,7 @@ import static org.junit.Assert.assertThat;
/**
* test defined in http://jsonpath.googlecode.com/svn/trunk/tests/jsonpath-test-js.html
*/
public class ComplianceTests {
public class ComplianceTest {
@Test
public void test_one() throws Exception {
@ -82,6 +80,7 @@ public class ComplianceTests {
//assertThat(JsonPath.<List<Integer>>read(json, "$['points'][?(@.x * @.x + @.y * @.y > 50)].id"), hasItems(?)); //low
}
@SuppressWarnings("UnusedAssignment")
@Test
public void test_four() throws Exception {
String json = "{ \"menu\": {\n" +

8
json-path/src/test/java/com/jayway/jsonpath/ExpressionEvalTest.java

@ -1,6 +1,6 @@
package com.jayway.jsonpath;
import com.jayway.jsonpath.reader.filter.eval.ExpressionEvaluator;
import com.jayway.jsonpath.internal.filter.eval.ExpressionEvaluator;
import org.junit.Test;
import static org.junit.Assert.assertFalse;
@ -14,12 +14,11 @@ import static org.junit.Assert.assertTrue;
*/
public class ExpressionEvalTest {
@Test
public void long_eval() throws Exception {
assertTrue(ExpressionEvaluator.eval(1L, "=", "1"));
assertTrue(ExpressionEvaluator.eval(1L, "==", "1"));
assertTrue(ExpressionEvaluator.eval(2L, "!=", "1"));
assertTrue(ExpressionEvaluator.eval(2L, ">", "1"));
assertTrue(ExpressionEvaluator.eval(2L, ">=", "1"));
@ -39,6 +38,7 @@ public class ExpressionEvalTest {
public void double_eval() throws Exception {
assertTrue(ExpressionEvaluator.eval(1D, "=", "1"));
assertTrue(ExpressionEvaluator.eval(1D, "==", "1"));
assertTrue(ExpressionEvaluator.eval(2D, "!=", "1"));
assertTrue(ExpressionEvaluator.eval(2D, ">", "1"));
assertTrue(ExpressionEvaluator.eval(2D, ">=", "1"));
@ -57,7 +57,7 @@ public class ExpressionEvalTest {
@Test
public void string_eval() throws Exception {
assertTrue(ExpressionEvaluator.eval("A", "=", "A"));
assertTrue(ExpressionEvaluator.eval("A", "==", "A"));
assertTrue(ExpressionEvaluator.eval("B", "!=", "A"));
}

30
json-path/src/test/java/com/jayway/jsonpath/IsDefinitePathTest.java

@ -1,30 +0,0 @@
package com.jayway.jsonpath;
import org.junit.Test;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
/**
* Created by IntelliJ IDEA.
* User: kallestenflo
* Date: 2/3/11
* Time: 9:58 PM
*/
public class IsDefinitePathTest {
@Test
public void path_is_not_definite() throws Exception {
assertFalse(JsonPath.compile("$..book[0]").isPathDefinite());
assertFalse(JsonPath.compile("$.books[*]").isPathDefinite());
}
@Test
public void path_is_definite() throws Exception {
assertTrue(JsonPath.compile("$.definite.this.is").isPathDefinite());
assertTrue(JsonPath.compile("rows[0].id").isPathDefinite());
}
}

2
json-path/src/test/java/com/jayway/jsonpath/IssueTests.java → json-path/src/test/java/com/jayway/jsonpath/IssuesTest.java

@ -10,7 +10,7 @@ import static junit.framework.Assert.assertNull;
* Date: 2/29/12
* Time: 8:42 AM
*/
public class IssueTests {
public class IssuesTest {
@Test
public void issue_7() throws Exception {

148
json-path/src/test/java/com/jayway/jsonpath/JsonModelMapperTest.java

@ -0,0 +1,148 @@
package com.jayway.jsonpath;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.type.TypeReference;
import org.junit.Test;
import java.util.Collection;
import java.util.List;
import java.util.Set;
import static org.junit.Assert.assertEquals;
/**
* Created by IntelliJ IDEA.
* User: kallestenflo
* Date: 3/2/12
* Time: 10:50 AM
*/
public class JsonModelMapperTest {
public final static String DOCUMENT =
"{ \"store\": {\n" +
" \"book\": [ \n" +
" { \"category\": \"reference\",\n" +
" \"author\": \"Nigel Rees\",\n" +
" \"title\": \"Sayings of the Century\",\n" +
" \"displayPrice\": 8.95\n" +
" },\n" +
" { \"category\": \"fiction\",\n" +
" \"author\": \"Evelyn Waugh\",\n" +
" \"title\": \"Sword of Honour\",\n" +
" \"displayPrice\": 12.99\n" +
" },\n" +
" { \"category\": \"fiction\",\n" +
" \"author\": \"Herman Melville\",\n" +
" \"title\": \"Moby Dick\",\n" +
" \"isbn\": \"0-553-21311-3\",\n" +
" \"displayPrice\": 8.99\n" +
" },\n" +
" { \"category\": \"fiction\",\n" +
" \"author\": \"J. R. R. Tolkien\",\n" +
" \"title\": \"The Lord of the Rings\",\n" +
" \"isbn\": \"0-395-19395-8\",\n" +
" \"displayPrice\": 22.99\n" +
" }\n" +
" ],\n" +
" \"bicycle\": {\n" +
" \"color\": \"red\",\n" +
" \"displayPrice\": 19.95,\n" +
" \"foo:bar\": \"fooBar\",\n" +
" \"dot.notation\": \"new\",\n" +
" \"dash-notation\": \"dashes\"\n" +
" }\n" +
" }\n" +
"}";
@Test
public void map_a_json_model() throws Exception {
JsonModel model = JsonModel.create(DOCUMENT);
List<Book> booksList = model.map("$.store.book[0,1]").toListOf(Book.class);
Set<Book> bookSet = model.map("$.store.book[0,1]").toSetOf(Book.class);
Book book = model.map("$.store.book[1]").to(Book.class);
System.out.println("test");
}
@Test
public void a_book_can_be_mapped() throws Exception {
//JsonPath.convert(DOCUMENT, "$.store.book[0,1]", List.class).to()
//List books = JsonPath.read(DOCUMENT, "$.store.book[0,1]", List.class);
ObjectMapper objectMapper = new ObjectMapper();
/*
//Standard
List<Map> res = JsonPath.read(DOCUMENT, "$.store.book[0,1]");
//or
List<Map> res = JsonPath.read(DOCUMENT, "$.store.book[0,1]", List.class);
//POJO Mapping med jackson ObjectMapper
List<Book> res = JsonPath.asList().of(Book.class).read(DOCUMENT, "$.store.book[0,1]");
List<Book> res = JsonPath.asListOf(Book.class).read(DOCUMENT, "$.store.book[0,1]");
Book res = JsonPath.as(Book.class).read(DOCUMENT, "$.store.book[0]");
*/
}
public static class Book {
private String category;
private String author;
private String title;
private Double displayPrice;
public Book() {
}
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public Double getDisplayPrice() {
return displayPrice;
}
public void setDisplayPrice(Double displayPrice) {
this.displayPrice = displayPrice;
}
}
}

6
json-path/src/test/java/com/jayway/jsonpath/JsonModelTest.java

@ -1,5 +1,7 @@
package com.jayway.jsonpath;
import com.jayway.jsonpath.spi.JsonProvider;
import com.jayway.jsonpath.spi.JsonProviderFactory;
import com.jayway.jsonpath.util.ScriptEngineJsonPath;
import org.junit.Test;
@ -57,7 +59,7 @@ public class JsonModelTest {
Object result = model.get(path);
System.out.println(model.getJsonProvider().toJson(result));
System.out.println(JsonProviderFactory.getInstance().toJson(result));
}
@ -99,7 +101,7 @@ public class JsonModelTest {
JsonModel model = new JsonModel(DOCUMENT);
JsonModel model2 = model.getModel("store.book[0]");
JsonModel model2 = model.getSubModel("store.book[0]");
System.out.println(model2.getJson());

60
json-path/src/test/java/com/jayway/jsonpath/JsonPathExtendedTest.java

@ -1,60 +0,0 @@
package com.jayway.jsonpath;
import org.junit.Test;
/**
* Created by IntelliJ IDEA.
* User: kallestenflo
* Date: 11/12/11
* Time: 8:25 PM
*/
public class JsonPathExtendedTest {
public final static String DOCUMENT =
"{ \"store\": {\n" +
" \"book\": [ \n" +
" { \"category\": \"reference\",\n" +
" \"author\": \"Nigel Rees\",\n" +
" \"title\": \"Sayings of the Century\",\n" +
" \"price\": 8.95\n" +
" },\n" +
" { \"category\": \"fiction\",\n" +
" \"author\": \"Evelyn Waugh\",\n" +
" \"title\": \"Sword of Honour\",\n" +
" \"price\": 12.99\n" +
" },\n" +
" { \"category\": \"fiction\",\n" +
" \"author\": \"Herman Melville\",\n" +
" \"title\": \"Moby Dick\",\n" +
" \"isbn\": \"0-553-21311-3\",\n" +
" \"price\": 8.99\n" +
" },\n" +
" { \"category\": \"fiction\",\n" +
" \"author\": \"J. R. R. Tolkien\",\n" +
" \"title\": \"The Lord of the Rings\",\n" +
" \"isbn\": \"0-395-19395-8\",\n" +
" \"price\": 22.99\n" +
" }\n" +
" ],\n" +
" \"bicycle\": {\n" +
" \"color\": \"red\",\n" +
" \"price\": 19.95,\n" +
" \"foo:bar\": \"fooBar\",\n" +
" \"dot.notation\": \"new\"\n" +
" }\n" +
" }\n" +
"}";
@Test
public void test_1() throws Exception {
JsonPath path = JsonPath.compile("$.store.book[*].title");
}
}

7
json-path/src/test/java/com/jayway/jsonpath/JsonPathTest.java

@ -1,6 +1,5 @@
package com.jayway.jsonpath;
import com.jayway.jsonpath.reader.PathTokenizer;
import com.jayway.jsonpath.util.ScriptEngineJsonPath;
import org.junit.Test;
@ -159,7 +158,6 @@ public class JsonPathTest {
assertThat(JsonPath.<List<String>>read(DOCUMENT, "$['store'].book[*]['author']"), hasItems("Nigel Rees", "Evelyn Waugh", "Herman Melville", "J. R. R. Tolkien"));
}
@Test
public void all_authors() throws Exception {
assertThat(JsonPath.<List<String>>read(DOCUMENT, "$..author"), hasItems("Nigel Rees", "Evelyn Waugh", "Herman Melville", "J. R. R. Tolkien"));
@ -264,9 +262,6 @@ public class JsonPathTest {
}
@Test(expected = InvalidPathException.class)
public void invalid_new_path_throws_exception() throws Exception {
JsonPath.read(DOCUMENT, "new ");
}
}

2
json-path/src/test/java/com/jayway/jsonpath/JacksonProviderTest.java → json-path/src/test/java/com/jayway/jsonpath/JsonProviderTest.java

@ -9,7 +9,7 @@ import org.junit.Test;
* Date: 11/8/11
* Time: 10:40 PM
*/
public class JacksonProviderTest {
public class JsonProviderTest {
public final static String ARRAY = "[{\"value\": 1},{\"value\": 2}, {\"value\": 3},{\"value\": 4}]";

45
json-path/src/test/java/com/jayway/jsonpath/ParserTest.java

@ -1,45 +0,0 @@
package com.jayway.jsonpath;
/**
* Created by IntelliJ IDEA.
* User: kallestenflo
* Date: 6/25/11
* Time: 4:16 PM
*/
public class ParserTest {
/*
private final static String SINGLE_QUOTE_JSON = "{'lhs': '1.0 U.S. dollar','rhs': 6.39778892, 'error': null, 'icc': true}";
private final static String NO_QUOTE_JSON = "{lhs: 1.0 U.S. dollar, rhs: 6.39778892, error: null, icc: true}";
@Test
public void default_mode_is_SLACK_MODE() throws Exception {
assertEquals(JsonPath.SLACK_MODE, JsonPath.getMode());
}
@Test
public void slack_mode_allows_single_quotes() throws Exception {
assertEquals(JsonPath.read(SINGLE_QUOTE_JSON, "lhs"), "1.0 U.S. dollar");
assertEquals(JsonPath.read(SINGLE_QUOTE_JSON, "rhs"), 6.39778892D);
}
@Test
public void slack_mode_allows_no_quotes() throws Exception {
assertEquals(JsonPath.read(NO_QUOTE_JSON, "lhs"), "1.0 U.S. dollar");
assertEquals(JsonPath.read(NO_QUOTE_JSON, "rhs"), 6.39778892D);
}
@Test(expected = ParseException.class)
public void strict_mode_does_not_accept_single_quotes() throws Exception {
JsonPath.setMode(JsonPath.STRICT_MODE);
JsonPath.read(SINGLE_QUOTE_JSON, "lhs");
}
@Test(expected = ParseException.class)
public void strict_mode_does_not_accept_no_quotes() throws Exception {
JsonPath.setMode(JsonPath.STRICT_MODE);
JsonPath.read(NO_QUOTE_JSON, "lhs");
}
*/
}

38
json-path/src/test/java/com/jayway/jsonpath/SplitPathFragmentsTest.java → json-path/src/test/java/com/jayway/jsonpath/PathTest.java

@ -1,11 +1,11 @@
package com.jayway.jsonpath;
import com.jayway.jsonpath.reader.PathTokenizer;
import com.jayway.jsonpath.spi.JsonProvider;
import com.jayway.jsonpath.internal.PathTokenizer;
import org.hamcrest.Matcher;
import org.junit.Test;
import static org.hamcrest.Matchers.hasItems;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
@ -15,32 +15,20 @@ import static org.junit.Assert.assertTrue;
* Date: 2/2/11
* Time: 1:22 PM
*/
public class SplitPathFragmentsTest {
private JsonProvider jsonProvider = JsonProvider.getInstance();
public class PathTest {
@Test
public void array_filter_bracket_test() throws Exception {
PathTokenizer tokenizer = new PathTokenizer("$.store.book[?(@['isbn'])].isbn", jsonProvider);
//PathTokenizer tokenizer = new PathTokenizer("$.store.book[?(@.isbn)].isbn", jsonProvider);
for (String fragment : tokenizer.getFragments()) {
System.out.println(fragment);
}
//assertThat(tokenizer.getFragments(), hasItems("$", "store", "[*]"));
public void path_is_not_definite() throws Exception {
assertFalse(JsonPath.compile("$..book[0]").isPathDefinite());
assertFalse(JsonPath.compile("$.books[*]").isPathDefinite());
}
@Test
public void path_is_definite() throws Exception {
assertTrue(JsonPath.compile("$.definite.this.is").isPathDefinite());
assertTrue(JsonPath.compile("rows[0].id").isPathDefinite());
}
@Test
public void valid_path_is_split_correctly() throws Exception {
@ -122,7 +110,7 @@ public class SplitPathFragmentsTest {
private void assertPathInvalid(String path) {
try {
PathTokenizer tokenizer = new PathTokenizer(path, jsonProvider);
PathTokenizer tokenizer = new PathTokenizer(path);
assertTrue("Expected exception!", false);
} catch (InvalidPathException expected) {}
}
@ -130,7 +118,7 @@ public class SplitPathFragmentsTest {
private void assertPath(String path, Matcher<Iterable<String>> matcher) {
System.out.println("PATH: " + path);
PathTokenizer tokenizer = new PathTokenizer(path, jsonProvider);
PathTokenizer tokenizer = new PathTokenizer(path);
for (String fragment : tokenizer.getFragments()) {
System.out.println(fragment);

2
json-path/src/test/java/com/jayway/jsonpath/util/ScriptEngineJsonPath.java

@ -43,7 +43,6 @@ public class ScriptEngineJsonPath {
private static String readScript(String script) {
InputStream is = null;
try {
is = ScriptEngineJsonPath.class.getClassLoader().getSystemResourceAsStream("js/" + script);
return new Scanner(is).useDelimiter("\\A").next();
@ -52,6 +51,7 @@ public class ScriptEngineJsonPath {
try {
is.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}

51
pom.xml

@ -86,8 +86,8 @@
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.5</source>
<target>1.5</target>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
@ -112,7 +112,34 @@
</execution>
</executions>
</plugin>
<!--
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.7</version>
<executions>
<execution>
<id>attach-javadocs</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>2.1.2</version>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
-->
</plugins>
</build>
@ -164,20 +191,15 @@
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.7.1</version>
</dependency>
<!--
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-library</artifactId>
<version>1.1</version>
<version>1.9.5</version>
</dependency>
-->
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-library</artifactId>
<version>1.2.1</version>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
@ -190,6 +212,13 @@
<version>2.5</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.1</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>

Loading…
Cancel
Save