Search in sources :

Example 6 with InvalidJsonException

use of com.jayway.jsonpath.InvalidJsonException in project nifi by apache.

the class JsonPathEvaluator method evaluate.

@Override
public QueryResult<String> evaluate(final Map<String, String> attributes) {
    final String subjectValue = subject.evaluate(attributes).getValue();
    if (subjectValue == null || subjectValue.length() == 0) {
        throw new AttributeExpressionLanguageException("Subject is empty");
    }
    DocumentContext documentContext = null;
    try {
        documentContext = validateAndEstablishJsonContext(subjectValue);
    } catch (InvalidJsonException e) {
        throw new AttributeExpressionLanguageException("Subject contains invalid JSON: " + subjectValue, e);
    }
    final JsonPath compiledJsonPath;
    if (precompiledJsonPathExp != null) {
        compiledJsonPath = precompiledJsonPathExp;
    } else {
        compiledJsonPath = compileJsonPathExpression(jsonPathExp.evaluate(attributes).getValue());
    }
    Object result = null;
    try {
        result = documentContext.read(compiledJsonPath);
    } catch (Exception e) {
        // assume the path did not match anything in the document
        return EMPTY_RESULT;
    }
    return new StringQueryResult(getResultRepresentation(result, EMPTY_RESULT.getValue()));
}
Also used : StringQueryResult(org.apache.nifi.attribute.expression.language.evaluation.StringQueryResult) AttributeExpressionLanguageException(org.apache.nifi.attribute.expression.language.exception.AttributeExpressionLanguageException) InvalidJsonException(com.jayway.jsonpath.InvalidJsonException) DocumentContext(com.jayway.jsonpath.DocumentContext) JsonPath(com.jayway.jsonpath.JsonPath) AttributeExpressionLanguageException(org.apache.nifi.attribute.expression.language.exception.AttributeExpressionLanguageException) InvalidJsonException(com.jayway.jsonpath.InvalidJsonException)

Example 7 with InvalidJsonException

use of com.jayway.jsonpath.InvalidJsonException in project JsonPath by json-path.

the class JacksonJsonProvider method toJson.

@Override
public String toJson(Object obj) {
    StringWriter writer = new StringWriter();
    try {
        JsonGenerator generator = objectMapper.getFactory().createGenerator(writer);
        objectMapper.writeValue(generator, obj);
        writer.flush();
        writer.close();
        generator.close();
        return writer.getBuffer().toString();
    } catch (IOException e) {
        throw new InvalidJsonException();
    }
}
Also used : StringWriter(java.io.StringWriter) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) InvalidJsonException(com.jayway.jsonpath.InvalidJsonException) IOException(java.io.IOException)

Example 8 with InvalidJsonException

use of com.jayway.jsonpath.InvalidJsonException in project JsonPath by json-path.

the class JettisonProvider method parse.

@Override
public Object parse(InputStream jsonStream, String charset) throws InvalidJsonException {
    try {
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int size;
        while ((size = jsonStream.read(buffer)) > 0) {
            stream.write(buffer, 0, size);
        }
        return parse(new JettisonTokener(new String(stream.toByteArray(), charset)));
    } catch (IOException ioe) {
        throw new InvalidJsonException(ioe);
    }
}
Also used : InvalidJsonException(com.jayway.jsonpath.InvalidJsonException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException)

Aggregations

InvalidJsonException (com.jayway.jsonpath.InvalidJsonException)8 IOException (java.io.IOException)4 DocumentContext (com.jayway.jsonpath.DocumentContext)3 JsonPath (com.jayway.jsonpath.JsonPath)3 PathNotFoundException (com.jayway.jsonpath.PathNotFoundException)3 List (java.util.List)3 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 StringWriter (java.io.StringWriter)2 StandardCharsets (java.nio.charset.StandardCharsets)2 ArrayList (java.util.ArrayList)2 Collection (java.util.Collection)2 Collections (java.util.Collections)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 Map (java.util.Map)2 Set (java.util.Set)2 StringUtils (org.apache.commons.lang3.StringUtils)2 EventDriven (org.apache.nifi.annotation.behavior.EventDriven)2 InputRequirement (org.apache.nifi.annotation.behavior.InputRequirement)2