microsoft / RulesEngine

A Json based Rules Engine with extensive Dynamic expression support
https://microsoft.github.io/RulesEngine/
MIT License
3.6k stars 543 forks source link

Using dynamic RuleParameter #546

Closed wmmihaa closed 10 months ago

wmmihaa commented 11 months ago

I have a scenario where I receive none typed JSON messages and would like to validate the payload. E.g

var  reading = JsonConvert.DeserializeObject($"{{\"temp\":42.0}}");
var message = new RuleParameter("reading", reading);
var result = await ruleEngine.ExecuteAllRulesAsync("TemperatureWarning", message, nodeId);

But I get an error saying: Exception while parsing expression 'reading.temp < 3 ' - The binary operator LessThan is not defined for the types 'Newtonsoft.Json.Linq.JToken' and 'Newtonsoft.Json.Linq.JToken'.

Does anyone have a suggestion of how to get around this?

abbasc52 commented 11 months ago

@wmmihaa Can you try with ExpandoObjectConverter ? Something like

 var converter = new ExpandoObjectConverter();
  var telemetryInfo = "{\"noOfVisitsPerMonth\": 10,\"percentageOfBuyingToVisit\": 15}";
  return JsonConvert.DeserializeObject<ExpandoObject>(telemetryInfo, converter);

If you want to use JObject , you might need cast the JObject before user int(reading.temp) < 3

abbasc52 commented 10 months ago

Closing issue due to inactivity