public void When_array_contains_only_primitive_types_return_same_array_back() { var array = new SimpleJson.JsonArray(); array.Add(1); array.Add(3); array.Add(5); var attrFilter = new Elements.JAttributeFilter(); attrFilter.Attribute = new Elements.JAttribute(); attrFilter.Attribute.Name = "Test"; attrFilter.Filter = new Elements.JFilter(); attrFilter.Filter.FilterType = Elements.JFilter.FilterTypes.Equal; attrFilter.Filter.Value = 1; JSONQuerySelector json = new JSONQuerySelector(); var results = json.ApplyFilter(array, attrFilter, (value1, value2) => { return(Convert.ToInt32(value1) == Convert.ToInt32(value2)); }); Assert.AreEqual(results.Count, array.Count); Assert.AreEqual(results[0], array[0]); Assert.AreEqual(results[1], array[1]); Assert.AreEqual(results[2], array[2]); }
/// <summary> /// Execute filter on items of given JSON array using evaluator /// </summary> /// <param name="target">JSON array</param> /// <param name="filter">Filter to apply</param> /// <param name="filterEvaluator">Evaluation function which contains the logic of how given filter is applied</param> /// <returns>Matching results in array format</returns> public SimpleJson.JsonArray ApplyFilter(SimpleJson.JsonArray target, Elements.JAttributeFilter filter, Func <object, object, bool> filterEvaluator) { bool isArrayItemsTypeOfDictionaryObject = false; var resultsToReturn = new SimpleJson.JsonArray(); foreach (var targetItem in target) { if (targetItem is IDictionary <string, object> ) { var targetItemSource = targetItem as IDictionary <string, object>; if (EvaluateFilter(targetItemSource, filter, filterEvaluator)) { resultsToReturn.Add(targetItemSource); } isArrayItemsTypeOfDictionaryObject = true; } } if (isArrayItemsTypeOfDictionaryObject) { return(resultsToReturn); } else { // Right we do not support filtering on array with primitive values, so in that case, return target back return(target); } }