use of org.jsfr.json.exception.JsonPathCompilerException in project hazelcast by hazelcast.
the class JsonValueFunction method eval.
@Override
public T eval(final Row row, final ExpressionEvalContext context) {
// first evaluate the required parameter
final String path = (String) operands[1].eval(row, context);
if (path == null) {
throw QueryException.error("SQL/JSON path expression cannot be null");
}
// needed for further checks, can be a dynamic expression, therefore can not be inlined as part of function args
final Object defaultOnEmpty = operands[2].eval(row, context);
final Object defaultOnError = operands[3].eval(row, context);
final Object operand0 = operands[0].eval(row, context);
String json = operand0 instanceof HazelcastJsonValue ? operand0.toString() : (String) operand0;
if (json == null) {
json = "";
}
final JsonPath jsonPath;
try {
jsonPath = pathCache.asMap().computeIfAbsent(path, JsonPathUtil::compile);
} catch (JsonPathCompilerException e) {
// We deliberately don't use the cause here. The reason is that exceptions from ANTLR are not always
// serializable, they can contain references to parser context and other objects, which are not.
// That's why we also log the exception here.
LOGGER.fine("JSON_QUERY JsonPath compilation failed", e);
throw QueryException.error("Invalid SQL/JSON path expression: " + e.getMessage());
}
Collection<Object> resultColl;
try {
resultColl = JsonPathUtil.read(json, jsonPath);
} catch (Exception e) {
return onErrorResponse(e, defaultOnError);
}
if (resultColl.isEmpty()) {
return onEmptyResponse(defaultOnEmpty);
}
if (resultColl.size() > 1) {
throw QueryException.error("JSON_VALUE evaluated to multiple values");
}
Object onlyResult = resultColl.iterator().next();
if (JsonPathUtil.isArrayOrObject(onlyResult)) {
return onErrorResponse(QueryException.error("Result of JSON_VALUE cannot be array or object"), defaultOnError);
}
@SuppressWarnings("unchecked") T result = (T) convertResultType(onlyResult);
return result;
}
use of org.jsfr.json.exception.JsonPathCompilerException in project hazelcast by hazelcast.
the class JsonQueryFunction method eval.
@Override
public HazelcastJsonValue eval(final Row row, final ExpressionEvalContext context) {
// first evaluate the required parameter
final String path = (String) operands[1].eval(row, context);
if (path == null) {
throw QueryException.error("SQL/JSON path expression cannot be null");
}
final Object operand0 = operands[0].eval(row, context);
String json = operand0 instanceof HazelcastJsonValue ? operand0.toString() : (String) operand0;
if (json == null) {
json = "";
}
final JsonPath jsonPath;
try {
jsonPath = pathCache.asMap().computeIfAbsent(path, JsonPathUtil::compile);
} catch (JsonPathCompilerException e) {
// We deliberately don't use the cause here. The reason is that exceptions from ANTLR are not always
// serializable, they can contain references to parser context and other objects, which are not.
// That's why we also log the exception here.
LOGGER.fine("JSON_QUERY JsonPath compilation failed", e);
throw QueryException.error("Invalid SQL/JSON path expression: " + e.getMessage());
}
return wrap(execute(json, jsonPath));
}
Aggregations