Search in sources :

Example 1 with JsonPathException

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);
    }
}
Also used : JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) JsonPathException(com.jayway.jsonpath.JsonPathException)

Example 2 with JsonPathException

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);
    }
}
Also used : JSONObject(org.json.JSONObject) JSONException(org.json.JSONException) JsonPathException(com.jayway.jsonpath.JsonPathException) JSONObject(org.json.JSONObject)

Example 3 with JsonPathException

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();
}
Also used : StringWriter(java.io.StringWriter) JsonPathException(com.jayway.jsonpath.JsonPathException)

Example 4 with JsonPathException

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);
    }
}
Also used : JsonPathException(com.jayway.jsonpath.JsonPathException) ManipulationException(org.commonjava.maven.ext.common.ManipulationException) List(java.util.List) DocumentContext(com.jayway.jsonpath.DocumentContext) Test(org.junit.Test)

Example 5 with JsonPathException

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);
    }
}
Also used : ManipulationException(org.commonjava.maven.ext.common.ManipulationException) JsonPathException(com.jayway.jsonpath.JsonPathException) List(java.util.List) DocumentContext(com.jayway.jsonpath.DocumentContext) File(java.io.File)

Aggregations

JsonPathException (com.jayway.jsonpath.JsonPathException)18 JSONException (org.json.JSONException)8 JSONObject (org.json.JSONObject)6 ArrayList (java.util.ArrayList)4 ManipulationException (org.commonjava.maven.ext.common.ManipulationException)4 JSONArray (org.json.JSONArray)4 DocumentContext (com.jayway.jsonpath.DocumentContext)3 IOException (java.io.IOException)3 ReadContext (com.jayway.jsonpath.ReadContext)2 File (java.io.File)2 StringWriter (java.io.StringWriter)2 List (java.util.List)2 Test (org.junit.Test)2 Parameter (com.jayway.jsonpath.internal.function.Parameter)1 AllErrors (gov.cms.qpp.conversion.model.error.AllErrors)1 Error (gov.cms.qpp.conversion.model.error.Error)1 Map (java.util.Map)1 Properties (java.util.Properties)1 YamlFile (org.commonjava.maven.ext.common.model.YamlFile)1