Browse Source

Merge pull request #53 from jochenberger/get-property-keys-for-maps-only

don't use `JsonProvider.getPropertyKeys(Object)` to query a list/array's indexes
pull/55/head
kallestenflo 10 years ago
parent
commit
daaeb1a6d9
  1. 7
      json-path/src/main/java/com/jayway/jsonpath/internal/compiler/ScanPathToken.java
  2. 13
      json-path/src/main/java/com/jayway/jsonpath/internal/spi/json/AbstractJsonProvider.java
  3. 6
      json-path/src/main/java/com/jayway/jsonpath/spi/json/JsonProvider.java

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

@ -197,8 +197,11 @@ public class ScanPathToken extends PathToken {
@Override
public boolean matches(Object model) {
Collection<String> keys = ctx.jsonProvider().getPropertyKeys(model);
return keys.containsAll(propertyPathToken.getProperties());
if(ctx.jsonProvider().isMap(model)){
Collection<String> keys = ctx.jsonProvider().getPropertyKeys(model);
return keys.containsAll(propertyPathToken.getProperties());
}
return false;
}
}
}

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

@ -150,20 +150,15 @@ public abstract class AbstractJsonProvider implements JsonProvider {
}
/**
* Returns the keys from the given object or the indexes from an array
* Returns the keys from the given object
*
* @param obj an array or an object
* @return the keys for an object or the indexes for an array
* @param obj an object
* @return the keys for an object
*/
@SuppressWarnings("unchecked")
public Collection<String> getPropertyKeys(Object obj) {
if (isArray(obj)) {
List l = (List) obj;
List<String> keys = new ArrayList<String>(l.size());
for (int i = 0; i < l.size(); i++) {
keys.add(String.valueOf(i));
}
return keys;
throw new UnsupportedOperationException();
} else {
return ((Map) obj).keySet();
}

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

@ -78,10 +78,10 @@ public interface JsonProvider {
/**
* Returns the keys from the given object or the indexes from an array
* Returns the keys from the given object
*
* @param obj an array or an object
* @return the keys for an object or the indexes for an array
* @param obj an object
* @return the keys for an object
*/
Collection<String> getPropertyKeys(Object obj);

Loading…
Cancel
Save