use of com.jayway.jsonpath.JsonPathException in project pom-manipulation-ext by release-engineering.
the class ConfigIO method parse.
public Properties parse(final File workingDir) throws ManipulationException {
Properties result = new Properties();
File propertyFile = new File(workingDir, propertyFileString);
File yamlPMEFile = new File(workingDir, yamlPMEFileString);
File yamlPNCFile = new File(workingDir, yamlPNCFileString);
File jsonPNCFile = new File(workingDir, jsonPNCFileString);
if (propertyFile.exists() && (yamlPMEFile.exists() || yamlPNCFile.exists() || jsonPNCFile.exists())) {
throw new ManipulationException("Cannot have both yaml, json and property configuration files.");
} else if (yamlPMEFile.exists() && yamlPNCFile.exists()) {
throw new ManipulationException("Cannot have both yaml configuration file formats.");
} else if ((yamlPMEFile.exists() || yamlPNCFile.exists()) && jsonPNCFile.exists()) {
throw new ManipulationException("Cannot have yaml and json configuration file formats.");
}
if (yamlPMEFile.exists()) {
result = loadYamlFile(yamlPMEFile);
logger.debug("Read yaml file containing {}.", result);
} else if (yamlPNCFile.exists()) {
result = loadYamlFile(yamlPNCFile);
logger.debug("Read yaml file containing {}.", result);
} else if (jsonPNCFile.exists()) {
try {
result.putAll(JsonPath.parse(jsonPNCFile).read("$.pme", Map.class));
} catch (IOException | JsonPathException e) {
throw new ManipulationException("Caught exception processing JSON file.", e);
}
logger.debug("Read json file containing {}.", result);
} else if (propertyFile.exists()) {
result = loadPropertiesFile(propertyFile);
logger.debug("Read properties file containing {}.", result);
}
return result;
}
use of com.jayway.jsonpath.JsonPathException in project JsonPath by json-path.
the class JsonOrgJsonProvider method getPropertyKeys.
@Override
public Collection<String> getPropertyKeys(Object obj) {
JSONObject jsonObject = toJsonObject(obj);
List<String> keys = new ArrayList<String>();
try {
for (int i = 0; i < jsonObject.names().length(); i++) {
String key = (String) jsonObject.names().get(i);
keys.add(key);
}
return keys;
} catch (JSONException e) {
throw new JsonPathException(e);
}
}
use of com.jayway.jsonpath.JsonPathException in project JsonPath by json-path.
the class JsonOrgJsonProvider method toIterable.
@Override
public Iterable<?> toIterable(Object obj) {
try {
if (isArray(obj)) {
JSONArray arr = toJsonArray(obj);
List<Object> values = new ArrayList<Object>(arr.length());
for (int i = 0; i < arr.length(); i++) {
values.add(unwrap(arr.get(i)));
}
return values;
} else {
JSONObject jsonObject = toJsonObject(obj);
List<Object> values = new ArrayList<Object>();
for (int i = 0; i < jsonObject.names().length(); i++) {
String key = (String) jsonObject.names().get(i);
Object val = jsonObject.get(key);
values.add(unwrap(val));
}
return values;
}
} catch (JSONException e) {
throw new JsonPathException(e);
}
}
use of com.jayway.jsonpath.JsonPathException in project JsonPath by json-path.
the class IsJson method describeMismatchSafely.
@Override
protected void describeMismatchSafely(T json, Description mismatchDescription) {
try {
ReadContext context = parse(json);
jsonMatcher.describeMismatch(context, mismatchDescription);
} catch (JsonPathException e) {
buildMismatchDescription(json, mismatchDescription, e);
} catch (IOException e) {
buildMismatchDescription(json, mismatchDescription, e);
}
}
use of com.jayway.jsonpath.JsonPathException in project JsonPath by jayway.
the class JsonOrgJsonProvider method setProperty.
@Override
public void setProperty(Object obj, Object key, Object value) {
try {
if (isMap(obj))
toJsonObject(obj).put(key.toString(), createJsonElement(value));
else {
JSONArray array = toJsonArray(obj);
int index;
if (key != null) {
index = key instanceof Integer ? (Integer) key : Integer.parseInt(key.toString());
} else {
index = array.length();
}
if (index == array.length()) {
array.put(createJsonElement(value));
} else {
array.put(index, createJsonElement(value));
}
}
} catch (JSONException e) {
throw new JsonPathException(e);
}
}
Aggregations