Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exception in object Apply(JToken rule, object data); if we pass a dictionary containing a field that has an empty string. #32

Open
Hassan-173732 opened this issue Aug 20, 2021 · 0 comments

Comments

@Hassan-173732
Copy link

`static void Main(string[] args)
{

        // The data that the rule will run against. 
        //object data = new { MyNumber = 8 };
        var inputData = new Dictionary<string, object> { { "RRLFLD0128", "" } };

        // Rule definition retrieved as JSON text
        string jsonText = "{\"and\":[{\"<=\":[{\"var\":\"RRLFLD0128\"},25]}]}";

        // Parse json into hierarchical structure
        var rule = JObject.Parse(jsonText);

        // Create an evaluator with default operators.
        var evaluator = new JsonLogicEvaluator(EvaluateOperators.Default);

        // Apply the rule to the data.
        object result = evaluator.Apply(rule, inputData);
       
    }`

The inputData passed to the evaluator.Apply(rule, inputData) causes an exception because we are trying to compare a integer number to an empty string so the parser is unable to map the empty string to a valid number.
For this I modified the method GenericArgsSatisfy in the EvaluateOperator class:
Previously the return method was:
return isAllText ? CheckCriteria(values.Cast<string>().ToArray(), criteriaText) : CheckCriteria(values.Select(a => a == null ? 0d : Double.Parse(a.ToString())).ToArray(), criteriaDouble);
After Modification:
return isAllText ? CheckCriteria(values.Cast<string>().ToArray(), criteriaText) : CheckCriteria(values.Select(a => a == null || (a is string && string.IsNullOrEmpty(a.ToString())) ? 0d : Double.Parse(a.ToString())).ToArray(), criteriaDouble);
Now the function can parse the empty string to null instead of throwing and exception and the apply method produces the desired results.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant