Skip to content

Commit 541eab2

Browse files
authored
Fix JSONPath scanning with nested indexer (#2180)
1 parent c89d6ad commit 541eab2

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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 Newtonsoft.Json.Linq;
27+
using System.Linq;
28+
#if DNXCORE50
29+
using Xunit;
30+
using Test = Xunit.FactAttribute;
31+
using Assert = Newtonsoft.Json.Tests.XUnitAssert;
32+
#else
33+
using NUnit.Framework;
34+
#endif
35+
36+
namespace Newtonsoft.Json.Tests.Issues
37+
{
38+
[TestFixture]
39+
public class Issue2156
40+
{
41+
[Test]
42+
public void Test()
43+
{
44+
string json = @"
45+
{
46+
""root"": {
47+
""a"": {
48+
""name"": ""John"",
49+
""b"": {
50+
""name"": ""Sarah""
51+
}
52+
}
53+
}
54+
}";
55+
56+
JToken t = JToken.Parse(json);
57+
58+
int count1 = t.SelectTokens("$..a.name").Count(); // result: 1, expected: 1
59+
int count2 = t.SelectTokens("$..['a']['name']").Count(); // result: 2, expected: 1
60+
61+
Assert.AreEqual(1, count1);
62+
Assert.AreEqual(1, count2);
63+
}
64+
}
65+
}

Src/Newtonsoft.Json/Linq/JsonPath/JPath.cs

+2
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,8 @@ private bool ParsePath(List<PathFilter> filters, int currentPartStartIndex, bool
118118
}
119119

120120
filters.Add(ParseIndexer(currentChar, scan));
121+
scan = false;
122+
121123
_currentIndex++;
122124
currentPartStartIndex = _currentIndex;
123125
followingIndexer = true;

0 commit comments

Comments
 (0)