Skip to content

Commit de68652

Browse files
committed
Undo deleting files for prevent conflicts with master branch
1 parent e2ebc21 commit de68652

File tree

13 files changed

+2904
-0
lines changed

13 files changed

+2904
-0
lines changed

src/Newtonsoft.Json.Tests/Linq/JsonPath/JPathExecuteTests.cs

Lines changed: 860 additions & 0 deletions
Large diffs are not rendered by default.

src/Newtonsoft.Json.Tests/Linq/JsonPath/JPathParseTests.cs

Lines changed: 660 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 198 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,198 @@
1+
#region License
2+
// Copyright (c) 2007 James Newton-King
3+
//
4+
// Permission is hereby granted, free of charge, to any person
5+
// obtaining a copy of this software and associated documentation
6+
// files (the "Software"), to deal in the Software without
7+
// restriction, including without limitation the rights to use,
8+
// copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
// copies of the Software, and to permit persons to whom the
10+
// Software is furnished to do so, subject to the following
11+
// conditions:
12+
//
13+
// The above copyright notice and this permission notice shall be
14+
// included in all copies or substantial portions of the Software.
15+
//
16+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17+
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
18+
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19+
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20+
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21+
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22+
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23+
// OTHER DEALINGS IN THE SOFTWARE.
24+
#endregion
25+
26+
using System;
27+
using System.Collections.Generic;
28+
using System.Text;
29+
using Newtonsoft.Json.Linq;
30+
using Newtonsoft.Json.Linq.JsonPath;
31+
#if NETFX_CORE
32+
using Microsoft.VisualStudio.TestPlatform.UnitTestFramework;
33+
using TestFixture = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestClassAttribute;
34+
using Test = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestMethodAttribute;
35+
#elif DNXCORE50
36+
using Xunit;
37+
using Test = Xunit.FactAttribute;
38+
using Assert = Newtonsoft.Json.Tests.XUnitAssert;
39+
#else
40+
using NUnit.Framework;
41+
#endif
42+
43+
namespace Newtonsoft.Json.Tests.Linq.JsonPath
44+
{
45+
[TestFixture]
46+
public class QueryExpressionTests : TestFixtureBase
47+
{
48+
[Test]
49+
public void AndExpressionTest()
50+
{
51+
CompositeExpression compositeExpression = new CompositeExpression
52+
{
53+
Operator = QueryOperator.And,
54+
Expressions = new List<QueryExpression>
55+
{
56+
new BooleanQueryExpression
57+
{
58+
Operator = QueryOperator.Exists,
59+
Path = new List<PathFilter>
60+
{
61+
new FieldFilter
62+
{
63+
Name = "FirstName"
64+
}
65+
}
66+
},
67+
new BooleanQueryExpression
68+
{
69+
Operator = QueryOperator.Exists,
70+
Path = new List<PathFilter>
71+
{
72+
new FieldFilter
73+
{
74+
Name = "LastName"
75+
}
76+
}
77+
}
78+
}
79+
};
80+
81+
JObject o1 = new JObject
82+
{
83+
{"Title","Title!"},
84+
{"FirstName", "FirstName!"},
85+
{"LastName", "LastName!"}
86+
};
87+
88+
Assert.IsTrue(compositeExpression.IsMatch(o1));
89+
90+
JObject o2 = new JObject
91+
{
92+
{"Title","Title!"},
93+
{"FirstName", "FirstName!"}
94+
};
95+
96+
Assert.IsFalse(compositeExpression.IsMatch(o2));
97+
98+
JObject o3 = new JObject
99+
{
100+
{"Title","Title!"}
101+
};
102+
103+
Assert.IsFalse(compositeExpression.IsMatch(o3));
104+
}
105+
106+
[Test]
107+
public void OrExpressionTest()
108+
{
109+
CompositeExpression compositeExpression = new CompositeExpression
110+
{
111+
Operator = QueryOperator.Or,
112+
Expressions = new List<QueryExpression>
113+
{
114+
new BooleanQueryExpression
115+
{
116+
Operator = QueryOperator.Exists,
117+
Path = new List<PathFilter>
118+
{
119+
new FieldFilter
120+
{
121+
Name = "FirstName"
122+
}
123+
}
124+
},
125+
new BooleanQueryExpression
126+
{
127+
Operator = QueryOperator.Exists,
128+
Path = new List<PathFilter>
129+
{
130+
new FieldFilter
131+
{
132+
Name = "LastName"
133+
}
134+
}
135+
}
136+
}
137+
};
138+
139+
JObject o1 = new JObject
140+
{
141+
{"Title","Title!"},
142+
{"FirstName", "FirstName!"},
143+
{"LastName", "LastName!"}
144+
};
145+
146+
Assert.IsTrue(compositeExpression.IsMatch(o1));
147+
148+
JObject o2 = new JObject
149+
{
150+
{"Title","Title!"},
151+
{"FirstName", "FirstName!"}
152+
};
153+
154+
Assert.IsTrue(compositeExpression.IsMatch(o2));
155+
156+
JObject o3 = new JObject
157+
{
158+
{"Title","Title!"}
159+
};
160+
161+
Assert.IsFalse(compositeExpression.IsMatch(o3));
162+
}
163+
164+
[Test]
165+
public void BooleanExpressionTest()
166+
{
167+
BooleanQueryExpression e1 = new BooleanQueryExpression
168+
{
169+
Operator = QueryOperator.LessThan,
170+
Value = new JValue(3),
171+
Path = new List<PathFilter>
172+
{
173+
new ArrayIndexFilter()
174+
}
175+
};
176+
177+
Assert.IsTrue(e1.IsMatch(new JArray(1, 2, 3, 4, 5)));
178+
Assert.IsTrue(e1.IsMatch(new JArray(2, 3, 4, 5)));
179+
Assert.IsFalse(e1.IsMatch(new JArray(3, 4, 5)));
180+
Assert.IsFalse(e1.IsMatch(new JArray(4, 5)));
181+
182+
BooleanQueryExpression e2 = new BooleanQueryExpression
183+
{
184+
Operator = QueryOperator.LessThanOrEquals,
185+
Value = new JValue(3),
186+
Path = new List<PathFilter>
187+
{
188+
new ArrayIndexFilter()
189+
}
190+
};
191+
192+
Assert.IsTrue(e2.IsMatch(new JArray(1, 2, 3, 4, 5)));
193+
Assert.IsTrue(e2.IsMatch(new JArray(2, 3, 4, 5)));
194+
Assert.IsTrue(e2.IsMatch(new JArray(3, 4, 5)));
195+
Assert.IsFalse(e2.IsMatch(new JArray(4, 5)));
196+
}
197+
}
198+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using System.Collections.Generic;
2+
using System.Globalization;
3+
using Newtonsoft.Json.Utilities;
4+
5+
namespace Newtonsoft.Json.Linq.JsonPath
6+
{
7+
internal class ArrayIndexFilter : PathFilter
8+
{
9+
public int? Index { get; set; }
10+
11+
public override IEnumerable<JToken> ExecuteFilter(IEnumerable<JToken> current, bool errorWhenNoMatch)
12+
{
13+
foreach (JToken t in current)
14+
{
15+
if (Index != null)
16+
{
17+
JToken v = GetTokenIndex(t, errorWhenNoMatch, Index.Value);
18+
19+
if (v != null)
20+
yield return v;
21+
}
22+
else
23+
{
24+
if (t is JArray || t is JConstructor)
25+
{
26+
foreach (JToken v in t)
27+
{
28+
yield return v;
29+
}
30+
}
31+
else
32+
{
33+
if (errorWhenNoMatch)
34+
throw new JsonException("Index * not valid on {0}.".FormatWith(CultureInfo.InvariantCulture, t.GetType().Name));
35+
}
36+
}
37+
}
38+
}
39+
}
40+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using System.Collections.Generic;
2+
3+
namespace Newtonsoft.Json.Linq.JsonPath
4+
{
5+
internal class ArrayMultipleIndexFilter : PathFilter
6+
{
7+
public List<int> Indexes { get; set; }
8+
9+
public override IEnumerable<JToken> ExecuteFilter(IEnumerable<JToken> current, bool errorWhenNoMatch)
10+
{
11+
foreach (JToken t in current)
12+
{
13+
foreach (int i in Indexes)
14+
{
15+
JToken v = GetTokenIndex(t, errorWhenNoMatch, i);
16+
17+
if (v != null)
18+
yield return v;
19+
}
20+
}
21+
}
22+
}
23+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Globalization;
4+
using Newtonsoft.Json.Utilities;
5+
6+
namespace Newtonsoft.Json.Linq.JsonPath
7+
{
8+
internal class ArraySliceFilter : PathFilter
9+
{
10+
public int? Start { get; set; }
11+
public int? End { get; set; }
12+
public int? Step { get; set; }
13+
14+
public override IEnumerable<JToken> ExecuteFilter(IEnumerable<JToken> current, bool errorWhenNoMatch)
15+
{
16+
if (Step == 0)
17+
throw new JsonException("Step cannot be zero.");
18+
19+
foreach (JToken t in current)
20+
{
21+
JArray a = t as JArray;
22+
if (a != null)
23+
{
24+
// set defaults for null arguments
25+
int stepCount = Step ?? 1;
26+
int startIndex = Start ?? ((stepCount > 0) ? 0 : a.Count - 1);
27+
int stopIndex = End ?? ((stepCount > 0) ? a.Count : -1);
28+
29+
// start from the end of the list if start is negitive
30+
if (Start < 0) startIndex = a.Count + startIndex;
31+
32+
// end from the start of the list if stop is negitive
33+
if (End < 0) stopIndex = a.Count + stopIndex;
34+
35+
// ensure indexes keep within collection bounds
36+
startIndex = Math.Max(startIndex, (stepCount > 0) ? 0 : int.MinValue);
37+
startIndex = Math.Min(startIndex, (stepCount > 0) ? a.Count : a.Count - 1);
38+
stopIndex = Math.Max(stopIndex, -1);
39+
stopIndex = Math.Min(stopIndex, a.Count);
40+
41+
bool positiveStep = (stepCount > 0);
42+
43+
if (IsValid(startIndex, stopIndex, positiveStep))
44+
{
45+
for (int i = startIndex; IsValid(i, stopIndex, positiveStep); i += stepCount)
46+
{
47+
yield return a[i];
48+
}
49+
}
50+
else
51+
{
52+
if (errorWhenNoMatch)
53+
throw new JsonException("Array slice of {0} to {1} returned no results.".FormatWith(CultureInfo.InvariantCulture,
54+
Start != null ? Start.Value.ToString(CultureInfo.InvariantCulture) : "*",
55+
End != null ? End.Value.ToString(CultureInfo.InvariantCulture) : "*"));
56+
}
57+
}
58+
else
59+
{
60+
if (errorWhenNoMatch)
61+
throw new JsonException("Array slice is not valid on {0}.".FormatWith(CultureInfo.InvariantCulture, t.GetType().Name));
62+
}
63+
}
64+
}
65+
66+
private bool IsValid(int index, int stopIndex, bool positiveStep)
67+
{
68+
if (positiveStep)
69+
return (index < stopIndex);
70+
71+
return (index > stopIndex);
72+
}
73+
}
74+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
using System.Collections.Generic;
2+
using System.Globalization;
3+
using Newtonsoft.Json.Utilities;
4+
5+
namespace Newtonsoft.Json.Linq.JsonPath
6+
{
7+
internal class FieldFilter : PathFilter
8+
{
9+
public string Name { get; set; }
10+
11+
public override IEnumerable<JToken> ExecuteFilter(IEnumerable<JToken> current, bool errorWhenNoMatch)
12+
{
13+
foreach (JToken t in current)
14+
{
15+
JObject o = t as JObject;
16+
if (o != null)
17+
{
18+
if (Name != null)
19+
{
20+
JToken v = o[Name];
21+
22+
if (v != null)
23+
yield return v;
24+
else if (errorWhenNoMatch)
25+
throw new JsonException("Property '{0}' does not exist on JObject.".FormatWith(CultureInfo.InvariantCulture, Name));
26+
}
27+
else
28+
{
29+
foreach (KeyValuePair<string, JToken> p in o)
30+
{
31+
yield return p.Value;
32+
}
33+
}
34+
}
35+
else
36+
{
37+
if (errorWhenNoMatch)
38+
throw new JsonException("Property '{0}' not valid on {1}.".FormatWith(CultureInfo.InvariantCulture, Name ?? "*", t.GetType().Name));
39+
}
40+
}
41+
}
42+
}
43+
}

0 commit comments

Comments
 (0)