use of com.jayway.jsonpath.JsonPathException in project JsonPath by json-path.
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);
}
}
use of com.jayway.jsonpath.JsonPathException in project JsonPath by json-path.
the class JsonOrgJsonProvider method getMapValue.
@Override
public Object getMapValue(Object obj, String key) {
try {
JSONObject jsonObject = toJsonObject(obj);
Object o = jsonObject.opt(key);
if (o == null) {
return UNDEFINED;
} else {
return unwrap(o);
}
} catch (JSONException e) {
throw new JsonPathException(e);
}
}
use of com.jayway.jsonpath.JsonPathException in project JsonPath by json-path.
the class Utils method unescape.
public static String unescape(String str) {
if (str == null) {
return null;
}
int len = str.length();
StringWriter writer = new StringWriter(len);
StringBuffer unicode = new StringBuffer(4);
boolean hadSlash = false;
boolean inUnicode = false;
for (int i = 0; i < len; i++) {
char ch = str.charAt(i);
if (inUnicode) {
unicode.append(ch);
if (unicode.length() == 4) {
try {
int value = Integer.parseInt(unicode.toString(), 16);
writer.write((char) value);
unicode.setLength(0);
inUnicode = false;
hadSlash = false;
} catch (NumberFormatException nfe) {
throw new JsonPathException("Unable to parse unicode value: " + unicode, nfe);
}
}
continue;
}
if (hadSlash) {
hadSlash = false;
switch(ch) {
case '\\':
writer.write('\\');
break;
case '\'':
writer.write('\'');
break;
case '\"':
writer.write('"');
break;
case 'r':
writer.write('\r');
break;
case 'f':
writer.write('\f');
break;
case 't':
writer.write('\t');
break;
case 'n':
writer.write('\n');
break;
case 'b':
writer.write('\b');
break;
case 'u':
{
inUnicode = true;
break;
}
default:
writer.write(ch);
break;
}
continue;
} else if (ch == '\\') {
hadSlash = true;
continue;
}
writer.write(ch);
}
if (hadSlash) {
writer.write('\\');
}
return writer.toString();
}
use of com.jayway.jsonpath.JsonPathException in project pom-manipulation-ext by release-engineering.
the class JSONIOTest method countMatches.
@Test
public void countMatches() throws ManipulationException, IOException {
String pluginsPath = "$..plugins";
String reposURLPath = "$.repository.url";
try {
DocumentContext doc = jsonIO.parseJSON(pluginFile);
List o = doc.read(pluginsPath);
assertTrue(o.size() == 1);
o = doc.read(reposURLPath);
assertTrue(o.size() == 1);
} catch (JsonPathException e) {
throw new ManipulationException("Caught JsonPath", e);
}
}
use of com.jayway.jsonpath.JsonPathException in project pom-manipulation-ext by release-engineering.
the class JSONManipulator method internalApplyChanges.
// Package accessible so tests can use it.
void internalApplyChanges(Project project, JSONState.JSONOperation operation) throws ManipulationException {
File target = new File(project.getPom().getParentFile(), operation.getFile());
logger.info("Attempting to start JSON update to file {} with xpath {} and replacement '{}' ", target, operation.getXPath(), operation.getUpdate());
DocumentContext dc = null;
try {
if (!target.exists()) {
logger.error("Unable to locate JSON file {} ", target);
throw new ManipulationException("Unable to locate JSON file " + target);
}
dc = jsonIO.parseJSON(target);
List o = dc.read(operation.getXPath());
if (o.size() == 0) {
if (project.isIncrementalPME()) {
logger.warn("Did not locate JSON using XPath " + operation.getXPath());
return;
} else {
logger.error("XPath {} did not find any expressions within {} ", operation.getXPath(), operation.getFile());
throw new ManipulationException("XPath did not resolve to a valid value");
}
}
if (isEmpty(operation.getUpdate())) {
// Delete
logger.info("Deleting {} on {}", operation.getXPath(), dc.toString());
dc.delete(operation.getXPath());
} else {
// Update
logger.info("Updating {} on {}", operation.getXPath(), dc.toString());
dc.set(operation.getXPath(), operation.getUpdate());
}
jsonIO.writeJSON(target, dc.jsonString());
} catch (JsonPathException e) {
logger.error("Caught JSON exception processing file {}, document context {} ", target, dc, e);
throw new ManipulationException("Caught JsonPath", e);
}
}
Aggregations