public override bool IsMatch(JToken root, JToken t, JsonSelectSettings?settings) { switch (Operator) { case QueryOperator.And: foreach (QueryExpression e in Expressions) { if (!e.IsMatch(root, t, settings)) { return(false); } } return(true); case QueryOperator.Or: foreach (QueryExpression e in Expressions) { if (e.IsMatch(root, t, settings)) { return(true); } } return(false); default: throw new ArgumentOutOfRangeException(); } }
public override bool IsMatch(JToken root, JToken t, JsonSelectSettings?settings) { if (Operator == QueryOperator.Exists) { return(GetResult(root, t, Left).Any()); } using (var leftResults = GetResult(root, t, Left).GetEnumerator()) { if (leftResults.MoveNext()) { var rightResultsEn = GetResult(root, t, Right); var rightResults = rightResultsEn as ICollection <JToken> ?? rightResultsEn.ToList(); do { var leftResult = leftResults.Current; foreach (var rightResult in rightResults) { if (MatchTokens(leftResult, rightResult, settings)) { return(true); } } } while (leftResults.MoveNext()); } } return(false); }
public override IEnumerable <JToken> ExecuteFilter( JToken root, IEnumerable <JToken> current, JsonSelectSettings?settings ) { foreach (JToken t in current) { if (t is JContainer c) { foreach (JToken d in c.DescendantsAndSelf()) { if (Expression.IsMatch(root, d, settings)) { yield return(d); } } } else { if (Expression.IsMatch(root, t, settings)) { yield return(t); } } } }
public override IEnumerable <JToken> ExecuteFilter( JToken root, IEnumerable <JToken> current, JsonSelectSettings?settings ) { foreach (JToken c in current) { JToken?value = c; while (true) { JContainer?container = value as JContainer; value = GetNextScanValue(c, container, value); if (value == null) { break; } if (value is JProperty property) { foreach (string name in _names) { if (property.Name == name) { yield return(property.Value); } } } } } }
public override IEnumerable <JToken> ExecuteFilter( JToken root, IEnumerable <JToken> current, JsonSelectSettings?settings ) { return(new[] { root }); }
protected static JToken?GetTokenIndex(JToken t, JsonSelectSettings?settings, int index) { if (t is JArray a) { if (a.Count <= index) { if (settings?.ErrorWhenNoMatch ?? false) { throw new JsonException( "Index {0} outside the bounds of JArray.".FormatWith( CultureInfo.InvariantCulture, index ) ); } return(null); } return(a[index]); } else if (t is JConstructor c) { if (c.Count <= index) { if (settings?.ErrorWhenNoMatch ?? false) { throw new JsonException( "Index {0} outside the bounds of JConstructor.".FormatWith( CultureInfo.InvariantCulture, index ) ); } return(null); } return(c[index]); } else { if (settings?.ErrorWhenNoMatch ?? false) { throw new JsonException( "Index {0} not valid on {1}.".FormatWith( CultureInfo.InvariantCulture, index, t.GetType().Name ) ); } return(null); } }
public override IEnumerable <JToken> ExecuteFilter( JToken root, IEnumerable <JToken> current, JsonSelectSettings?settings ) { foreach (JToken t in current) { if (t is JObject o) { if (Name != null) { JToken?v = o[Name]; if (v != null) { yield return(v); } else if (settings?.ErrorWhenNoMatch ?? false) { throw new JsonException( "Property '{0}' does not exist on JObject.".FormatWith( CultureInfo.InvariantCulture, Name ) ); } } else { foreach (KeyValuePair <string, JToken?> p in o) { yield return(p.Value !); } } } else { if (settings?.ErrorWhenNoMatch ?? false) { throw new JsonException( "Property '{0}' not valid on {1}.".FormatWith( CultureInfo.InvariantCulture, Name ?? "*", t.GetType().Name ) ); } } } }
public override IEnumerable <JToken> ExecuteFilter( JToken root, IEnumerable <JToken> current, JsonSelectSettings?settings ) { foreach (JToken t in current) { if (t is JObject o) { foreach (string name in Names) { JToken?v = o[name]; if (v != null) { yield return(v); } if (settings?.ErrorWhenNoMatch ?? false) { throw new JsonException( "Property '{0}' does not exist on JObject.".FormatWith( CultureInfo.InvariantCulture, name ) ); } } } else { if (settings?.ErrorWhenNoMatch ?? false) { throw new JsonException( "Properties {0} not valid on {1}.".FormatWith( CultureInfo.InvariantCulture, string.Join( ", ", Names.Select(n => "'" + n + "'") #if !HAVE_STRING_JOIN_WITH_ENUMERABLE .ToArray() #endif ), t.GetType().Name ) ); } } } }
internal static IEnumerable <JToken> Evaluate( List <PathFilter> filters, JToken root, JToken t, JsonSelectSettings?settings ) { IEnumerable <JToken> current = new[] { t }; foreach (PathFilter filter in filters) { current = filter.ExecuteFilter(root, current, settings); } return(current); }
public override IEnumerable <JToken> ExecuteFilter( JToken root, IEnumerable <JToken> current, JsonSelectSettings?settings ) { foreach (JToken t in current) { foreach (JToken v in t) { if (Expression.IsMatch(root, v, settings)) { yield return(v); } } } }
public override IEnumerable <JToken> ExecuteFilter( JToken root, IEnumerable <JToken> current, JsonSelectSettings?settings ) { foreach (JToken t in current) { foreach (int i in Indexes) { JToken?v = GetTokenIndex(t, settings, i); if (v != null) { yield return(v); } } } }
public override IEnumerable <JToken> ExecuteFilter( JToken root, IEnumerable <JToken> current, JsonSelectSettings?settings ) { foreach (JToken t in current) { if (Index != null) { JToken?v = GetTokenIndex(t, settings, Index.GetValueOrDefault()); if (v != null) { yield return(v); } } else { if (t is JArray || t is JConstructor) { foreach (JToken v in t) { yield return(v); } } else { if (settings?.ErrorWhenNoMatch ?? false) { throw new JsonException( "Index * not valid on {0}.".FormatWith( CultureInfo.InvariantCulture, t.GetType().Name ) ); } } } } }
public override IEnumerable <JToken> ExecuteFilter(JToken root, IEnumerable <JToken> current, JsonSelectSettings?settings) { if (Step == 0) { throw new JsonException("Step cannot be zero."); } foreach (JToken t in current) { if (t is JArray a) { // set defaults for null arguments int stepCount = Step ?? 1; int startIndex = Start ?? ((stepCount > 0) ? 0 : a.Count - 1); int stopIndex = End ?? ((stepCount > 0) ? a.Count : -1); // start from the end of the list if start is negative if (Start < 0) { startIndex = a.Count + startIndex; } // end from the start of the list if stop is negative if (End < 0) { stopIndex = a.Count + stopIndex; } // ensure indexes keep within collection bounds startIndex = Math.Max(startIndex, (stepCount > 0) ? 0 : int.MinValue); startIndex = Math.Min(startIndex, (stepCount > 0) ? a.Count : a.Count - 1); stopIndex = Math.Max(stopIndex, -1); stopIndex = Math.Min(stopIndex, a.Count); bool positiveStep = (stepCount > 0); if (IsValid(startIndex, stopIndex, positiveStep)) { for (int i = startIndex; IsValid(i, stopIndex, positiveStep); i += stepCount) { yield return(a[i]); } } else { if (settings?.ErrorWhenNoMatch ?? false) { throw new JsonException("Array slice of {0} to {1} returned no results.".FormatWith(CultureInfo.InvariantCulture, Start != null ? Start.GetValueOrDefault().ToString(CultureInfo.InvariantCulture) : "*", End != null ? End.GetValueOrDefault().ToString(CultureInfo.InvariantCulture) : "*")); } } } else { if (settings?.ErrorWhenNoMatch ?? false) { throw new JsonException("Array slice is not valid on {0}.".FormatWith(CultureInfo.InvariantCulture, t.GetType().Name)); } } } }
public abstract bool IsMatch(JToken root, JToken t, JsonSelectSettings?settings);
public abstract IEnumerable <JToken> ExecuteFilter(JToken root, IEnumerable <JToken> current, JsonSelectSettings?settings);
private bool MatchTokens(JToken leftResult, JToken rightResult, JsonSelectSettings?settings) { if (leftResult is JValue leftValue && rightResult is JValue rightValue) { switch (Operator) { case QueryOperator.RegexEquals: if (RegexEquals(leftValue, rightValue, settings)) { return(true); } break; case QueryOperator.Equals: if (EqualsWithStringCoercion(leftValue, rightValue)) { return(true); } break; case QueryOperator.StrictEquals: if (EqualsWithStrictMatch(leftValue, rightValue)) { return(true); } break; case QueryOperator.NotEquals: if (!EqualsWithStringCoercion(leftValue, rightValue)) { return(true); } break; case QueryOperator.StrictNotEquals: if (!EqualsWithStrictMatch(leftValue, rightValue)) { return(true); } break; case QueryOperator.GreaterThan: if (leftValue.CompareTo(rightValue) > 0) { return(true); } break; case QueryOperator.GreaterThanOrEquals: if (leftValue.CompareTo(rightValue) >= 0) { return(true); } break; case QueryOperator.LessThan: if (leftValue.CompareTo(rightValue) < 0) { return(true); } break; case QueryOperator.LessThanOrEquals: if (leftValue.CompareTo(rightValue) <= 0) { return(true); } break; case QueryOperator.Exists: return(true); } }
internal IEnumerable <JToken> Evaluate(JToken root, JToken t, JsonSelectSettings?settings) { return(Evaluate(Filters, root, t, settings)); }