|
|
@ -126,7 +126,7 @@ public class JsonModel { |
|
|
|
/** |
|
|
|
/** |
|
|
|
* Prints this JsonModel to standard out |
|
|
|
* Prints this JsonModel to standard out |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public void print(){ |
|
|
|
public void print() { |
|
|
|
String json = toJson(); |
|
|
|
String json = toJson(); |
|
|
|
System.out.println(JsonFormatter.prettyPrint(json)); |
|
|
|
System.out.println(JsonFormatter.prettyPrint(json)); |
|
|
|
} |
|
|
|
} |
|
|
@ -134,30 +134,28 @@ public class JsonModel { |
|
|
|
/** |
|
|
|
/** |
|
|
|
* Check if this JsonModel has the given definite path |
|
|
|
* Check if this JsonModel has the given definite path |
|
|
|
* |
|
|
|
* |
|
|
|
* @see com.jayway.jsonpath.JsonPath#isPathDefinite() |
|
|
|
|
|
|
|
* |
|
|
|
|
|
|
|
* @param jsonPath path to check |
|
|
|
* @param jsonPath path to check |
|
|
|
* @return true if model contains path |
|
|
|
* @return true if model contains path |
|
|
|
|
|
|
|
* @see com.jayway.jsonpath.JsonPath#isPathDefinite() |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public boolean hasPath(String jsonPath){ |
|
|
|
public boolean hasPath(String jsonPath) { |
|
|
|
return hasPath(JsonPath.compile(jsonPath)); |
|
|
|
return hasPath(JsonPath.compile(jsonPath)); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Check if this JsonModel has the given definite path |
|
|
|
* Check if this JsonModel has the given definite path |
|
|
|
* |
|
|
|
* |
|
|
|
* @see com.jayway.jsonpath.JsonPath#isPathDefinite() |
|
|
|
|
|
|
|
* |
|
|
|
|
|
|
|
* @param jsonPath path to check |
|
|
|
* @param jsonPath path to check |
|
|
|
* @return true if model contains path |
|
|
|
* @return true if model contains path |
|
|
|
|
|
|
|
* @see com.jayway.jsonpath.JsonPath#isPathDefinite() |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public boolean hasPath(JsonPath jsonPath){ |
|
|
|
public boolean hasPath(JsonPath jsonPath) { |
|
|
|
|
|
|
|
|
|
|
|
isTrue(jsonPath.isPathDefinite(), "hasPath can only be used for definite paths"); |
|
|
|
isTrue(jsonPath.isPathDefinite(), "hasPath can only be used for definite paths"); |
|
|
|
|
|
|
|
|
|
|
|
try { |
|
|
|
try { |
|
|
|
get(jsonPath); |
|
|
|
get(jsonPath); |
|
|
|
} catch(InvalidPathException e){ |
|
|
|
} catch (InvalidPathException e) { |
|
|
|
return false; |
|
|
|
return false; |
|
|
|
} |
|
|
|
} |
|
|
|
return true; |
|
|
|
return true; |
|
|
@ -219,7 +217,7 @@ public class JsonModel { |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
@SuppressWarnings({"unchecked"}) |
|
|
|
@SuppressWarnings({"unchecked"}) |
|
|
|
public <T> T get(String jsonPath, Filter... filters) { |
|
|
|
public <T> T get(String jsonPath, Filter... filters) { |
|
|
|
return (T) get(JsonPath.compile(jsonPath, filters)); |
|
|
|
return get(JsonPath.compile(jsonPath, filters)); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
@ -233,7 +231,7 @@ public class JsonModel { |
|
|
|
public <T> T get(JsonPath jsonPath) { |
|
|
|
public <T> T get(JsonPath jsonPath) { |
|
|
|
notNull(jsonPath, "jsonPath can not be null"); |
|
|
|
notNull(jsonPath, "jsonPath can not be null"); |
|
|
|
|
|
|
|
|
|
|
|
return (T) jsonPath.read(jsonObject); |
|
|
|
return jsonPath.read(jsonProvider, jsonObject); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// --------------------------------------------------------
|
|
|
|
// --------------------------------------------------------
|
|
|
@ -341,7 +339,7 @@ public class JsonModel { |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public String toJson(boolean prettyPrint) { |
|
|
|
public String toJson(boolean prettyPrint) { |
|
|
|
String json = jsonProvider.toJson(jsonObject); |
|
|
|
String json = jsonProvider.toJson(jsonObject); |
|
|
|
if(prettyPrint) |
|
|
|
if (prettyPrint) |
|
|
|
return JsonFormatter.prettyPrint(json); |
|
|
|
return JsonFormatter.prettyPrint(json); |
|
|
|
else |
|
|
|
else |
|
|
|
return json; |
|
|
|
return json; |
|
|
@ -378,10 +376,8 @@ public class JsonModel { |
|
|
|
* addressed by a definite path. In contrast to a detached model changes on the sub model |
|
|
|
* addressed by a definite path. In contrast to a detached model changes on the sub model |
|
|
|
* will be applied on the source model (the JsonModel from which the sub model was created) |
|
|
|
* will be applied on the source model (the JsonModel from which the sub model was created) |
|
|
|
* |
|
|
|
* |
|
|
|
* |
|
|
|
|
|
|
|
* @param jsonPath the absolute path to extract a JsonModel for |
|
|
|
* @param jsonPath the absolute path to extract a JsonModel for |
|
|
|
* @return the new JsonModel |
|
|
|
* @return the new JsonModel |
|
|
|
* |
|
|
|
|
|
|
|
* @see com.jayway.jsonpath.JsonPath#isPathDefinite() |
|
|
|
* @see com.jayway.jsonpath.JsonPath#isPathDefinite() |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public JsonModel getSubModel(String jsonPath) { |
|
|
|
public JsonModel getSubModel(String jsonPath) { |
|
|
@ -393,10 +389,8 @@ public class JsonModel { |
|
|
|
* addressed by a definite path. In contrast to a detached model changes on the sub model |
|
|
|
* addressed by a definite path. In contrast to a detached model changes on the sub model |
|
|
|
* will be applied on the source model (the JsonModel from which the sub model was created) |
|
|
|
* will be applied on the source model (the JsonModel from which the sub model was created) |
|
|
|
* |
|
|
|
* |
|
|
|
* |
|
|
|
|
|
|
|
* @param jsonPath the absolute path to extract a JsonModel for |
|
|
|
* @param jsonPath the absolute path to extract a JsonModel for |
|
|
|
* @return the new JsonModel |
|
|
|
* @return the new JsonModel |
|
|
|
* |
|
|
|
|
|
|
|
* @see com.jayway.jsonpath.JsonPath#isPathDefinite() |
|
|
|
* @see com.jayway.jsonpath.JsonPath#isPathDefinite() |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public JsonModel getSubModel(JsonPath jsonPath) { |
|
|
|
public JsonModel getSubModel(JsonPath jsonPath) { |
|
|
@ -442,7 +436,7 @@ public class JsonModel { |
|
|
|
public JsonModel getSubModelDetached(JsonPath jsonPath) { |
|
|
|
public JsonModel getSubModelDetached(JsonPath jsonPath) { |
|
|
|
notNull(jsonPath, "jsonPath can not be null"); |
|
|
|
notNull(jsonPath, "jsonPath can not be null"); |
|
|
|
|
|
|
|
|
|
|
|
Object subModel = jsonPath.read(jsonObject); |
|
|
|
Object subModel = jsonPath.read(jsonProvider, jsonObject); |
|
|
|
|
|
|
|
|
|
|
|
if (!jsonProvider.isContainer(subModel)) { |
|
|
|
if (!jsonProvider.isContainer(subModel)) { |
|
|
|
throw new InvalidModelException("The path " + jsonPath.getPath() + " returned an invalid model " + (subModel != null ? subModel.getClass() : "null")); |
|
|
|
throw new InvalidModelException("The path " + jsonPath.getPath() + " returned an invalid model " + (subModel != null ? subModel.getClass() : "null")); |
|
|
@ -500,14 +494,27 @@ public class JsonModel { |
|
|
|
/** |
|
|
|
/** |
|
|
|
* Creates a JsonModel |
|
|
|
* Creates a JsonModel |
|
|
|
* |
|
|
|
* |
|
|
|
|
|
|
|
* @param jsonProvider JsonProvider to use |
|
|
|
* @param json json string |
|
|
|
* @param json json string |
|
|
|
* @return a new JsonModel |
|
|
|
* @return a new JsonModel |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public static JsonModel model(String json) { |
|
|
|
public static JsonModel model(JsonProvider jsonProvider, String json) { |
|
|
|
|
|
|
|
notNull(jsonProvider, "jsonProvider can not be null"); |
|
|
|
notEmpty(json, "json can not be null or empty"); |
|
|
|
notEmpty(json, "json can not be null or empty"); |
|
|
|
|
|
|
|
|
|
|
|
return new JsonModel(json, JsonProviderFactory.createProvider()); |
|
|
|
return new JsonModel(json, jsonProvider); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Creates a JsonModel |
|
|
|
|
|
|
|
* |
|
|
|
|
|
|
|
* @param json json string |
|
|
|
|
|
|
|
* @return a new JsonModel |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
public static JsonModel model(String json) { |
|
|
|
|
|
|
|
return model(JsonProviderFactory.createProvider(), json); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public static JsonModel create(String json) { |
|
|
|
public static JsonModel create(String json) { |
|
|
|
return model(json); |
|
|
|
return model(json); |
|
|
|
} |
|
|
|
} |
|
|
@ -515,14 +522,27 @@ public class JsonModel { |
|
|
|
/** |
|
|
|
/** |
|
|
|
* Creates a JsonModel |
|
|
|
* Creates a JsonModel |
|
|
|
* |
|
|
|
* |
|
|
|
|
|
|
|
* @param jsonProvider JsonProvider to use |
|
|
|
* @param jsonObject a json container (a {@link Map} or a {@link List}) |
|
|
|
* @param jsonObject a json container (a {@link Map} or a {@link List}) |
|
|
|
* @return a new JsonModel |
|
|
|
* @return a new JsonModel |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public static JsonModel model(Object jsonObject) { |
|
|
|
public static JsonModel model(JsonProvider jsonProvider, Object jsonObject) { |
|
|
|
|
|
|
|
notNull(jsonProvider, "jsonProvider can not be null"); |
|
|
|
notNull(jsonObject, "jsonObject can not be null"); |
|
|
|
notNull(jsonObject, "jsonObject can not be null"); |
|
|
|
|
|
|
|
return new JsonModel(jsonObject, jsonProvider); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return new JsonModel(jsonObject, JsonProviderFactory.createProvider()); |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Creates a JsonModel |
|
|
|
|
|
|
|
* |
|
|
|
|
|
|
|
* @param jsonObject a json container (a {@link Map} or a {@link List}) |
|
|
|
|
|
|
|
* @return a new JsonModel |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
public static JsonModel model(Object jsonObject) { |
|
|
|
|
|
|
|
return model(JsonProviderFactory.createProvider(), jsonObject); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public static JsonModel create(Object jsonObject) { |
|
|
|
public static JsonModel create(Object jsonObject) { |
|
|
|
return model(jsonObject); |
|
|
|
return model(jsonObject); |
|
|
|
} |
|
|
|
} |
|
|
@ -530,14 +550,27 @@ public class JsonModel { |
|
|
|
/** |
|
|
|
/** |
|
|
|
* Creates a JsonModel |
|
|
|
* Creates a JsonModel |
|
|
|
* |
|
|
|
* |
|
|
|
|
|
|
|
* @param jsonProvider JsonProvider to use |
|
|
|
* @param url pointing to a Json document |
|
|
|
* @param url pointing to a Json document |
|
|
|
* @return a new JsonModel |
|
|
|
* @return a new JsonModel |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public static JsonModel model(URL url) throws IOException { |
|
|
|
public static JsonModel model(JsonProvider jsonProvider, URL url) throws IOException { |
|
|
|
|
|
|
|
notNull(jsonProvider, "jsonProvider can not be null"); |
|
|
|
notNull(url, "url can not be null"); |
|
|
|
notNull(url, "url can not be null"); |
|
|
|
|
|
|
|
|
|
|
|
return new JsonModel(url, JsonProviderFactory.createProvider()); |
|
|
|
return new JsonModel(url, jsonProvider); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Creates a JsonModel |
|
|
|
|
|
|
|
* |
|
|
|
|
|
|
|
* @param url pointing to a Json document |
|
|
|
|
|
|
|
* @return a new JsonModel |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
public static JsonModel model(URL url) throws IOException { |
|
|
|
|
|
|
|
return model(JsonProviderFactory.createProvider(), url); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public static JsonModel create(URL url) throws IOException { |
|
|
|
public static JsonModel create(URL url) throws IOException { |
|
|
|
return model(url); |
|
|
|
return model(url); |
|
|
|
} |
|
|
|
} |
|
|
@ -545,14 +578,27 @@ public class JsonModel { |
|
|
|
/** |
|
|
|
/** |
|
|
|
* Creates a JsonModel |
|
|
|
* Creates a JsonModel |
|
|
|
* |
|
|
|
* |
|
|
|
|
|
|
|
* @param jsonProvider JsonProvider to use |
|
|
|
* @param jsonInputStream json document stream |
|
|
|
* @param jsonInputStream json document stream |
|
|
|
* @return a new JsonModel |
|
|
|
* @return a new JsonModel |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public static JsonModel model(InputStream jsonInputStream) throws IOException { |
|
|
|
public static JsonModel model(JsonProvider jsonProvider, InputStream jsonInputStream) throws IOException { |
|
|
|
|
|
|
|
notNull(jsonProvider, "jsonProvider can not be null"); |
|
|
|
notNull(jsonInputStream, "jsonInputStream can not be null"); |
|
|
|
notNull(jsonInputStream, "jsonInputStream can not be null"); |
|
|
|
|
|
|
|
|
|
|
|
return new JsonModel(jsonInputStream, JsonProviderFactory.createProvider()); |
|
|
|
return new JsonModel(jsonInputStream, jsonProvider); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Creates a JsonModel |
|
|
|
|
|
|
|
* |
|
|
|
|
|
|
|
* @param jsonInputStream json document stream |
|
|
|
|
|
|
|
* @return a new JsonModel |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
public static JsonModel model(InputStream jsonInputStream) throws IOException { |
|
|
|
|
|
|
|
return model(JsonProviderFactory.createProvider(), jsonInputStream); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public static JsonModel create(InputStream jsonInputStream) throws IOException { |
|
|
|
public static JsonModel create(InputStream jsonInputStream) throws IOException { |
|
|
|
return model(jsonInputStream); |
|
|
|
return model(jsonInputStream); |
|
|
|
} |
|
|
|
} |
|
|
@ -570,8 +616,6 @@ public class JsonModel { |
|
|
|
throw new IndefinitePathException(jsonPath.getPath()); |
|
|
|
throw new IndefinitePathException(jsonPath.getPath()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
JsonProvider jsonProvider = JsonProviderFactory.createProvider(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Object modelRef = jsonObject; |
|
|
|
Object modelRef = jsonObject; |
|
|
|
|
|
|
|
|
|
|
|
if (jsonPath.getTokenizer().size() == 1) { |
|
|
|
if (jsonPath.getTokenizer().size() == 1) { |
|
|
@ -633,11 +677,10 @@ public class JsonModel { |
|
|
|
/** |
|
|
|
/** |
|
|
|
* Converts this JsonModel to the specified class using the configured {@link com.jayway.jsonpath.spi.MappingProvider} |
|
|
|
* Converts this JsonModel to the specified class using the configured {@link com.jayway.jsonpath.spi.MappingProvider} |
|
|
|
* |
|
|
|
* |
|
|
|
* @see MappingProviderFactory |
|
|
|
|
|
|
|
* |
|
|
|
|
|
|
|
* @param targetClass class to convert the {@link JsonModel} to |
|
|
|
* @param targetClass class to convert the {@link JsonModel} to |
|
|
|
* @param <T> template class
|
|
|
|
* @param <T> template class
|
|
|
|
* @return the mapped model |
|
|
|
* @return the mapped model |
|
|
|
|
|
|
|
* @see MappingProviderFactory |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
<T> T to(Class<T> targetClass); |
|
|
|
<T> T to(Class<T> targetClass); |
|
|
|
} |
|
|
|
} |
|
|
@ -693,6 +736,7 @@ public class JsonModel { |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Returns the operation target |
|
|
|
* Returns the operation target |
|
|
|
|
|
|
|
* |
|
|
|
* @return the operation target |
|
|
|
* @return the operation target |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
Map<String, Object> getTarget(); |
|
|
|
Map<String, Object> getTarget(); |
|
|
@ -709,6 +753,7 @@ public class JsonModel { |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Adds the value to the target map if it is not already present |
|
|
|
* Adds the value to the target map if it is not already present |
|
|
|
|
|
|
|
* |
|
|
|
* @param key the key |
|
|
|
* @param key the key |
|
|
|
* @param value the value |
|
|
|
* @param value the value |
|
|
|
* @return this {@link ObjectOps} |
|
|
|
* @return this {@link ObjectOps} |
|
|
@ -722,6 +767,7 @@ public class JsonModel { |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Tries to convert the value associated with the key to an {@link Integer} |
|
|
|
* Tries to convert the value associated with the key to an {@link Integer} |
|
|
|
|
|
|
|
* |
|
|
|
* @param key the key |
|
|
|
* @param key the key |
|
|
|
* @return converted value |
|
|
|
* @return converted value |
|
|
|
*/ |
|
|
|
*/ |
|
|
@ -729,6 +775,7 @@ public class JsonModel { |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Tries to convert the value associated with the key to an {@link Long} |
|
|
|
* Tries to convert the value associated with the key to an {@link Long} |
|
|
|
|
|
|
|
* |
|
|
|
* @param key the key |
|
|
|
* @param key the key |
|
|
|
* @return converted value |
|
|
|
* @return converted value |
|
|
|
*/ |
|
|
|
*/ |
|
|
@ -736,6 +783,7 @@ public class JsonModel { |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Tries to convert the value associated with the key to an {@link Double} |
|
|
|
* Tries to convert the value associated with the key to an {@link Double} |
|
|
|
|
|
|
|
* |
|
|
|
* @param key the key |
|
|
|
* @param key the key |
|
|
|
* @return converted value |
|
|
|
* @return converted value |
|
|
|
*/ |
|
|
|
*/ |
|
|
@ -743,6 +791,7 @@ public class JsonModel { |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Tries to convert the value associated with the key to an {@link String} |
|
|
|
* Tries to convert the value associated with the key to an {@link String} |
|
|
|
|
|
|
|
* |
|
|
|
* @param key the key |
|
|
|
* @param key the key |
|
|
|
* @return converted value |
|
|
|
* @return converted value |
|
|
|
*/ |
|
|
|
*/ |
|
|
@ -769,6 +818,7 @@ public class JsonModel { |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Map the target of this {@link ObjectOps} to the provided class
|
|
|
|
* Map the target of this {@link ObjectOps} to the provided class
|
|
|
|
|
|
|
|
* |
|
|
|
* @param targetClass class to convert the target object to |
|
|
|
* @param targetClass class to convert the target object to |
|
|
|
* @param <T> template class
|
|
|
|
* @param <T> template class
|
|
|
|
* @return the mapped model |
|
|
|
* @return the mapped model |
|
|
@ -783,6 +833,7 @@ public class JsonModel { |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Returns the operation target |
|
|
|
* Returns the operation target |
|
|
|
|
|
|
|
* |
|
|
|
* @return the operation target |
|
|
|
* @return the operation target |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
List<Object> getTarget(); |
|
|
|
List<Object> getTarget(); |
|
|
@ -984,7 +1035,7 @@ public class JsonModel { |
|
|
|
public ArrayOps each(Transformer<Object> transformer) { |
|
|
|
public ArrayOps each(Transformer<Object> transformer) { |
|
|
|
|
|
|
|
|
|
|
|
List targetObject = getTargetObject(jsonPath, List.class); |
|
|
|
List targetObject = getTargetObject(jsonPath, List.class); |
|
|
|
for(int i = 0; i < targetObject.size(); i++){ |
|
|
|
for (int i = 0; i < targetObject.size(); i++) { |
|
|
|
targetObject.set(i, transformer.transform(targetObject.get(i))); |
|
|
|
targetObject.set(i, transformer.transform(targetObject.get(i))); |
|
|
|
} |
|
|
|
} |
|
|
|
return this; |
|
|
|
return this; |
|
|
|