Browse Source

JsonProvider returns constant value to signal undefined instead of throwing exception.

pull/41/head
Kalle Stenflo 11 years ago
parent
commit
fd7074aa1f
  1. 7
      json-path/src/main/java/com/jayway/jsonpath/internal/spi/compiler/PathToken.java
  2. 9
      json-path/src/main/java/com/jayway/jsonpath/internal/spi/json/AbstractJsonProvider.java
  3. 7
      json-path/src/main/java/com/jayway/jsonpath/spi/json/JsonProvider.java

7
json-path/src/main/java/com/jayway/jsonpath/internal/spi/compiler/PathToken.java

@ -4,6 +4,7 @@ import com.jayway.jsonpath.InvalidPathException;
import com.jayway.jsonpath.Option;
import com.jayway.jsonpath.PathNotFoundException;
import com.jayway.jsonpath.internal.Utils;
import com.jayway.jsonpath.spi.json.JsonProvider;
import java.util.List;
@ -67,7 +68,11 @@ abstract class PathToken {
}
private Object readObjectProperty(String property, Object model, EvaluationContextImpl ctx) {
return ctx.jsonProvider().getMapValue(model, property, ctx.options().contains(Option.THROW_ON_MISSING_PROPERTY));
Object val = ctx.jsonProvider().getMapValue(model, property, ctx.options().contains(Option.THROW_ON_MISSING_PROPERTY));
if(val == JsonProvider.UNDEFINED){
throw new PathNotFoundException("Property ['" + property + "'] not found in the current context" );
}
return val;
}
void handleArrayIndex(int index, String currentPath, Object json, EvaluationContextImpl ctx) {

9
json-path/src/main/java/com/jayway/jsonpath/internal/spi/json/AbstractJsonProvider.java

@ -14,7 +14,6 @@
*/
package com.jayway.jsonpath.internal.spi.json;
import com.jayway.jsonpath.PathNotFoundException;
import com.jayway.jsonpath.internal.Utils;
import com.jayway.jsonpath.spi.json.JsonProvider;
@ -79,13 +78,13 @@ public abstract class AbstractJsonProvider implements JsonProvider {
*
* @param obj a map
* @param key property key
* @param throwOnMissing if true a PathNotFoundException is thrown if property is missing
* @param signalUndefined if true the constant {@link com.jayway.jsonpath.spi.json.JsonProvider#UNDEFINED} is returned for missing properties
* @return the map entry
*/
public Object getMapValue(Object obj, String key, boolean throwOnMissing){
public Object getMapValue(Object obj, String key, boolean signalUndefined){
Map m = (Map) obj;
if(!m.containsKey(key) && throwOnMissing){
throw new PathNotFoundException("Property ['" + key + "'] not found in the current context" );
if(!m.containsKey(key) && signalUndefined){
return JsonProvider.UNDEFINED;
} else {
return m.get(key);
}

7
json-path/src/main/java/com/jayway/jsonpath/spi/json/JsonProvider.java

@ -19,10 +19,11 @@ import com.jayway.jsonpath.InvalidJsonException;
import java.io.InputStream;
import java.io.Reader;
import java.util.Collection;
import java.util.List;
public interface JsonProvider {
static final Object UNDEFINED = new Object();
Mode getMode();
Object parse(String json) throws InvalidJsonException;
@ -114,10 +115,10 @@ public interface JsonProvider {
*
* @param obj a map
* @param key property key
* @param throwOnMissing if true a PathNotFoundException is thrown if property is not defined
* @param signalUndefined if true the constant {@link com.jayway.jsonpath.spi.json.JsonProvider#UNDEFINED} is returned for missing properties
* @return the map entry
*/
Object getMapValue(Object obj, String key, boolean throwOnMissing);
Object getMapValue(Object obj, String key, boolean signalUndefined);
/**
* Sets a value in an object or array

Loading…
Cancel
Save