diff --git a/src/JsonDocumentPath.Test/JPathExecuteTests.cs b/src/JsonDocumentPath.Test/JPathExecuteTests.cs index 484fe8e..b68a5ea 100644 --- a/src/JsonDocumentPath.Test/JPathExecuteTests.cs +++ b/src/JsonDocumentPath.Test/JPathExecuteTests.cs @@ -74,6 +74,31 @@ public void GreaterThanWithStringParameterAndIntegerValue() Assert.Equal(1, results.Count); } + [Fact] + public void VerifyNameAndValue() + { + string json = @"{ + ""persons"": [ + { + ""name"" : ""John"", + ""age"": 26 + }, + { + ""name"" : ""Jane"", + ""age"": 2 + } + ] + }"; + + var models = JsonDocument.Parse(json).RootElement; + + var results = models.SelectExtElements("persons[*].name").ToList(); + + Assert.Equal(2, results.Count); + Assert.Equal("John", results[0].Element.ToString()); + Assert.Equal("name", results[0].Name); + } + [Fact] public void RecursiveWildcard() { @@ -572,7 +597,7 @@ public void EvaluateWildcardIndexOnObjectWithError() }"; var document = JsonDocument.Parse(json).RootElement; - ExceptionAssert.Throws(() => { document.SelectElement("[*]", true); }, @"Index * not valid on JsonElement."); + ExceptionAssert.Throws(() => { document.SelectElement("[*]", true); }, @"Index * not valid on JsonElementExt."); } [Fact] @@ -583,7 +608,7 @@ public void EvaluateSliceOnObjectWithError() }"; var document = JsonDocument.Parse(json).RootElement; - ExceptionAssert.Throws(() => { document.SelectElement("[:]", true); }, @"Array slice is not valid on JsonElement."); + ExceptionAssert.Throws(() => { document.SelectElement("[:]", true); }, @"Array slice is not valid on JsonElementExt."); } [Fact] @@ -610,7 +635,7 @@ public void EvaluatePropertyOnArrayWithError() string json = @"[1,2,3,4,5]"; var document = JsonDocument.Parse(json).RootElement; - ExceptionAssert.Throws(() => { document.SelectElement("BlahBlah", true); }, @"Property 'BlahBlah' not valid on JsonElement."); + ExceptionAssert.Throws(() => { document.SelectElement("BlahBlah", true); }, @"Property 'BlahBlah' not valid on JsonElementExt."); } [Fact] @@ -661,7 +686,7 @@ public void EvaluateMultiPropertyIndexOnArrayWithError() { var a = JsonDocument.Parse("[1,2,3,4,5]").RootElement; - ExceptionAssert.Throws(() => { a.SelectElement("['Missing','Missing2']", true); }, "Properties 'Missing', 'Missing2' not valid on JsonElement."); + ExceptionAssert.Throws(() => { a.SelectElement("['Missing','Missing2']", true); }, "Properties 'Missing', 'Missing2' not valid on JsonElementExt."); } [Fact] diff --git a/src/JsonDocumentPath/Filters/ArrayIndexFilter.cs b/src/JsonDocumentPath/Filters/ArrayIndexFilter.cs index c48a64a..0c016a2 100644 --- a/src/JsonDocumentPath/Filters/ArrayIndexFilter.cs +++ b/src/JsonDocumentPath/Filters/ArrayIndexFilter.cs @@ -6,13 +6,13 @@ internal class ArrayIndexFilter : PathFilter { public int? Index { get; set; } - public override IEnumerable ExecuteFilter(JsonElement root, IEnumerable current, bool errorWhenNoMatch) + public override IEnumerable ExecuteFilter(JsonElement root, IEnumerable current, bool errorWhenNoMatch) { - foreach (JsonElement t in current) + foreach (JsonElementExt t in current) { if (Index != null) { - JsonElement? v = GetTokenIndex(t, errorWhenNoMatch, Index.GetValueOrDefault()); + JsonElementExt v = GetTokenIndex(t.Element.Value, errorWhenNoMatch, Index.GetValueOrDefault()); if (v != null) { @@ -21,11 +21,11 @@ internal class ArrayIndexFilter : PathFilter } else { - if (t.ValueKind == JsonValueKind.Array) + if (t.Element?.ValueKind == JsonValueKind.Array) { - foreach (JsonElement v in t.EnumerateArray()) + foreach (JsonElement v in t.Element.Value.EnumerateArray()) { - yield return v; + yield return new JsonElementExt{ Element = v }; } } else diff --git a/src/JsonDocumentPath/Filters/ArrayMultipleIndexFilter.cs b/src/JsonDocumentPath/Filters/ArrayMultipleIndexFilter.cs index bbcf52c..4499500 100644 --- a/src/JsonDocumentPath/Filters/ArrayMultipleIndexFilter.cs +++ b/src/JsonDocumentPath/Filters/ArrayMultipleIndexFilter.cs @@ -11,13 +11,13 @@ public ArrayMultipleIndexFilter(List indexes) Indexes = indexes; } - public override IEnumerable ExecuteFilter(JsonElement root, IEnumerable current, bool errorWhenNoMatch) + public override IEnumerable ExecuteFilter(JsonElement root, IEnumerable current, bool errorWhenNoMatch) { - foreach (JsonElement t in current) + foreach (JsonElementExt t in current) { foreach (int i in Indexes) { - JsonElement? v = GetTokenIndex(t, errorWhenNoMatch, i); + JsonElementExt v = GetTokenIndex(t.Element.Value, errorWhenNoMatch, i); if (v != null) { diff --git a/src/JsonDocumentPath/Filters/ArraySliceFilter.cs b/src/JsonDocumentPath/Filters/ArraySliceFilter.cs index 3012c59..43cc56b 100644 --- a/src/JsonDocumentPath/Filters/ArraySliceFilter.cs +++ b/src/JsonDocumentPath/Filters/ArraySliceFilter.cs @@ -8,18 +8,18 @@ internal class ArraySliceFilter : PathFilter public int? End { get; set; } public int? Step { get; set; } - public override IEnumerable ExecuteFilter(JsonElement root, IEnumerable current, bool errorWhenNoMatch) + public override IEnumerable ExecuteFilter(JsonElement root, IEnumerable current, bool errorWhenNoMatch) { if (Step == 0) { throw new JsonException("Step cannot be zero."); } - foreach (JsonElement t in current) + foreach (JsonElementExt t in current) { - if (t.ValueKind == JsonValueKind.Array) + if (t.Element.Value.ValueKind == JsonValueKind.Array) { - var aCount = t.GetArrayLength(); + var aCount = t.Element.Value.GetArrayLength(); // set defaults for null arguments int stepCount = Step ?? 1; int startIndex = Start ?? ((stepCount > 0) ? 0 : aCount - 1); @@ -49,7 +49,7 @@ internal class ArraySliceFilter : PathFilter { for (int i = startIndex; IsValid(i, stopIndex, positiveStep); i += stepCount) { - yield return t[i]; + yield return new JsonElementExt(){ Element = t.Element.Value[i] }; } } else diff --git a/src/JsonDocumentPath/Filters/FieldFilter.cs b/src/JsonDocumentPath/Filters/FieldFilter.cs index 1087b98..bccaa75 100644 --- a/src/JsonDocumentPath/Filters/FieldFilter.cs +++ b/src/JsonDocumentPath/Filters/FieldFilter.cs @@ -4,26 +4,26 @@ namespace System.Text.Json { internal class FieldFilter : PathFilter { - internal string? Name; + internal string Name; - public FieldFilter(string? name) + public FieldFilter(string name) { Name = name; } - public override IEnumerable ExecuteFilter(JsonElement root, IEnumerable current, bool errorWhenNoMatch) + public override IEnumerable ExecuteFilter(JsonElement root, IEnumerable current, bool errorWhenNoMatch) { - foreach (JsonElement t in current) + foreach (JsonElementExt t in current) { - if (t.ValueKind == JsonValueKind.Object) + if (t.Element.Value.ValueKind == JsonValueKind.Object) { if (Name != null) { - if (t.TryGetProperty(Name, out JsonElement v)) + if (t.Element.Value.TryGetProperty(Name, out JsonElement v)) { if (v.ValueKind != JsonValueKind.Null) { - yield return v; + yield return new JsonElementExt(){Element = v, Name =Name }; } else if (errorWhenNoMatch) { @@ -33,9 +33,9 @@ public FieldFilter(string? name) } else { - foreach (var p in t.ChildrenTokens()) + foreach (var p in t.Element.Value.ChildrenTokens()) { - yield return p; + yield return new JsonElementExt() {Element = p}; } } } diff --git a/src/JsonDocumentPath/Filters/FieldMultipleFilter.cs b/src/JsonDocumentPath/Filters/FieldMultipleFilter.cs index 538b060..46f9b02 100644 --- a/src/JsonDocumentPath/Filters/FieldMultipleFilter.cs +++ b/src/JsonDocumentPath/Filters/FieldMultipleFilter.cs @@ -12,19 +12,19 @@ public FieldMultipleFilter(List names) Names = names; } - public override IEnumerable ExecuteFilter(JsonElement root, IEnumerable current, bool errorWhenNoMatch) + public override IEnumerable ExecuteFilter(JsonElement root, IEnumerable current, bool errorWhenNoMatch) { - foreach (JsonElement t in current) + foreach (JsonElementExt t in current) { - if (t.ValueKind == JsonValueKind.Object) + if (t.Element.Value.ValueKind == JsonValueKind.Object) { foreach (string name in Names) { - if (t.TryGetProperty(name, out JsonElement v)) + if (t.Element.Value.TryGetProperty(name, out JsonElement v)) { if (v.ValueKind != JsonValueKind.Null) { - yield return v; + yield return new JsonElementExt(){ Element = v, Name = name }; } else if (errorWhenNoMatch) { diff --git a/src/JsonDocumentPath/Filters/QueryFilter.cs b/src/JsonDocumentPath/Filters/QueryFilter.cs index 2b05f4f..a6be981 100644 --- a/src/JsonDocumentPath/Filters/QueryFilter.cs +++ b/src/JsonDocumentPath/Filters/QueryFilter.cs @@ -11,27 +11,27 @@ public QueryFilter(QueryExpression expression) Expression = expression; } - public override IEnumerable ExecuteFilter(JsonElement root, IEnumerable current, bool errorWhenNoMatch) + public override IEnumerable ExecuteFilter(JsonElement root, IEnumerable current, bool errorWhenNoMatch) { - foreach (JsonElement el in current) + foreach (JsonElementExt el in current) { - if (el.ValueKind == JsonValueKind.Array) + if (el.Element.Value.ValueKind == JsonValueKind.Array) { - foreach (JsonElement v in el.EnumerateArray()) + foreach (JsonElement v in el.Element.Value.EnumerateArray()) { if (Expression.IsMatch(root, v)) { - yield return v; + yield return new JsonElementExt(){ Element = v }; } } } - else if (el.ValueKind == JsonValueKind.Object) + else if (el.Element.Value.ValueKind == JsonValueKind.Object) { - foreach (JsonProperty v in el.EnumerateObject()) + foreach (JsonProperty v in el.Element.Value.EnumerateObject()) { if (Expression.IsMatch(root, v.Value)) { - yield return v.Value; + yield return new JsonElementExt(){ Element = v.Value, Name = v.Name }; } } } diff --git a/src/JsonDocumentPath/Filters/QueryScanFilter.cs b/src/JsonDocumentPath/Filters/QueryScanFilter.cs index 810f5db..564a25e 100644 --- a/src/JsonDocumentPath/Filters/QueryScanFilter.cs +++ b/src/JsonDocumentPath/Filters/QueryScanFilter.cs @@ -11,15 +11,15 @@ public QueryScanFilter(QueryExpression expression) Expression = expression; } - public override IEnumerable ExecuteFilter(JsonElement root, IEnumerable current, bool errorWhenNoMatch) + public override IEnumerable ExecuteFilter(JsonElement root, IEnumerable current, bool errorWhenNoMatch) { - foreach (JsonElement t in current) + foreach (JsonElementExt t in current) { - foreach (var (_, Value) in GetNextScanValue(t)) + foreach (var (name, v) in GetNextScanValue(t)) { - if (Expression.IsMatch(root, Value)) + if (Expression.IsMatch(root, v.Element.Value)) { - yield return Value; + yield return new JsonElementExt(){ Element = v.Element, Name = name }; } } } diff --git a/src/JsonDocumentPath/Filters/RootFilter.cs b/src/JsonDocumentPath/Filters/RootFilter.cs index f2a37ae..40a0b41 100644 --- a/src/JsonDocumentPath/Filters/RootFilter.cs +++ b/src/JsonDocumentPath/Filters/RootFilter.cs @@ -10,9 +10,15 @@ private RootFilter() { } - public override IEnumerable ExecuteFilter(JsonElement root, IEnumerable current, bool errorWhenNoMatch) + public override IEnumerable ExecuteFilter(JsonElement root, IEnumerable current, bool errorWhenNoMatch) { - return new JsonElement?[1] { root }; + return new List() + { + new JsonElementExt() + { + Element = root + } + }; } } } diff --git a/src/JsonDocumentPath/Filters/ScanFilter.cs b/src/JsonDocumentPath/Filters/ScanFilter.cs index 3b75ef7..fe173e6 100644 --- a/src/JsonDocumentPath/Filters/ScanFilter.cs +++ b/src/JsonDocumentPath/Filters/ScanFilter.cs @@ -12,9 +12,9 @@ public ScanFilter(string? name) Name = name; } - public override IEnumerable ExecuteFilter(JsonElement root, IEnumerable current, bool errorWhenNoMatch) + public override IEnumerable ExecuteFilter(JsonElement root, IEnumerable current, bool errorWhenNoMatch) { - foreach (JsonElement c in current) + foreach (JsonElementExt c in current) { foreach (var e in GetNextScanValue(c)) { diff --git a/src/JsonDocumentPath/Filters/ScanMultipleFilter.cs b/src/JsonDocumentPath/Filters/ScanMultipleFilter.cs index dc7d35a..d8fc7f0 100644 --- a/src/JsonDocumentPath/Filters/ScanMultipleFilter.cs +++ b/src/JsonDocumentPath/Filters/ScanMultipleFilter.cs @@ -11,12 +11,10 @@ public ScanMultipleFilter(List names) _names = names; } - public override IEnumerable ExecuteFilter(JsonElement root, IEnumerable current, bool errorWhenNoMatch) + public override IEnumerable ExecuteFilter(JsonElement root, IEnumerable current, bool errorWhenNoMatch) { - foreach (JsonElement c in current) + foreach (JsonElementExt c in current) { - JsonElement? value = c; - foreach (var e in GetNextScanValue(c)) { if (e.Name != null) @@ -25,7 +23,7 @@ public ScanMultipleFilter(List names) { if (e.Name == name) { - yield return e.Value; + yield return new JsonElementExt(){ Element = e.Value.Element, Name = e.Name }; } } } diff --git a/src/JsonDocumentPath/JsonDocumentPath.cs b/src/JsonDocumentPath/JsonDocumentPath.cs index 27f8b7c..216a646 100644 --- a/src/JsonDocumentPath/JsonDocumentPath.cs +++ b/src/JsonDocumentPath/JsonDocumentPath.cs @@ -897,14 +897,15 @@ private void EnsureLength(string message) } } - internal IEnumerable Evaluate(JsonElement root, JsonElement t, bool errorWhenNoMatch) + internal IEnumerable Evaluate(JsonElement root, JsonElement t, bool errorWhenNoMatch) { return Evaluate(Filters, root, t, errorWhenNoMatch); } - internal static IEnumerable Evaluate(List filters, JsonElement root, JsonElement t, bool errorWhenNoMatch) + internal static IEnumerable Evaluate(List filters, JsonElement root, JsonElement t, bool errorWhenNoMatch) { - IEnumerable current = new JsonElement?[] { t }; + IEnumerable current = new [] { new JsonElementExt{ Element = t }}; + foreach (PathFilter filter in filters) { current = filter.ExecuteFilter(root, current, errorWhenNoMatch); diff --git a/src/JsonDocumentPath/JsonDocumentPathExtensions.cs b/src/JsonDocumentPath/JsonDocumentPathExtensions.cs index 9d93404..020454a 100644 --- a/src/JsonDocumentPath/JsonDocumentPathExtensions.cs +++ b/src/JsonDocumentPath/JsonDocumentPathExtensions.cs @@ -1,4 +1,5 @@ using System.Collections.Generic; +using System.Linq; namespace System.Text.Json { @@ -17,6 +18,19 @@ public static class JsonDocumentPathExtensions return SelectElements(src.RootElement, path, false); } + /// + /// Selects a collection of elements using a JSONPath expression. + /// + /// + /// A that contains a JSONPath expression. + /// + /// An of that contains the selected elements. + + public static IEnumerable SelectExtElements(this JsonDocument src, string path) + { + return SelectExtElements(src.RootElement, path, false); + } + /// /// Selects a collection of elements using a JSONPath expression. /// @@ -30,6 +44,19 @@ public static class JsonDocumentPathExtensions return SelectElements(src, path, false); } + /// + /// Selects a collection of elements using a JSONPath expression. + /// + /// + /// A that contains a JSONPath expression. + /// + /// An of that contains the selected elements. + + public static IEnumerable SelectExtElements(this JsonElement src, string path) + { + return SelectExtElements(src, path, false); + } + /// /// Selects a collection of elements using a JSONPath expression. /// @@ -39,6 +66,20 @@ public static class JsonDocumentPathExtensions /// A flag to indicate whether an error should be thrown if no tokens are found when evaluating part of the expression. /// An of that contains the selected elements. public static IEnumerable SelectElements(this JsonElement src, string path, bool errorWhenNoMatch) + { + var parser = new JsonDocumentPath(path); + return parser.Evaluate(src, src, errorWhenNoMatch).Select(x=>x.Element); + } + + /// + /// Selects a collection of elements using a JSONPath expression. + /// + /// + /// A that contains a JSONPath expression. + /// + /// A flag to indicate whether an error should be thrown if no tokens are found when evaluating part of the expression. + /// An of that contains the selected elements. + public static IEnumerable SelectExtElements(this JsonElement src, string path, bool errorWhenNoMatch) { var parser = new JsonDocumentPath(path); return parser.Evaluate(src, src, errorWhenNoMatch); @@ -53,6 +94,20 @@ public static class JsonDocumentPathExtensions /// A flag to indicate whether an error should be thrown if no tokens are found when evaluating part of the expression. /// An of that contains the selected elements. public static IEnumerable SelectElements(this JsonDocument src, string path, bool errorWhenNoMatch) + { + var parser = new JsonDocumentPath(path); + return parser.Evaluate(src.RootElement, src.RootElement, errorWhenNoMatch).Select(x=>x.Element); + } + + /// + /// Selects a collection of elements using a JSONPath expression. + /// + /// + /// A that contains a JSONPath expression. + /// + /// A flag to indicate whether an error should be thrown if no tokens are found when evaluating part of the expression. + /// An of that contains the selected elements. + public static IEnumerable SelectExtElements(this JsonDocument src, string path, bool errorWhenNoMatch) { var parser = new JsonDocumentPath(path); return parser.Evaluate(src.RootElement, src.RootElement, errorWhenNoMatch); @@ -70,6 +125,18 @@ public static class JsonDocumentPathExtensions return SelectElement(src.RootElement, path, false); } + /// + /// Selects a using a JSONPath expression. Selects the token that matches the object path. + /// + /// + /// A that contains a JSONPath expression. + /// + /// A , or null. + public static JsonElementExt SelectExtElement(this JsonDocument src, string path) + { + return SelectExtElement(src.RootElement, path, false); + } + /// /// Selects a using a JSONPath expression. Selects the token that matches the object path. /// @@ -82,6 +149,18 @@ public static class JsonDocumentPathExtensions return SelectElement(src, path, false); } + /// + /// Selects a using a JSONPath expression. Selects the token that matches the object path. + /// + /// + /// A that contains a JSONPath expression. + /// + /// A , or null. + public static JsonElementExt SelectExtElement(this JsonElement src, string path) + { + return SelectExtElement(src, path, false); + } + /// /// Selects a using a JSONPath expression. Selects the token that matches the object path. /// @@ -93,8 +172,31 @@ public static class JsonDocumentPathExtensions public static JsonElement? SelectElement(this JsonDocument src, string path, bool errorWhenNoMatch) { var p = new JsonDocumentPath(path); - JsonElement? el = null; - foreach (JsonElement t in p.Evaluate(src.RootElement, src.RootElement, errorWhenNoMatch)) + JsonElementExt el = null; + foreach (JsonElementExt t in p.Evaluate(src.RootElement, src.RootElement, errorWhenNoMatch)) + { + if (el != null) + { + throw new JsonException("Path returned multiple tokens."); + } + el = t; + } + return el.Element; + } + + /// + /// Selects a using a JSONPath expression. Selects the token that matches the object path. + /// + /// + /// A that contains a JSONPath expression. + /// + /// A flag to indicate whether an error should be thrown if no tokens are found when evaluating part of the expression. + /// A . + public static JsonElementExt SelectExtElement(this JsonDocument src, string path, bool errorWhenNoMatch) + { + var p = new JsonDocumentPath(path); + JsonElementExt el = null; + foreach (JsonElementExt t in p.Evaluate(src.RootElement, src.RootElement, errorWhenNoMatch)) { if (el != null) { @@ -116,8 +218,31 @@ public static class JsonDocumentPathExtensions public static JsonElement? SelectElement(this JsonElement src, string path, bool errorWhenNoMatch) { var p = new JsonDocumentPath(path); - JsonElement? el = null; - foreach (JsonElement t in p.Evaluate(src, src, errorWhenNoMatch)) + JsonElementExt el = null; + foreach (JsonElementExt t in p.Evaluate(src, src, errorWhenNoMatch)) + { + if (el != null) + { + throw new JsonException("Path returned multiple tokens."); + } + el = t; + } + return el?.Element; + } + + /// + /// Selects a using a JSONPath expression. Selects the token that matches the object path. + /// + /// + /// A that contains a JSONPath expression. + /// + /// A flag to indicate whether an error should be thrown if no tokens are found when evaluating part of the expression. + /// A . + public static JsonElementExt SelectExtElement(this JsonElement src, string path, bool errorWhenNoMatch) + { + var p = new JsonDocumentPath(path); + JsonElementExt el = null; + foreach (JsonElementExt t in p.Evaluate(src, src, errorWhenNoMatch)) { if (el != null) { diff --git a/src/JsonDocumentPath/JsonElementExt.cs b/src/JsonDocumentPath/JsonElementExt.cs new file mode 100644 index 0000000..dd277ba --- /dev/null +++ b/src/JsonDocumentPath/JsonElementExt.cs @@ -0,0 +1,9 @@ +namespace System.Text.Json +{ + public class JsonElementExt + { + public JsonElement? Element { get; set; } + + public string Name { get; set; } + } +} diff --git a/src/JsonDocumentPath/PathFilter.cs b/src/JsonDocumentPath/PathFilter.cs index bc804a5..fbdedb4 100644 --- a/src/JsonDocumentPath/PathFilter.cs +++ b/src/JsonDocumentPath/PathFilter.cs @@ -4,9 +4,9 @@ namespace System.Text.Json { public abstract class PathFilter { - public abstract IEnumerable ExecuteFilter(JsonElement root, IEnumerable current, bool errorWhenNoMatch); + public abstract IEnumerable ExecuteFilter(JsonElement root, IEnumerable current, bool errorWhenNoMatch); - protected static JsonElement? GetTokenIndex(JsonElement t, bool errorWhenNoMatch, int index) + protected static JsonElementExt GetTokenIndex(JsonElement t, bool errorWhenNoMatch, int index) { if (t.ValueKind == JsonValueKind.Array) { @@ -20,7 +20,7 @@ public abstract class PathFilter return null; } - return t[index]; + return new JsonElementExt() {Element = t[index]}; } else { @@ -33,27 +33,27 @@ public abstract class PathFilter } } - protected static IEnumerable<(string Name, JsonElement Value)> GetNextScanValue(JsonElement value) + protected static IEnumerable<(string Name, JsonElementExt Value)> GetNextScanValue(JsonElementExt value) { yield return (null, value); - if (value.ValueKind == JsonValueKind.Array) + if (value.Element?.ValueKind == JsonValueKind.Array) { - foreach (var e in value.EnumerateArray()) + foreach (var e in value.Element?.EnumerateArray()) { - foreach (var c in GetNextScanValue(e)) + foreach (var c in GetNextScanValue(new JsonElementExt(){ Element = e })) { yield return c; } } } - else if (value.ValueKind == JsonValueKind.Object) + else if (value.Element?.ValueKind == JsonValueKind.Object) { - foreach (var e in value.EnumerateObject()) + foreach (var e in value.Element?.EnumerateObject()) { - yield return (e.Name, e.Value); + yield return (e.Name, new JsonElementExt{Element = e.Value, Name = e.Name }); - foreach (var c in GetNextScanValue(e.Value)) + foreach (var c in GetNextScanValue(new JsonElementExt(){ Element = e.Value })) { yield return c; } diff --git a/src/JsonDocumentPath/QueryExpression.cs b/src/JsonDocumentPath/QueryExpression.cs index ba7a6ef..a6e2ccc 100644 --- a/src/JsonDocumentPath/QueryExpression.cs +++ b/src/JsonDocumentPath/QueryExpression.cs @@ -81,11 +81,11 @@ public BooleanQueryExpression(QueryOperator @operator, object left, object? righ Right = right; } - private IEnumerable GetResult(JsonElement root, JsonElement t, object? o) + private IEnumerable GetResult(JsonElement root, JsonElement t, object? o) { if (o is JsonElement resultToken) { - return new JsonElement?[1] { resultToken }; + return new JsonElementExt[1] { new JsonElementExt(){ Element=resultToken } }; } if (o is List pathFilters) @@ -93,7 +93,7 @@ public BooleanQueryExpression(QueryOperator @operator, object left, object? righ return JsonDocumentPath.Evaluate(pathFilters, root, t, false); } - return Enumerable.Empty(); + return Enumerable.Empty(); } public override bool IsMatch(JsonElement root, JsonElement t) @@ -103,19 +103,19 @@ public override bool IsMatch(JsonElement root, JsonElement t) return GetResult(root, t, Left).Any(); } - using (IEnumerator leftResults = GetResult(root, t, Left).GetEnumerator()) + using (IEnumerator leftResults = GetResult(root, t, Left).GetEnumerator()) { if (leftResults.MoveNext()) { - IEnumerable rightResultsEn = GetResult(root, t, Right); - ICollection rightResults = rightResultsEn as ICollection ?? rightResultsEn.ToList(); + IEnumerable rightResultsEn = GetResult(root, t, Right); + ICollection rightResults = rightResultsEn as ICollection ?? rightResultsEn.ToList(); do { - JsonElement leftResult = leftResults.Current.Value; - foreach (JsonElement rightResult in rightResults) + JsonElement? leftResult = leftResults.Current.Element; + foreach (JsonElementExt rightResult in rightResults) { - if (MatchTokens(leftResult, rightResult)) + if (MatchTokens(leftResult.Value, rightResult.Element.Value)) { return true; }