From e4ae07003ba80ab8fef424c61bc3013e9fb4dcbf Mon Sep 17 00:00:00 2001 From: James Newton-King Date: Sun, 23 Feb 2014 14:35:28 +1300 Subject: [PATCH] -Fixed error with whitespace only JSONPath -Fixed property query path that starts with $ -Fixed array query path with open brace after $ --- .../Converters/XmlNodeConverterTest.cs | 5 +-- .../Linq/JsonPath/JPathExecuteTests.cs | 40 +++++++++++++++++++ .../Linq/JsonPath/JPathParseTests.cs | 8 ++++ Src/Newtonsoft.Json/Linq/JsonPath/JPath.cs | 23 ++++++----- 4 files changed, 62 insertions(+), 14 deletions(-) diff --git a/Src/Newtonsoft.Json.Tests/Converters/XmlNodeConverterTest.cs b/Src/Newtonsoft.Json.Tests/Converters/XmlNodeConverterTest.cs index 0c63f04e..4d0d37b3 100644 --- a/Src/Newtonsoft.Json.Tests/Converters/XmlNodeConverterTest.cs +++ b/Src/Newtonsoft.Json.Tests/Converters/XmlNodeConverterTest.cs @@ -1930,7 +1930,4 @@ namespace Newtonsoft.Json.Tests.Converters } } } - -#endif - - +#endif \ No newline at end of file diff --git a/Src/Newtonsoft.Json.Tests/Linq/JsonPath/JPathExecuteTests.cs b/Src/Newtonsoft.Json.Tests/Linq/JsonPath/JPathExecuteTests.cs index 818b4407..7f07085b 100644 --- a/Src/Newtonsoft.Json.Tests/Linq/JsonPath/JPathExecuteTests.cs +++ b/Src/Newtonsoft.Json.Tests/Linq/JsonPath/JPathExecuteTests.cs @@ -50,6 +50,46 @@ namespace Newtonsoft.Json.Tests.Linq.JsonPath [TestFixture] 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] public void EvaluateSingleProperty() { diff --git a/Src/Newtonsoft.Json.Tests/Linq/JsonPath/JPathParseTests.cs b/Src/Newtonsoft.Json.Tests/Linq/JsonPath/JPathParseTests.cs index 59845a38..e3b33796 100644 --- a/Src/Newtonsoft.Json.Tests/Linq/JsonPath/JPathParseTests.cs +++ b/Src/Newtonsoft.Json.Tests/Linq/JsonPath/JPathParseTests.cs @@ -141,6 +141,14 @@ namespace Newtonsoft.Json.Tests.Linq.JsonPath 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] public void WildcardArray() { diff --git a/Src/Newtonsoft.Json/Linq/JsonPath/JPath.cs b/Src/Newtonsoft.Json/Linq/JsonPath/JPath.cs index da37b05f..58b94ab9 100644 --- a/Src/Newtonsoft.Json/Linq/JsonPath/JPath.cs +++ b/Src/Newtonsoft.Json/Linq/JsonPath/JPath.cs @@ -51,21 +51,24 @@ namespace Newtonsoft.Json.Linq.JsonPath { int currentPartStartIndex = _currentIndex; - if (_expression.Length == 0) - return; - EatWhitespace(); + if (_expression.Length == _currentIndex) + return; + if (_expression[_currentIndex] == '$') { - _currentIndex++; + if (_expression.Length == 1) + return; - EnsureLength("Unexpected end while parsing path."); - - if (_expression[_currentIndex] != '.') - throw new JsonException("Unexpected character while parsing path: " + _expression[_currentIndex]); - - currentPartStartIndex = _currentIndex; + // only increment position for "$." or "$[" + // otherwise assume property that starts with $ + char c = _expression[_currentIndex + 1]; + if (c == '.' || c == '[') + { + _currentIndex++; + currentPartStartIndex = _currentIndex; + } } if (!ParsePath(Filters, currentPartStartIndex, false))