Search in sources :

Example 16 with JsonPathException

use of com.jayway.jsonpath.JsonPathException in project JsonPath by jayway.

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);
    StringBuilder unicode = new StringBuilder(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 17 with JsonPathException

use of com.jayway.jsonpath.JsonPathException in project JsonPath by jayway.

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

Example 18 with JsonPathException

use of com.jayway.jsonpath.JsonPathException in project qpp-conversion-tool by CMSgov.

the class ValidationServiceImpl method convertQppValidationErrorsToQrda.

/**
 * Converts the QPP error returned from the validation API to QRDA3 errors
 *
 * @param validationResponse The JSON response containing a QPP error.
 * @param wrapper The QPP that resulted in the QPP error.
 * @return The QRDA3 errors.
 */
AllErrors convertQppValidationErrorsToQrda(String validationResponse, JsonWrapper wrapper) {
    AllErrors errors = new AllErrors();
    if (validationResponse == null) {
        return errors;
    }
    Error error = getError(validationResponse);
    error.getDetails().forEach(detail -> {
        detail.setMessage(SV_LABEL + detail.getMessage());
        String newPath = UNABLE_PROVIDE_XPATH;
        try {
            newPath = PathCorrelator.prepPath(detail.getPath(), wrapper);
        } catch (ClassCastException | JsonPathException exc) {
            API_LOG.warn("Failed to convert from json path to an XPath.", exc);
        }
        detail.setPath(newPath);
    });
    errors.addError(error);
    return errors;
}
Also used : AllErrors(gov.cms.qpp.conversion.model.error.AllErrors) Error(gov.cms.qpp.conversion.model.error.Error) JsonPathException(com.jayway.jsonpath.JsonPathException)

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