Added jpath operator

master
M66B 2 weeks ago
parent a477065676
commit 9b051f8c2c

@ -3026,6 +3026,7 @@ The following extra operators are available:
* *contains* (string/array of strings contains substring) * *contains* (string/array of strings contains substring)
* *matches* (string/array of strings matches regex) * *matches* (string/array of strings matches regex)
* *startswith* / *endswith* (string/array of strings starting or ending with a string; since version 1.2233) * *startswith* / *endswith* (string/array of strings starting or ending with a string; since version 1.2233)
* *jpath* (string/array of JSON strings matching the [specified path](https://github.com/json-path/jsonpath); since version 1.2233)
The following extra functions are available: The following extra functions are available:

@ -37,6 +37,7 @@ import com.ezylang.evalex.operators.InfixOperator;
import com.ezylang.evalex.parser.ASTNode; import com.ezylang.evalex.parser.ASTNode;
import com.ezylang.evalex.parser.ParseException; import com.ezylang.evalex.parser.ParseException;
import com.ezylang.evalex.parser.Token; import com.ezylang.evalex.parser.Token;
import com.jayway.jsonpath.JsonPath;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
@ -123,6 +124,7 @@ public class ExpressionHelper {
ContainsOperator oMatches = new ContainsOperator(true); ContainsOperator oMatches = new ContainsOperator(true);
StartsWithOperator oStartsWith = new StartsWithOperator(false); StartsWithOperator oStartsWith = new StartsWithOperator(false);
StartsWithOperator oEndsWith = new StartsWithOperator(true); StartsWithOperator oEndsWith = new StartsWithOperator(true);
JPathOperator oJPath = new JPathOperator();
ExpressionConfiguration configuration = ExpressionConfiguration.defaultConfiguration(); ExpressionConfiguration configuration = ExpressionConfiguration.defaultConfiguration();
@ -142,6 +144,7 @@ public class ExpressionHelper {
configuration.getOperatorDictionary().addOperator("Matches", oMatches); configuration.getOperatorDictionary().addOperator("Matches", oMatches);
configuration.getOperatorDictionary().addOperator("StartsWith", oStartsWith); configuration.getOperatorDictionary().addOperator("StartsWith", oStartsWith);
configuration.getOperatorDictionary().addOperator("EndsWith", oEndsWith); configuration.getOperatorDictionary().addOperator("EndsWith", oEndsWith);
configuration.getOperatorDictionary().addOperator("JPath", oJPath);
Expression expression = new Expression(eval, configuration) Expression expression = new Expression(eval, configuration)
.with("received", message == null ? null : message.received) .with("received", message == null ? null : message.received)
@ -640,4 +643,44 @@ public class ExpressionHelper {
return expression.convertValue(result); return expression.convertValue(result);
} }
} }
@InfixOperator(precedence = OPERATOR_PRECEDENCE_COMPARISON)
public static class JPathOperator extends AbstractOperator {
JPathOperator() {
}
@Override
public EvaluationValue evaluate(
Expression expression, Token operatorToken, EvaluationValue... operands) {
String result = null;
try {
if (operands.length == 2) {
List<EvaluationValue> array;
if (operands[0].getDataType() == EvaluationValue.DataType.ARRAY)
array = operands[0].getArrayValue();
else
array = Arrays.asList(operands[0]);
String path = operands[1].getStringValue();
if (array != null && !array.isEmpty() && !TextUtils.isEmpty(path))
for (EvaluationValue item : array) {
String value = item.getStringValue();
if (!TextUtils.isEmpty(value)) {
result = JsonPath.read(value, path);
break;
}
}
}
} catch (Throwable ex) {
Log.e("EXPR", ex);
}
Log.i("EXPR " + operands[0] + "JSONPATH" + operands[1] +
" result=" + result);
return expression.convertValue(result);
}
}
} }

@ -1656,6 +1656,7 @@ X-Google-Original-From: Somebody &lt;somebody+extra@example.org&gt;</code></pre>
<li><em>contains</em> (string/array of strings contains substring)</li> <li><em>contains</em> (string/array of strings contains substring)</li>
<li><em>matches</em> (string/array of strings matches regex)</li> <li><em>matches</em> (string/array of strings matches regex)</li>
<li><em>startswith</em> / <em>endswith</em> (string/array of strings starting or ending with a string; since version 1.2233)</li> <li><em>startswith</em> / <em>endswith</em> (string/array of strings starting or ending with a string; since version 1.2233)</li>
<li><em>jpath</em> (string/array of JSON strings matching the <a href="https://github.com/json-path/jsonpath">specified path</a>; since version 1.2233)</li>
</ul> </ul>
<p>The following extra functions are available:</p> <p>The following extra functions are available:</p>
<ul> <ul>

Loading…
Cancel
Save