Browse Source

Improved exceptions and simplified JsonProviderFactory.

pull/29/head
Kalle Stenflo 11 years ago
parent
commit
6d316c7d29
  1. 24
      json-path/src/main/java/com/jayway/jsonpath/PathNotFoundException.java
  2. 7
      json-path/src/main/java/com/jayway/jsonpath/internal/filter/FieldFilter.java
  3. 28
      json-path/src/main/java/com/jayway/jsonpath/spi/JsonProviderFactory.java
  4. 13
      json-path/src/main/java/com/jayway/jsonpath/spi/impl/JsonSmartJsonProvider.java
  5. 22
      json-path/src/test/java/com/jayway/jsonpath/HelpTest.java
  6. 31
      json-path/src/test/java/com/jayway/jsonpath/IssuesTest.java

24
json-path/src/main/java/com/jayway/jsonpath/PathNotFoundException.java

@ -0,0 +1,24 @@
package com.jayway.jsonpath;
/**
* User: kalle
* Date: 8/20/13
* Time: 2:33 PM
*/
public class PathNotFoundException extends InvalidPathException {
public PathNotFoundException() {
}
public PathNotFoundException(String message) {
super(message);
}
public PathNotFoundException(String message, Throwable cause) {
super(message, cause);
}
public PathNotFoundException(Throwable cause) {
super(cause);
}
}

7
json-path/src/main/java/com/jayway/jsonpath/internal/filter/FieldFilter.java

@ -16,6 +16,7 @@ package com.jayway.jsonpath.internal.filter;
import com.jayway.jsonpath.Filter;
import com.jayway.jsonpath.InvalidPathException;
import com.jayway.jsonpath.PathNotFoundException;
import com.jayway.jsonpath.spi.JsonProvider;
import java.util.LinkedList;
@ -38,7 +39,7 @@ public class FieldFilter extends PathTokenFilter {
public Object filter(Object obj, JsonProvider jsonProvider, LinkedList<Filter> filters, boolean inArrayContext) {
if (jsonProvider.isList(obj)) {
if (!inArrayContext) {
throw new InvalidPathException("Trying to access field on array");
throw new PathNotFoundException("Trying to access the field '" + condition +"' in an array context.");
} else {
List<Object> result = jsonProvider.createList();
for (Object current : jsonProvider.toList(obj)) {
@ -73,7 +74,7 @@ public class FieldFilter extends PathTokenFilter {
Map<String, Object> map = jsonProvider.toMap(obj);
if(!map.containsKey(condition) && split.length == 1){
throw new InvalidPathException("invalid path");
throw new PathNotFoundException("Path '" + condition + "' not found in the current context.");
} else {
if(split.length == 1){
@ -83,8 +84,6 @@ public class FieldFilter extends PathTokenFilter {
for (String prop : split) {
if(map.containsKey(prop)){
res.put(prop, map.get(prop));
} else {
throw new InvalidPathException("invalid path");
}
}
return res;

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

@ -21,35 +21,15 @@ import com.jayway.jsonpath.spi.impl.JsonSmartJsonProvider;
*/
public abstract class JsonProviderFactory {
private static Class defaultProvider = null;
public static JsonProviderFactory factory = new JsonProviderFactory() {
@Override
protected JsonProvider create() {
JsonProvider provider = null;
try {
if(defaultProvider != null) {
provider = (JsonProvider)defaultProvider.newInstance();
}
} catch(Throwable t) {
}
if(provider == null) {
provider = new JsonSmartJsonProvider();
}
return provider;
//return new JacksonProvider();
}
};
private static JsonProvider provider = new JsonSmartJsonProvider();
public static JsonProvider createProvider() {
return factory.create();
return provider;
}
public static synchronized void setDefaultProvider(Class<? extends JsonProvider> jsonProvider) {
defaultProvider = jsonProvider;
public static synchronized void setProvider(JsonProvider jsonProvider) {
provider = jsonProvider;
}
protected abstract JsonProvider create();
}

13
json-path/src/main/java/com/jayway/jsonpath/spi/impl/JsonSmartJsonProvider.java

@ -37,6 +37,8 @@ public class JsonSmartJsonProvider extends AbstractJsonProvider {
private JSONParser parser;
private ContainerFactory containerFactory = ContainerFactory.FACTORY_SIMPLE;
public JsonSmartJsonProvider() {
this(Mode.SLACK);
}
@ -47,16 +49,17 @@ public class JsonSmartJsonProvider extends AbstractJsonProvider {
}
public Map<String, Object> createMap() {
return new JSONObject();
return containerFactory.createObjectContainer();
}
public List<Object> createList() {
return new JSONArray();
return containerFactory.createArrayContainer();
}
public Object parse(String json) {
try {
return parser.parse(json, ContainerFactory.FACTORY_ORDERED);
//return parser.parse(json, ContainerFactory.FACTORY_ORDERED);
return parser.parse(json, containerFactory);
} catch (ParseException e) {
throw new InvalidJsonException(e);
}
@ -65,7 +68,7 @@ public class JsonSmartJsonProvider extends AbstractJsonProvider {
@Override
public Object parse(Reader jsonReader) throws InvalidJsonException {
try {
return parser.parse(jsonReader, ContainerFactory.FACTORY_ORDERED);
return parser.parse(jsonReader, containerFactory);
} catch (ParseException e) {
throw new InvalidJsonException(e);
}
@ -74,7 +77,7 @@ public class JsonSmartJsonProvider extends AbstractJsonProvider {
@Override
public Object parse(InputStream jsonStream) throws InvalidJsonException {
try {
return parser.parse(new InputStreamReader(jsonStream), ContainerFactory.FACTORY_ORDERED);
return parser.parse(new InputStreamReader(jsonStream), containerFactory);
} catch (ParseException e) {
throw new InvalidJsonException(e);
}

22
json-path/src/test/java/com/jayway/jsonpath/HelpTest.java

@ -62,6 +62,20 @@ public class HelpTest {
" \"mimeType\": \"application/octet-stream\"\n" +
" }\n" +
" ]\n" +
" },\n" +
" {\n" +
" \"groupType\": \"series\",\n" +
" \"instanceId\": \"grp://15\",\n" +
" \"extra\": 1,\n" +
" \"id\": \"prg://16\",\n" +
" \"type\": \"group\",\n" +
" \"media\": [\n" +
" {\n" +
" \"classification\": \"urn:1.2.3\",\n" +
" \"uri\": \"http://yahoo.com/1.png\",\n" +
" \"mimeType\": \"application/octet-stream\"\n" +
" }\n" +
" ]\n" +
" }\n" +
" ],\n" +
" \"header\": {\n" +
@ -71,7 +85,13 @@ public class HelpTest {
@Test
public void sample_one_x(){
public void sample_one_a(){
System.out.println(JsonPath.read(JSON2, "$.['error', 'header']"));
System.out.println(JsonPath.read(JSON2, "$.contents[*].['groupType', 'type', 'extra']"));
}
@Test
public void sample_one_b(){
System.out.println(JsonPath.read(JSON2, "$.['error', 'header']"));
System.out.println(JsonPath.read(JSON2, "$.contents[*].['groupType', 'type']"));
}

31
json-path/src/test/java/com/jayway/jsonpath/IssuesTest.java

@ -20,7 +20,7 @@ import static org.junit.Assert.assertThat;
* Time: 8:42 AM
*/
public class IssuesTest {
@Test
public void issue_11() throws Exception {
String json = "{ \"foo\" : [] }";
@ -71,7 +71,7 @@ public class IssuesTest {
@Test
public void issue_24(){
public void issue_24() {
InputStream is = null;
try {
@ -85,7 +85,7 @@ public class IssuesTest {
System.out.println(o);
is.close();
} catch (Exception e){
} catch (Exception e) {
e.printStackTrace();
IOUtils.closeQuietly(is);
}
@ -93,7 +93,7 @@ public class IssuesTest {
}
@Test
public void issue_28_string(){
public void issue_28_string() {
String json = "{\"contents\": [\"one\",\"two\",\"three\"]}";
List<String> result = JsonPath.read(json, "$.contents[?(@ == 'two')]");
@ -103,7 +103,7 @@ public class IssuesTest {
}
@Test
public void issue_28_int(){
public void issue_28_int() {
String json = "{\"contents\": [1,2,3]}";
List<Integer> result = JsonPath.read(json, "$.contents[?(@ == 2)]");
@ -113,7 +113,7 @@ public class IssuesTest {
}
@Test
public void issue_28_boolean(){
public void issue_28_boolean() {
String json = "{\"contents\": [true, true, false]}";
List<Boolean> result = JsonPath.read(json, "$.contents[?(@ == true)]");
@ -123,21 +123,32 @@ public class IssuesTest {
}
@Test
@Test(expected = PathNotFoundException.class)
public void issue_22() throws Exception {
String json = "{\"a\":{\"b\":1,\"c\":2}}";
System.out.println(JsonPath.read(json, "a.d"));
}
@Test
public void issue_22b() throws Exception {
String json = "{\"a\":[{\"b\":1,\"c\":2},{\"b\":5,\"c\":2}]}";
System.out.println(JsonPath.read(json, "a[?(@.b==5)].d"));
System.out.println(JsonPath.read(json, "a[?(@.b==5)].d"));
}
@Test(expected = PathNotFoundException.class)
public void issue_26() throws Exception {
String json = "[{\"a\":[{\"b\":1,\"c\":2}]}]";
Object o = JsonPath.read(json, "$.a");
}
@Test
public void issue_29_b() throws Exception {
String json = "{\"list\": [ { \"a\":\"atext\", \"b\":{ \"b-a\":\"batext\", \"b-b\":\"bbtext\" } }, { \"a\":\"atext2\", \"b\":{ \"b-a\":\"batext2\", \"b-b\":\"bbtext2\" } } ] }";
List<String> result = JsonPath.read(json, "$.list[?]", Filter.filter(Criteria.where("b.b-a").eq("batext2")));
assertTrue(result.size() == 1);
}
}

Loading…
Cancel
Save