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. 15
      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']"));
}

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

@ -123,11 +123,23 @@ public class IssuesTest {
}
@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_22() throws Exception {
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
@ -139,5 +151,4 @@ public class IssuesTest {
}
}

Loading…
Cancel
Save