private static bool CheckIfKeyFulfillsRequirements(string key, KeyValuePairFilter filter) { if (filter.key != null) { var k = filter.key; if (key == null) { return(false); } if (!filter.caseSensitive) { key = key.ToLower(); k = k.ToLower(); } k = SerializeStringIfNotString(k); key = SerializeStringIfNotString(key); if (k != key) { return(false); } return(true); } return(false); }
/// <summary> /// Searches a json string for objects filtered by their key value pairs. /// </summary> /// <param name="jsonData">the json string which will be examined</param> /// <param name="key">the desired key of the key value pair contained in the objects</param> /// <param name="value">the desired value of the key value pair contained in the objects</param> /// <param name="valueType">value type of the key value pair's value</param> /// <param name="arrayEntryOrKvpValue">//TODO</param> /// <param name="caseSensitive">key and value filter is case sensitive</param> /// <param name="returnFirstInstance">stops searching for more objects after finding one</param> /// <param name="depth">maximum amount of indices</param> /// <returns>list of indices to the desired objects</returns> public static List <string[]> FindObjects(string jsonData, string key, string value, ValueType valueType, ArrayEntryOrKvpValue arrayEntryOrKvpValue, bool caseSensitive, bool returnFirstInstance, int depth) { var filters = new KeyValuePairFilter[] { new KeyValuePairFilter(key, value, valueType, caseSensitive, true, arrayEntryOrKvpValue) }; return(FindObjects(jsonData, new string[0], filters, returnFirstInstance, depth)); }
private static bool CheckIfValueFulfillsRequirements(string value, KeyValuePairFilter filter) { var v = value; value = filter.value; var valueType = filter.valueType; var caseSensitive = filter.caseSensitive; if (CheckValueType(value) == ValueType.Object) { if (CheckValueType(v) == ValueType.Number && (valueType == ValueType.Number || valueType == ValueType.Unspecified)) { var f = DeserializeObject(value); foreach (var fkvp in f) { if (DeserializeString(fkvp.Key.ToLower()) == "gt") { double a; double w; if (Double.TryParse(fkvp.Value, out a) && Double.TryParse(v, out w)) { if (w < a) { return(false); } } else { continue; } } if (DeserializeString(fkvp.Key.ToLower()) == "lt") { double a; double w; if (Double.TryParse(fkvp.Value, out a) && Double.TryParse(v, out w)) { if (w > a) { return(false); } } else { continue; } } } return(true); } } if (value != null) { if (v == null) { return(false); } if (!caseSensitive) { value = value.ToLower(); v = v.ToLower(); } v = SerializeStringIfInvalid(v); value = SerializeStringIfInvalid(value); if (v != value) { return(false); } } if (valueType != ValueType.Unspecified) { if (CheckValueType(v) != valueType) { return(false); } } return(true); }
private static bool CheckIfObjectFulfillsRequirements(Dictionary <string, string> objectDictionary, KeyValuePairFilter filter, bool arrayEntry) { var key = filter.key; var value = filter.value; var valueType = filter.valueType; var caseSensitive = filter.caseSensitive; var keyIsMandatory = filter.keyIsMandatory; var arrayEntryOrKvpValue = filter.arrayEntryOrKvpValue; if ((arrayEntry && arrayEntryOrKvpValue == ArrayEntryOrKvpValue.KvpValue) || (!arrayEntry && arrayEntryOrKvpValue == ArrayEntryOrKvpValue.ArrayEntry)) { return(false); } foreach (var kvp in objectDictionary) { if (key != null) { if (CheckIfKeyFulfillsRequirements(kvp.Key, filter)) { if (value != null) { return(CheckIfValueFulfillsRequirements(kvp.Value, filter)); } else { return(true); } } } else { if (CheckIfValueFulfillsRequirements(kvp.Value, filter)) { return(true); } } } return(!keyIsMandatory); }