-JsonPath++
This commit is contained in:
@@ -0,0 +1,23 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Newtonsoft.Json.Linq.JsonPath
|
||||
{
|
||||
internal class AndFilter : PathFilter
|
||||
{
|
||||
public List<PathFilter> Filters { get; set; }
|
||||
|
||||
public override IEnumerable<JToken> ExecuteFilter(IEnumerable<JToken> current, bool errorWhenNoMatch)
|
||||
{
|
||||
foreach (PathFilter f in Filters)
|
||||
{
|
||||
current = f.ExecuteFilter(current, errorWhenNoMatch).ToList();
|
||||
}
|
||||
|
||||
return current;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -85,6 +85,7 @@ namespace Newtonsoft.Json.Linq.JsonPath
|
||||
{
|
||||
bool scan = false;
|
||||
bool followingIndexer = false;
|
||||
bool followingDot = false;
|
||||
|
||||
bool ended = false;
|
||||
while (_currentIndex < _expression.Length && !ended)
|
||||
@@ -107,6 +108,7 @@ namespace Newtonsoft.Json.Linq.JsonPath
|
||||
_currentIndex++;
|
||||
currentPartStartIndex = _currentIndex;
|
||||
followingIndexer = true;
|
||||
followingDot = false;
|
||||
break;
|
||||
case ']':
|
||||
case ')':
|
||||
@@ -135,9 +137,10 @@ namespace Newtonsoft.Json.Linq.JsonPath
|
||||
_currentIndex++;
|
||||
currentPartStartIndex = _currentIndex;
|
||||
followingIndexer = false;
|
||||
followingDot = true;
|
||||
break;
|
||||
default:
|
||||
if (query && (currentChar == '=' || currentChar == '<' || currentChar == '!' || currentChar == '>'))
|
||||
if (query && (currentChar == '=' || currentChar == '<' || currentChar == '!' || currentChar == '>' || currentChar == '|' || currentChar == '&'))
|
||||
{
|
||||
ended = true;
|
||||
}
|
||||
@@ -152,6 +155,8 @@ namespace Newtonsoft.Json.Linq.JsonPath
|
||||
}
|
||||
}
|
||||
|
||||
bool atPathEnd = (_currentIndex == _expression.Length);
|
||||
|
||||
if (_currentIndex > currentPartStartIndex)
|
||||
{
|
||||
string member = _expression.Substring(currentPartStartIndex, _currentIndex - currentPartStartIndex).TrimEnd();
|
||||
@@ -160,8 +165,14 @@ namespace Newtonsoft.Json.Linq.JsonPath
|
||||
PathFilter filter = (scan) ? (PathFilter)new ScanFilter() { Name = member } : new FieldFilter() { Name = member };
|
||||
filters.Add(filter);
|
||||
}
|
||||
else
|
||||
{
|
||||
// no field name following dot in path and at end of base path/query
|
||||
if (followingDot && (atPathEnd || query))
|
||||
throw new JsonException("Unexpected end while parsing path.");
|
||||
}
|
||||
|
||||
return (_currentIndex == _expression.Length);
|
||||
return atPathEnd;
|
||||
}
|
||||
|
||||
private PathFilter ParseIndexer(char indexerOpenChar)
|
||||
@@ -344,75 +355,118 @@ namespace Newtonsoft.Json.Linq.JsonPath
|
||||
|
||||
_currentIndex++;
|
||||
|
||||
List<QueryExpression> expressions = new List<QueryExpression>();
|
||||
QueryExpression expression = ParseExpression();
|
||||
|
||||
while (true)
|
||||
_currentIndex++;
|
||||
EnsureLength("Path ended with open indexer.");
|
||||
EatWhitespace();
|
||||
|
||||
if (_expression[_currentIndex] != indexerCloseChar)
|
||||
throw new JsonException("Unexpected character while parsing path indexer: " + _expression[_currentIndex]);
|
||||
|
||||
return new QueryFilter
|
||||
{
|
||||
expressions.Add(ParseExpression());
|
||||
|
||||
if (_expression[_currentIndex] == ')')
|
||||
{
|
||||
_currentIndex++;
|
||||
EnsureLength("Path ended with open indexer.");
|
||||
EatWhitespace();
|
||||
|
||||
if (_expression[_currentIndex] != indexerCloseChar)
|
||||
throw new JsonException("Unexpected character while parsing path indexer: " + _expression[_currentIndex]);
|
||||
|
||||
return new QueryFilter
|
||||
{
|
||||
Expression = expressions
|
||||
};
|
||||
}
|
||||
else
|
||||
{
|
||||
_currentIndex++;
|
||||
}
|
||||
}
|
||||
Expression = expression
|
||||
};
|
||||
}
|
||||
|
||||
private QueryExpression ParseExpression()
|
||||
{
|
||||
EatWhitespace();
|
||||
QueryExpression rootExpression = null;
|
||||
CompositeExpression parentExpression = null;
|
||||
|
||||
if (_expression[_currentIndex] != '@')
|
||||
throw new JsonException("Unexpected character while parsing path query: " + _expression[_currentIndex]);
|
||||
|
||||
_currentIndex++;
|
||||
|
||||
List<PathFilter> expressionPath = new List<PathFilter>();
|
||||
|
||||
if (ParsePath(expressionPath, _currentIndex, true))
|
||||
throw new JsonException("Path ended with open query.");
|
||||
|
||||
EatWhitespace();
|
||||
EnsureLength("Path ended with open query.");
|
||||
|
||||
QueryOperator op;
|
||||
object value = null;
|
||||
if (_expression[_currentIndex] == ')')
|
||||
while (_currentIndex < _expression.Length)
|
||||
{
|
||||
op = QueryOperator.Exists;
|
||||
}
|
||||
else
|
||||
{
|
||||
op = ParseOperator();
|
||||
EatWhitespace();
|
||||
|
||||
if (_expression[_currentIndex] != '@')
|
||||
throw new JsonException("Unexpected character while parsing path query: " + _expression[_currentIndex]);
|
||||
|
||||
_currentIndex++;
|
||||
|
||||
List<PathFilter> expressionPath = new List<PathFilter>();
|
||||
|
||||
if (ParsePath(expressionPath, _currentIndex, true))
|
||||
throw new JsonException("Path ended with open query.");
|
||||
|
||||
EatWhitespace();
|
||||
EnsureLength("Path ended with open query.");
|
||||
|
||||
value = ParseValue();
|
||||
QueryOperator op;
|
||||
object value = null;
|
||||
if (_expression[_currentIndex] == ')'
|
||||
|| _expression[_currentIndex] == '|'
|
||||
|| _expression[_currentIndex] == '&')
|
||||
{
|
||||
op = QueryOperator.Exists;
|
||||
}
|
||||
else
|
||||
{
|
||||
op = ParseOperator();
|
||||
|
||||
EatWhitespace();
|
||||
EnsureLength("Path ended with open query.");
|
||||
EatWhitespace();
|
||||
EnsureLength("Path ended with open query.");
|
||||
|
||||
value = ParseValue();
|
||||
|
||||
EatWhitespace();
|
||||
EnsureLength("Path ended with open query.");
|
||||
}
|
||||
|
||||
BooleanQueryExpression booleanExpression = new BooleanQueryExpression
|
||||
{
|
||||
Path = expressionPath,
|
||||
Operator = op,
|
||||
Value = (op != QueryOperator.Exists) ? new JValue(value) : null
|
||||
};
|
||||
|
||||
if (_expression[_currentIndex] == ')')
|
||||
{
|
||||
if (parentExpression != null)
|
||||
{
|
||||
parentExpression.Expressions.Add(booleanExpression);
|
||||
return rootExpression;
|
||||
}
|
||||
|
||||
return booleanExpression;
|
||||
}
|
||||
if (_expression[_currentIndex] == '&' && Match("&&"))
|
||||
{
|
||||
if (parentExpression == null || parentExpression.Operator != QueryOperator.And)
|
||||
{
|
||||
CompositeExpression andExpression = new CompositeExpression { Operator = QueryOperator.And };
|
||||
|
||||
if (parentExpression != null)
|
||||
parentExpression.Expressions.Add(andExpression);
|
||||
|
||||
parentExpression = andExpression;
|
||||
|
||||
if (rootExpression == null)
|
||||
rootExpression = parentExpression;
|
||||
}
|
||||
|
||||
parentExpression.Expressions.Add(booleanExpression);
|
||||
}
|
||||
if (_expression[_currentIndex] == '|' && Match("||"))
|
||||
{
|
||||
if (parentExpression == null || parentExpression.Operator != QueryOperator.Or)
|
||||
{
|
||||
CompositeExpression orExpression = new CompositeExpression { Operator = QueryOperator.Or };
|
||||
|
||||
if (parentExpression != null)
|
||||
parentExpression.Expressions.Add(orExpression);
|
||||
|
||||
parentExpression = orExpression;
|
||||
|
||||
if (rootExpression == null)
|
||||
rootExpression = parentExpression;
|
||||
}
|
||||
|
||||
parentExpression.Expressions.Add(booleanExpression);
|
||||
}
|
||||
}
|
||||
|
||||
return new QueryExpression
|
||||
{
|
||||
Path = expressionPath,
|
||||
Operator = op,
|
||||
Value = (op != QueryOperator.Exists) ? new JValue(value) : null
|
||||
};
|
||||
throw new JsonException("Path ended with open query.");
|
||||
}
|
||||
|
||||
private object ParseValue()
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace Newtonsoft.Json.Linq.JsonPath
|
||||
{
|
||||
internal class OrFilter : PathFilter
|
||||
{
|
||||
public List<PathFilter> Filters { get; set; }
|
||||
|
||||
public override IEnumerable<JToken> ExecuteFilter(IEnumerable<JToken> current, bool errorWhenNoMatch)
|
||||
{
|
||||
List<JToken> cachedCurrent = current.ToList();
|
||||
List<JToken> remaining = cachedCurrent.ToList();
|
||||
List<JToken> matches = new List<JToken>();
|
||||
int previousResultCount = 0;
|
||||
|
||||
foreach (PathFilter f in Filters)
|
||||
{
|
||||
matches.AddRange(f.ExecuteFilter(remaining, errorWhenNoMatch));
|
||||
|
||||
for (int i = previousResultCount; i < matches.Count; i++)
|
||||
{
|
||||
remaining.Remove(matches[i]);
|
||||
}
|
||||
previousResultCount = matches.Count;
|
||||
}
|
||||
|
||||
return cachedCurrent.Where(matches.Contains);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -13,19 +13,103 @@ namespace Newtonsoft.Json.Linq.JsonPath
|
||||
LessThan,
|
||||
LessThanOrEquals,
|
||||
GreaterThan,
|
||||
GreaterThanOrEquals
|
||||
GreaterThanOrEquals,
|
||||
And,
|
||||
Or
|
||||
}
|
||||
|
||||
internal class QueryExpression
|
||||
internal abstract class QueryExpression
|
||||
{
|
||||
public QueryOperator Operator { get; set; }
|
||||
|
||||
public abstract bool IsMatch(JToken t);
|
||||
}
|
||||
|
||||
internal class CompositeExpression : QueryExpression
|
||||
{
|
||||
public List<QueryExpression> Expressions { get; set; }
|
||||
|
||||
public CompositeExpression()
|
||||
{
|
||||
Expressions = new List<QueryExpression>();
|
||||
}
|
||||
|
||||
public override bool IsMatch(JToken t)
|
||||
{
|
||||
switch (Operator)
|
||||
{
|
||||
case QueryOperator.And:
|
||||
foreach (QueryExpression e in Expressions)
|
||||
{
|
||||
if (!e.IsMatch(t))
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
case QueryOperator.Or:
|
||||
foreach (QueryExpression e in Expressions)
|
||||
{
|
||||
if (e.IsMatch(t))
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
default:
|
||||
throw new ArgumentOutOfRangeException();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
internal class BooleanQueryExpression : QueryExpression
|
||||
{
|
||||
public List<PathFilter> Path { get; set; }
|
||||
public QueryOperator Operator { get; set; }
|
||||
public JValue Value { get; set; }
|
||||
|
||||
public override bool IsMatch(JToken t)
|
||||
{
|
||||
IEnumerable<JToken> pathResult = JPath.Evaluate(Path, t, false);
|
||||
|
||||
foreach (JToken r in pathResult)
|
||||
{
|
||||
JValue v = r as JValue;
|
||||
switch (Operator)
|
||||
{
|
||||
case QueryOperator.Equals:
|
||||
if (v != null && v.Equals(Value))
|
||||
return true;
|
||||
break;
|
||||
case QueryOperator.NotEquals:
|
||||
if (v != null && !v.Equals(Value))
|
||||
return true;
|
||||
break;
|
||||
case QueryOperator.GreaterThan:
|
||||
if (v != null && v.CompareTo(Value) > 0)
|
||||
return true;
|
||||
break;
|
||||
case QueryOperator.GreaterThanOrEquals:
|
||||
if (v != null && v.CompareTo(Value) >= 0)
|
||||
return true;
|
||||
break;
|
||||
case QueryOperator.LessThan:
|
||||
if (v != null && v.CompareTo(Value) < 0)
|
||||
return true;
|
||||
break;
|
||||
case QueryOperator.LessThanOrEquals:
|
||||
if (v != null && v.CompareTo(Value) <= 0)
|
||||
return true;
|
||||
break;
|
||||
case QueryOperator.Exists:
|
||||
return true;
|
||||
default:
|
||||
throw new ArgumentOutOfRangeException();
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
internal class QueryFilter : PathFilter
|
||||
{
|
||||
public List<QueryExpression> Expression { get; set; }
|
||||
public QueryExpression Expression { get; set; }
|
||||
|
||||
public override IEnumerable<JToken> ExecuteFilter(IEnumerable<JToken> current, bool errorWhenNoMatch)
|
||||
{
|
||||
@@ -33,56 +117,10 @@ namespace Newtonsoft.Json.Linq.JsonPath
|
||||
{
|
||||
foreach (JToken v in t)
|
||||
{
|
||||
if (Evaluate(errorWhenNoMatch, v))
|
||||
if (Expression.IsMatch(v))
|
||||
yield return v;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private bool Evaluate(bool errorWhenNoMatch, JToken value)
|
||||
{
|
||||
foreach (QueryExpression expression in Expression)
|
||||
{
|
||||
IEnumerable<JToken> pathResult = JPath.Evaluate(expression.Path, value, errorWhenNoMatch);
|
||||
|
||||
foreach (JToken r in pathResult)
|
||||
{
|
||||
JValue v = r as JValue;
|
||||
switch (expression.Operator)
|
||||
{
|
||||
case QueryOperator.Equals:
|
||||
if (v != null && v.Equals(expression.Value))
|
||||
return true;
|
||||
break;
|
||||
case QueryOperator.NotEquals:
|
||||
if (v != null && !v.Equals(expression.Value))
|
||||
return true;
|
||||
break;
|
||||
case QueryOperator.GreaterThan:
|
||||
if (v != null && v.CompareTo(expression.Value) > 0)
|
||||
return true;
|
||||
break;
|
||||
case QueryOperator.GreaterThanOrEquals:
|
||||
if (v != null && v.CompareTo(expression.Value) >= 0)
|
||||
return true;
|
||||
break;
|
||||
case QueryOperator.LessThan:
|
||||
if (v != null && v.CompareTo(expression.Value) < 0)
|
||||
return true;
|
||||
break;
|
||||
case QueryOperator.LessThanOrEquals:
|
||||
if (v != null && v.CompareTo(expression.Value) <= 0)
|
||||
return true;
|
||||
break;
|
||||
case QueryOperator.Exists:
|
||||
return true;
|
||||
default:
|
||||
throw new ArgumentOutOfRangeException();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -111,6 +111,7 @@
|
||||
<Compile Include="Linq\JContainer.cs" />
|
||||
<Compile Include="Linq\JEnumerable.cs" />
|
||||
<Compile Include="Linq\JObject.cs" />
|
||||
<Compile Include="Linq\JsonPath\AndFilter.cs" />
|
||||
<Compile Include="Linq\JsonPath\ArrayIndexFilter.cs" />
|
||||
<Compile Include="Linq\JsonPath\ArrayMultipleIndexFilter.cs" />
|
||||
<Compile Include="Linq\JsonPath\ArraySliceFilter.cs" />
|
||||
@@ -120,6 +121,7 @@
|
||||
<Compile Include="Linq\JPropertyDescriptor.cs" />
|
||||
<Compile Include="Linq\JPropertyKeyedCollection.cs" />
|
||||
<Compile Include="Linq\JRaw.cs" />
|
||||
<Compile Include="Linq\JsonPath\OrFilter.cs" />
|
||||
<Compile Include="Linq\JsonPath\PathFilter.cs" />
|
||||
<Compile Include="Linq\JsonPath\QueryFilter.cs" />
|
||||
<Compile Include="Linq\JsonPath\ScanFilter.cs" />
|
||||
|
||||
Reference in New Issue
Block a user