diff --git a/JsonRuleEngine.Net.Tests/Evaluate.cs b/JsonRuleEngine.Net.Tests/Evaluate.cs index b386ff9..40e4176 100644 --- a/JsonRuleEngine.Net.Tests/Evaluate.cs +++ b/JsonRuleEngine.Net.Tests/Evaluate.cs @@ -194,13 +194,22 @@ public void DictionaryList() var conditions = JsonConvert.DeserializeObject("{\"operator\":\"in\",\"field\":\"Test\"}"); conditions.Value = new List() { - "1", - "2" + "1" }; bool result = JsonRuleEngine.Evaluate( dict, conditions ); + + conditions = JsonConvert.DeserializeObject("{\"operator\":\"in\",\"field\":\"Test\"}"); + conditions.Value = new JArray + { + "2" + }; + result = JsonRuleEngine.Evaluate( + dict, + conditions + ); Assert.True(result); // Return true } diff --git a/JsonRuleEngine.Net/Engine/JsonRuleEngine.cs b/JsonRuleEngine.Net/Engine/JsonRuleEngine.cs index 60bc2b6..bf3ad33 100644 --- a/JsonRuleEngine.Net/Engine/JsonRuleEngine.cs +++ b/JsonRuleEngine.Net/Engine/JsonRuleEngine.cs @@ -435,9 +435,38 @@ private static Type GetDictionaryType(object value, ConditionRuleOperator op) } } + if (value is JArray) + return typeof(IEnumerable<>).MakeGenericType(((JArray)value).GetJArrayType()); + return value.GetType(); } + private static Type GetJArrayType(this JArray array) + { + var firstValue = array.FirstOrDefault(); + if (firstValue != null) + { + switch (firstValue.Type) + { + case JTokenType.String: + return typeof(string); + case JTokenType.Float: + return typeof(double); + case JTokenType.Object: + return typeof(object); + case JTokenType.Date: + return typeof(DateTime); + case JTokenType.Boolean: + return typeof(Boolean); + case JTokenType.Integer: + return typeof(int); + case JTokenType.Array: + return typeof(JArray); + } + } + return null; + } + /// /// Handle table rule @@ -453,7 +482,7 @@ private static Expression HandleTableRule(Expression array, Expression param, Co { var currentField = remainingFields.First(); - var childType = array.Type == typeof(JArray) ? ((JArray)value).FirstOrDefault()?.GetType() : array.Type.GetGenericArguments().First(); + var childType = array.Type == typeof(JArray) ? ((JArray)value).GetJArrayType() : array.Type.GetGenericArguments().First(); // Contains methods // Need a conversion to an array of string