|
|
@ -15,9 +15,12 @@ |
|
|
|
package com.jayway.jsonpath.spi.impl; |
|
|
|
package com.jayway.jsonpath.spi.impl; |
|
|
|
|
|
|
|
|
|
|
|
import com.jayway.jsonpath.spi.JsonProvider; |
|
|
|
import com.jayway.jsonpath.spi.JsonProvider; |
|
|
|
|
|
|
|
|
|
|
|
import org.apache.commons.lang3.SerializationUtils; |
|
|
|
import org.apache.commons.lang3.SerializationUtils; |
|
|
|
|
|
|
|
|
|
|
|
import java.io.Serializable; |
|
|
|
import java.io.Serializable; |
|
|
|
|
|
|
|
import java.util.ArrayList; |
|
|
|
|
|
|
|
import java.util.Collection; |
|
|
|
import java.util.List; |
|
|
|
import java.util.List; |
|
|
|
import java.util.Map; |
|
|
|
import java.util.Map; |
|
|
|
|
|
|
|
|
|
|
@ -32,67 +35,111 @@ public abstract class AbstractJsonProvider implements JsonProvider { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* checks if object is <code>instanceof</code> <code>java.util.List</code> or <code>java.util.Map</code> |
|
|
|
* checks if object is a map or an array |
|
|
|
* |
|
|
|
* |
|
|
|
* @param obj object to check |
|
|
|
* @param obj object to check |
|
|
|
* @return true if List or Map |
|
|
|
* @return true if obj is a map or an array |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public boolean isContainer(Object obj) { |
|
|
|
public boolean isContainer(Object obj) { |
|
|
|
return (isList(obj) || isMap(obj)); |
|
|
|
return (isArray(obj) || isMap(obj)); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* checks if object is <code>instanceof</code> <code>java.util.List</code> |
|
|
|
* checks if object is an array |
|
|
|
* |
|
|
|
* |
|
|
|
* @param obj object to check |
|
|
|
* @param obj object to check |
|
|
|
* @return true if List |
|
|
|
* @return true if obj is an array |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public boolean isList(Object obj) { |
|
|
|
public boolean isArray(Object obj) { |
|
|
|
return (obj instanceof List); |
|
|
|
return (obj instanceof List); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Converts give object to a List |
|
|
|
* Extracts a value from an object or array |
|
|
|
* |
|
|
|
* |
|
|
|
* @param list object to convert |
|
|
|
* @param obj an array or an object |
|
|
|
* @return object as list |
|
|
|
* @param key a String key or a numerical index |
|
|
|
|
|
|
|
* @return the entry at the given key, i.e. obj[key] |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
@SuppressWarnings({"unchecked"}) |
|
|
|
public Object getProperty(Object obj, Object key) { |
|
|
|
public List<Object> toList(Object list) { |
|
|
|
if (isMap(obj)) |
|
|
|
return (List<Object>) list; |
|
|
|
return ((Map) obj).get(key.toString()); |
|
|
|
|
|
|
|
else{ |
|
|
|
|
|
|
|
int index = key instanceof Integer? (Integer) key : Integer.parseInt(key.toString()); |
|
|
|
|
|
|
|
return ((List) obj).get(index); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Converts given object to a Map |
|
|
|
* Sets a value in an object or array |
|
|
|
* |
|
|
|
* |
|
|
|
* @param map object to convert |
|
|
|
* @param obj an array or an object |
|
|
|
* @return object as map |
|
|
|
* @param key a String key or a numerical index |
|
|
|
|
|
|
|
* @param value the value to set |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
@SuppressWarnings({"unchecked"}) |
|
|
|
public void setProperty(Object obj, Object key, Object value) { |
|
|
|
public Map<String, Object> toMap(Object map) { |
|
|
|
if (isMap(obj)) |
|
|
|
return (Map<String, Object>) map; |
|
|
|
((Map) obj).put(key.toString(), value); |
|
|
|
|
|
|
|
else{ |
|
|
|
|
|
|
|
int index = key instanceof Integer? (Integer) key : Integer.parseInt(key.toString()); |
|
|
|
|
|
|
|
((List) obj).add(index, value); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Extracts a value from a Map |
|
|
|
* checks if object is a map (i.e. no array) |
|
|
|
* |
|
|
|
* |
|
|
|
* @param map map to read from |
|
|
|
* @param obj object to check |
|
|
|
* @param key key to read |
|
|
|
* @return true if the object is a map |
|
|
|
* @return value of key in map |
|
|
|
|
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public Object getMapValue(Object map, String key) { |
|
|
|
public boolean isMap(Object obj) { |
|
|
|
return toMap(map).get(key); |
|
|
|
return (obj instanceof Map); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* checks if object is <code>instanceof</code> <code>java.util.Map</code> |
|
|
|
* Returns the keys from the given object or the indexes from an array |
|
|
|
* |
|
|
|
* |
|
|
|
* @param obj object to check |
|
|
|
* @param obj an array or an object |
|
|
|
* @return true if Map |
|
|
|
* @return the keys for an object or the indexes for an array |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public boolean isMap(Object obj) { |
|
|
|
public Collection<String> getPropertyKeys(Object obj) { |
|
|
|
return (obj instanceof Map); |
|
|
|
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; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
else{ |
|
|
|
|
|
|
|
return ((Map)obj).keySet(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Get the length of an array or object |
|
|
|
|
|
|
|
* @param obj an array or an object |
|
|
|
|
|
|
|
* @return the number of entries in the array or object |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
public int length(Object obj) { |
|
|
|
|
|
|
|
if (isArray(obj)){ |
|
|
|
|
|
|
|
return ((List)obj).size(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return getPropertyKeys(obj).size(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Converts given object to an {@link Iterable} |
|
|
|
|
|
|
|
* |
|
|
|
|
|
|
|
* @param obj an array or an object |
|
|
|
|
|
|
|
* @return the entries for an array or the values for a map |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
public Iterable<Object> toIterable(Object obj) { |
|
|
|
|
|
|
|
if (isArray(obj)) |
|
|
|
|
|
|
|
return ((Iterable) obj); |
|
|
|
|
|
|
|
else |
|
|
|
|
|
|
|
return ((Map)obj).values(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|