Browse Source

Only arrays will be passed to JsonProvider.toIterable(Object) anyway

pull/117/head
Jochen Berger 9 years ago
parent
commit
77583725b4
  1. 8
      json-path/src/main/java/com/jayway/jsonpath/spi/json/AbstractJsonProvider.java
  2. 19
      json-path/src/main/java/com/jayway/jsonpath/spi/json/GsonJsonProvider.java
  3. 19
      json-path/src/main/java/com/jayway/jsonpath/spi/json/JacksonJsonNodeJsonProvider.java
  4. 6
      json-path/src/main/java/com/jayway/jsonpath/spi/json/JsonProvider.java

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

@ -163,17 +163,17 @@ public abstract class AbstractJsonProvider implements JsonProvider {
}
/**
* Converts given object to an {@link Iterable}
* Converts given array to an {@link Iterable}
*
* @param obj an array or an object
* @return the entries for an array or the values for a map
* @param obj an array
* @return an Iterable that iterates over the entries of an array
*/
@SuppressWarnings("unchecked")
public Iterable<? extends Object> toIterable(Object obj) {
if (isArray(obj))
return ((Iterable) obj);
else
return ((Map) obj).values();
throw new JsonPathException("Cannot iterate over " + obj!=null?obj.getClass().getName():"null");
}
@Override

19
json-path/src/main/java/com/jayway/jsonpath/spi/json/GsonJsonProvider.java

@ -219,21 +219,12 @@ public class GsonJsonProvider extends AbstractJsonProvider {
@Override
public Iterable<?> toIterable(Object obj) {
if (isArray(obj)) {
JsonArray arr = toJsonArray(obj);
List<Object> values = new ArrayList<Object>(arr.size());
for (Object o : arr) {
values.add(unwrap(o));
}
return values;
} else {
JsonObject jsonObject = toJsonObject(obj);
List<Object> values = new ArrayList<Object>();
for (Map.Entry<String, JsonElement> entry : jsonObject.entrySet()) {
values.add(unwrap(entry.getValue()));
}
return values;
JsonArray arr = toJsonArray(obj);
List<Object> values = new ArrayList<Object>(arr.size());
for (Object o : arr) {
values.add(unwrap(o));
}
return values;
}
private JsonElement createJsonElement(Object o) {

19
json-path/src/main/java/com/jayway/jsonpath/spi/json/JacksonJsonNodeJsonProvider.java

@ -217,21 +217,12 @@ public class JacksonJsonNodeJsonProvider extends AbstractJsonProvider {
@Override
public Iterable<?> toIterable(Object obj) {
if (isArray(obj)) {
ArrayNode arr = toJsonArray(obj);
List<Object> values = new ArrayList<Object>(arr.size());
for (Object o : arr) {
values.add(unwrap(o));
}
return values;
} else {
List<Object> values = new ArrayList<Object>();
Iterator<JsonNode> iter = toJsonObject(obj).elements();
while (iter.hasNext()){
values.add(unwrap(iter.next()));
}
return values;
ArrayNode arr = toJsonArray(obj);
List<Object> values = new ArrayList<Object>(arr.size());
for (Object o : arr) {
values.add(unwrap(o));
}
return values;
}
private JsonNode createJsonElement(Object o) {

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

@ -76,10 +76,10 @@ public interface JsonProvider {
int length(Object obj);
/**
* Converts given object to an {@link Iterable}
* Converts given array to an {@link Iterable}
*
* @param obj an array or an object
* @return the entries for an array or the values for a map
* @param obj an array
* @return an Iterable that iterates over the entries of an array
*/
Iterable<?> toIterable(Object obj);

Loading…
Cancel
Save