-Fixed error with whitespace only JSONPath

-Fixed property query path that starts with $
-Fixed array query path with open brace after $
This commit is contained in:
James Newton-King
2014-02-23 14:35:28 +13:00
parent c49b58ae18
commit e4ae07003b
4 changed files with 62 additions and 14 deletions
@@ -1930,7 +1930,4 @@ namespace Newtonsoft.Json.Tests.Converters
} }
} }
} }
#endif
#endif
@@ -50,6 +50,46 @@ namespace Newtonsoft.Json.Tests.Linq.JsonPath
[TestFixture] [TestFixture]
public class JPathExecuteTests : TestFixtureBase public class JPathExecuteTests : TestFixtureBase
{ {
[Test]
public void EvaluateEmptyString()
{
JObject o = new JObject(
new JProperty("Blah", 1));
JToken t = o.SelectToken("");
Assert.AreEqual(o, t);
}
[Test]
public void EvaluateWhitespaceString()
{
JObject o = new JObject(
new JProperty("Blah", 1));
JToken t = o.SelectToken(" ");
Assert.AreEqual(o, t);
}
[Test]
public void EvaluateDollarString()
{
JObject o = new JObject(
new JProperty("Blah", 1));
JToken t = o.SelectToken("$");
Assert.AreEqual(o, t);
}
[Test]
public void EvaluateDollarTypeString()
{
JObject o = new JObject(
new JProperty("$values", new JArray(1, 2, 3)));
JToken t = o.SelectToken("$values[1]");
Assert.AreEqual(2, (int)t);
}
[Test] [Test]
public void EvaluateSingleProperty() public void EvaluateSingleProperty()
{ {
@@ -141,6 +141,14 @@ namespace Newtonsoft.Json.Tests.Linq.JsonPath
Assert.AreEqual(null, ((ArrayIndexFilter)path.Filters[0]).Index); Assert.AreEqual(null, ((ArrayIndexFilter)path.Filters[0]).Index);
} }
[Test]
public void RootArrayNoDot()
{
JPath path = new JPath("$[1]");
Assert.AreEqual(1, path.Filters.Count);
Assert.AreEqual(1, ((ArrayIndexFilter)path.Filters[0]).Index);
}
[Test] [Test]
public void WildcardArray() public void WildcardArray()
{ {
+13 -10
View File
@@ -51,21 +51,24 @@ namespace Newtonsoft.Json.Linq.JsonPath
{ {
int currentPartStartIndex = _currentIndex; int currentPartStartIndex = _currentIndex;
if (_expression.Length == 0)
return;
EatWhitespace(); EatWhitespace();
if (_expression.Length == _currentIndex)
return;
if (_expression[_currentIndex] == '$') if (_expression[_currentIndex] == '$')
{ {
_currentIndex++; if (_expression.Length == 1)
return;
EnsureLength("Unexpected end while parsing path."); // only increment position for "$." or "$["
// otherwise assume property that starts with $
if (_expression[_currentIndex] != '.') char c = _expression[_currentIndex + 1];
throw new JsonException("Unexpected character while parsing path: " + _expression[_currentIndex]); if (c == '.' || c == '[')
{
currentPartStartIndex = _currentIndex; _currentIndex++;
currentPartStartIndex = _currentIndex;
}
} }
if (!ParsePath(Filters, currentPartStartIndex, false)) if (!ParsePath(Filters, currentPartStartIndex, false))