// Test for any true matching condition(s) private bool Matches(Newtonsoft.Json.Linq.JObject json) { for (int i = 0; i < Match.Length; i += 2) { string field = Match[i]; string expr = Match[i + 1]; JToken token = null; if (json.TryGetValue(field, out token)) { string text = token.ToString(); if (!string.IsNullOrEmpty(text)) { var resolver = new RegexGrokResolver(); var pattern = resolver.ResolveToRegex(expr); var match = Regex.Match(text, pattern); if (match.Success) { var regex = new Regex(pattern); var namedCaptures = regex.MatchNamedCaptures(text); foreach (string fieldName in namedCaptures.Keys) { AddOrModify(json, fieldName, namedCaptures[fieldName]); } return true; // Yes! } } if (string.IsNullOrEmpty(expr)) return true; // Empty field is no match } } return false; // Not specified is failure }
protected override Entity ReadJson(Entity entity, Type objectType, Newtonsoft.Json.Linq.JObject json, JsonSerializer serializer) { var conn = base.ReadJson(entity, objectType, json, serializer) as Connection; if (conn == null) return null; // Parse the relation information // Relation id JToken value; if (json.TryGetValue("__relationid", out value) == true && value.Type != JTokenType.Null) conn.RelationId = value.ToString(); // Parse the endpoints if (json.TryGetValue("__endpointa", out value) == true && value.Type == JTokenType.Object) conn.EndpointA = ParseEndpoint(value as JObject, serializer); else throw new Exception(string.Format("Endpoint A for connection with id {0} is invalid.", conn.Id)); if (json.TryGetValue("__endpointb", out value) == true && value.Type == JTokenType.Object) conn.EndpointB = ParseEndpoint(value as JObject, serializer); else throw new Exception(string.Format("Endpoint B for connection with id {0} is invalid.", conn.Id)); return conn; }
private bool Matches(Newtonsoft.Json.Linq.JObject json) { string field = Match[0]; CultureInfo ci = CultureInfo.CreateSpecificCulture(Locale); JToken token = null; if (json.TryGetValue(field, out token)) { string text = token.ToString(); if (!string.IsNullOrEmpty(text)) { DateTime ts; var exprArray = Match.Skip(1).ToArray(); var resolver = new RegexGrokResolver(); for (int i=0; i<exprArray.Length; i++) { var pattern = resolver.ResolveToRegex(exprArray[i]); exprArray[i] = pattern; } if (DateTime.TryParseExact(text, exprArray, ci, DateTimeStyles.None, out ts)) AddOrModify(json, ts); } return true; // Empty field is no match } return false; // Not specified is failure }