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()));
}
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();
}
}
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);
}
}
Aggregations