/// <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); } }
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]); }
public static bool isUniqueMessage(string message, Dictionary <string, object> thisData = null) { if (!filterMessages) { return(true); } var client = new RestClient("https://api.rollbar.com/api/1"); var request = new RestRequest("/items/", Method.GET); request.AddParameter("access_token", rollbarReadToken); var clientResponse = client.Execute <Dictionary <string, object> >(request); Dictionary <string, object> response = clientResponse.Data; object val; response.TryGetValue("err", out val); // Check for errors before we proceed if ((long)val == 0) { response.TryGetValue("result", out val); Dictionary <string, object> result = (Dictionary <string, object>)val; result.TryGetValue("items", out val); SimpleJson.JsonArray jsonArray = (SimpleJson.JsonArray)val; object[] items = jsonArray.ToArray(); foreach (object Item in items) { Dictionary <string, object> item = (Dictionary <string, object>)Item; string itemMessage = JsonParsing.getString(item, "title"); string itemStatus = JsonParsing.getString(item, "status"); string itemResolvedVersion = JsonParsing.getString(item, "resolved_in_version"); long itemId = JsonParsing.getLong(item, "id"); bool uniqueData = isUniqueData(itemId, thisData); if (itemMessage == message) { // Filter messages & data so that we send only reports which are unique if (itemStatus == "active" && !uniqueData) { return(false); // Note that if an item reoccurs after being marked as "resolved" it is automatically reactivated. } else if (itemResolvedVersion != null) { if (Versioning.Compare(itemResolvedVersion, Constants.EDDI_VERSION) > 0) { return(false); // This has been marked as resolved in a more current client version. } } } } } return(true); }
public static bool isUniqueData(long itemId, Dictionary <string, object> thisData = null) { if (thisData is null) { return(true); } var client = new RestClient("https://api.rollbar.com/api/1"); var request = new RestRequest("/item/" + itemId + "/instances/", Method.GET); request.AddParameter("access_token", rollbarReadToken); var clientResponse = client.Execute <Dictionary <string, object> >(request); Dictionary <string, object> response = clientResponse.Data; object val; response.TryGetValue("err", out val); // Check for errors before we proceed if ((long)val == 0) { response.TryGetValue("result", out val); Dictionary <string, object> result = (Dictionary <string, object>)val; result.TryGetValue("instances", out val); SimpleJson.JsonArray jsonArray = (SimpleJson.JsonArray)val; object[] instances = jsonArray.ToArray(); foreach (object Instance in instances) { Dictionary <string, object> instance = (Dictionary <string, object>)Instance; instance.TryGetValue("data", out val); Dictionary <string, object> instanceData = (Dictionary <string, object>)val; instanceData.TryGetValue("custom", out val); Dictionary <string, object> customData = (Dictionary <string, object>)val; if (customData != null) { if (customData.Keys.Count == thisData.Keys.Count && customData.Keys.All(k => thisData.ContainsKey(k) && Equals(thisData[k], customData[k]))) { return(false); } } } } return(true); }
public static SimpleJson.JsonArray StringToJa(string str, bool defaultNull = false) { try { if (!string.IsNullOrEmpty(str)) { SimpleJson.JsonArray ja = SimpleJson.SimpleJson.DeserializeObject <SimpleJson.JsonArray>(str); if (ja != null) { return(ja); } } } catch (Exception e) { Debugger.LogError(string.Format("Can not convert {0} to JsonArray with {1}", str, e)); } return(defaultNull ? null : new SimpleJson.JsonArray()); }
public static EtcdResultNode Parse(SimpleJson.JsonObject jsonObj) { EtcdResultNode node = new EtcdResultNode(); node.key = (string)jsonObj["key"]; node.createdIndex = (long)jsonObj["createdIndex"]; node.modifiedIndex = (long)jsonObj["modifiedIndex"]; if (jsonObj.ContainsKey("dir")) { node.dir = (bool)jsonObj["dir"]; } if (jsonObj.ContainsKey("ttl")) { node.ttl = (long)jsonObj["ttl"]; } if (jsonObj.ContainsKey("expiration")) { DateTime expiration; if (DateTime.TryParse((string)jsonObj["expiration"], out expiration)) { node.expiration = expiration; } } if (jsonObj.ContainsKey("value")) { node.value = (string)jsonObj["value"]; } if (jsonObj.ContainsKey("nodes")) { SimpleJson.JsonArray children = (SimpleJson.JsonArray)jsonObj["nodes"]; node.nodes = new EtcdResultNode[children.Count]; for (int i = 0; i < children.Count; ++i) { node.nodes[i] = Parse((SimpleJson.JsonObject)children[i]); } } return(node); }
/// <summary> /// Search given expression in JSON data and return results /// </summary> /// <param name="jsonData">JSON data to be searched</param> /// <param name="expression">Search expression</param> /// <returns>Search results whose types might be different depending on given JSON data structure and expression</returns> public object SearchValue(string jsonData, string expression) { if (String.IsNullOrEmpty(jsonData)) { return(null); } jsonData = parser.ClearExpression(jsonData); object currentData = SimpleJson.SimpleJson.DeserializeObject(jsonData); var currentElement = parser.ParseExpression(expression); while (currentElement != null && currentData != null) { if (String.IsNullOrEmpty(currentElement.Name)) // [0]. or [0][Attr = "..."]. { currentData = FilterByAttributes(currentData, currentElement.AttributeFilter); currentData = FilterByIndex(currentData, currentElement); } else if (currentData is IDictionary <string, object> ) { currentData = SelectData((IDictionary <string, object>)currentData, currentElement); } else if (currentData is SimpleJson.JsonArray) { var tmpData = currentData as SimpleJson.JsonArray; SimpleJson.JsonArray foundItems = null; foreach (var tmpDataItem in tmpData) { if (tmpDataItem is IDictionary <string, object> ) { var results = SelectData((IDictionary <string, object>)tmpDataItem, currentElement); if (results != null) { if (foundItems == null) { foundItems = new SimpleJson.JsonArray(); } if (results is SimpleJson.JsonArray) { foundItems.AddRange((SimpleJson.JsonArray)results); } else { foundItems.Add(results); } } } } currentData = foundItems; } currentElement = currentElement.Child; } // Return only value when array contains 1 element with primitive type if (currentData is SimpleJson.JsonArray) { var currentDataItems = currentData as SimpleJson.JsonArray; if (currentDataItems.Count == 1 && currentDataItems[0].IsPrimitive()) { currentData = currentDataItems[0]; } } return(currentData); }
private static string format(SimpleJson.JsonObject json, string strTab = "", bool bAddComma = false) { string strOut = strTab + "{" + Environment.NewLine; string strOriginalTab = strTab; bool bOriginalAddComma = bAddComma; strTab += " "; KeyValuePair <string, object>[] rgItems = json.ToArray(); for (int i = 0; i < rgItems.Length; i++) { string strKey = rgItems[i].Key; SimpleJson.JsonArray children = rgItems[i].Value as SimpleJson.JsonArray; strOut += strTab + "\"" + strKey + "\":"; if (children != null) { if (children.Count > 1) { strOut += "["; } strOut += Environment.NewLine; bAddComma = true; int nIdx = 0; foreach (object objChild in children) { if (nIdx >= children.Count - 1) { bAddComma = false; } SimpleJson.JsonObject child = objChild as SimpleJson.JsonObject; if (child != null) { strOut += format(child, strTab + " ", bAddComma); } else { strOut += strTab + " "; if (objChild == null) { strOut += "null"; } else { strOut += "\"" + objChild.ToString() + "\""; } strOut += (bAddComma ? "," : "") + Environment.NewLine; } nIdx++; } if (children.Count > 1) { strOut += strTab + "]" + Environment.NewLine; } } else { SimpleJson.JsonObject child = rgItems[i].Value as SimpleJson.JsonObject; if (child != null) { strOut += format(child, strTab + " ", bAddComma); } else { if (rgItems[i].Value == null) { strOut += "null"; } else { strOut += "\"" + rgItems[i].Value.ToString() + "\""; } if (bAddComma) { strOut += ","; } strOut += Environment.NewLine; } } } strOut += strOriginalTab + "}" + (bOriginalAddComma ? "," : "") + Environment.NewLine; return(strOut); }
private static void extract(SimpleJson.JsonObject json, JsonData root) { KeyValuePair <string, object>[] rgItems = json.ToArray(); for (int i = 0; i < rgItems.Length; i++) { string strKey = rgItems[i].Key; SimpleJson.JsonArray children = rgItems[i].Value as SimpleJson.JsonArray; JsonData data = new JsonData(strKey); if (children != null) { int nIdx = 0; foreach (object objChild in children) { JsonData child1 = new JsonData(nIdx.ToString()); SimpleJson.JsonObject child = objChild as SimpleJson.JsonObject; if (child != null) { extract(child, child1); } else { if (objChild == null) { data.Values.Add("null"); } else { data.Values.Add(objChild.ToString()); } } nIdx++; data.Children.Add(child1); } } else { SimpleJson.JsonObject child = rgItems[i].Value as SimpleJson.JsonObject; if (child != null) { extract(child, data); } else { if (rgItems[i].Value == null) { data.Values.Add("null"); } else { data.Values.Add(rgItems[i].Value.ToString()); } } } root.Children.Add(data); } }
public string[] eth_getWork() { string strCmd = "{\"jsonrpc\":\"2.0\",\"method\":\"eth_getWork\",\"params\":[],\"id\":1}"; byte[] rgCmd = Encoding.UTF8.GetBytes(strCmd); HttpWebRequest request = (HttpWebRequest)WebRequest.Create(m_strUrl); request.ContentType = "application/json"; request.Method = "POST"; request.ContentLength = rgCmd.Length; Stream strm = request.GetRequestStream(); strm.Write(rgCmd, 0, rgCmd.Length); strm.Close(); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); string strStatus = response.StatusDescription; HttpStatusCode status = response.StatusCode; strm = response.GetResponseStream(); string strContent = ""; using (StreamReader sr = new StreamReader(strm)) { strContent = sr.ReadToEnd(); } SimpleJson.JsonObject json = SimpleJson.SimpleJson.DeserializeObject(strContent) as SimpleJson.JsonObject; List <string> rgstr = new List <string>(); if (json.Count == 3) { if (!verifyValue(json, "jsonrpc", "2.0")) { return(null); } if (!verifyValue(json, "id", "1")) { return(null); } SimpleJson.JsonArray rgObj = getValue(json, "result") as SimpleJson.JsonArray; if (rgObj == null) { return(null); } if (rgObj.Count != 4) { return(null); } string strCurrentBlockHeader = rgObj[0].ToString(); string strSeedHash = rgObj[1].ToString(); string strBoundaryCondition = rgObj[2].ToString(); rgstr.Add(strCurrentBlockHeader); rgstr.Add(strSeedHash); rgstr.Add(strBoundaryCondition); } if (rgstr.Count == 0) { return(null); } return(rgstr.ToArray()); }