-Fixed jsonpath error when parsing a property that follows an escaped property

-Changed reader/writer/token path to escape property names containing special characters
This commit is contained in:
James Newton-King
2014-10-18 15:36:42 +13:00
parent 01f1c5e261
commit 88a29704da
12 changed files with 385 additions and 287 deletions
@@ -33,4 +33,4 @@ using System.Runtime.InteropServices;
// by using the '*' as shown below: // by using the '*' as shown below:
// [assembly: AssemblyVersion("6.0.0.0")] // [assembly: AssemblyVersion("6.0.0.0")]
[assembly: AssemblyVersion("6.0.0.0")] [assembly: AssemblyVersion("6.0.0.0")]
[assembly: AssemblyFileVersion("6.0.6.17817")] [assembly: AssemblyFileVersion("6.0.6.17818")]
@@ -203,7 +203,8 @@ namespace Newtonsoft.Json.Tests.Converters
ExceptionAssert.Throws<JsonException>(() => ExceptionAssert.Throws<JsonException>(() =>
{ {
converter.ReadJson(writer, typeof (JsonObject), null, null); converter.ReadJson(writer, typeof (JsonObject), null, null);
}, "Could not convert 'Windows.Data.Json.JsonValue' to 'Windows.Data.Json.JsonObject'. Path '', line 1, position 4."); },
"Could not convert 'Windows.Data.Json.JsonValue' to 'Windows.Data.Json.JsonObject'. Path '', line 1, position 4.");
} }
[Test] [Test]
@@ -314,7 +315,7 @@ namespace Newtonsoft.Json.Tests.Converters
string jsonnet = timer.Elapsed.TotalSeconds.ToString(); string jsonnet = timer.Elapsed.TotalSeconds.ToString();
throw new Exception(string.Format("winrt: {0}, jsonnet: {1}, jsonnet linq: {2}", winrt, jsonnet, linq)); Debug.WriteLine("winrt: {0}, jsonnet: {1}, jsonnet linq: {2}", winrt, jsonnet, linq);
} }
[Test] [Test]
@@ -361,7 +362,7 @@ namespace Newtonsoft.Json.Tests.Converters
string jsonnet = timer.Elapsed.TotalSeconds.ToString(); string jsonnet = timer.Elapsed.TotalSeconds.ToString();
throw new Exception(string.Format("winrt: {0}, jsonnet: {1}, jsonnet linq: {2}", winrt, jsonnet, linq)); Debug.WriteLine("winrt: {0}, jsonnet: {1}, jsonnet linq: {2}", winrt, jsonnet, linq);
} }
[Test] [Test]
@@ -405,7 +406,9 @@ namespace Newtonsoft.Json.Tests.Converters
JsonObject jsonObject = new JsonObject JsonObject jsonObject = new JsonObject
{ {
{"CPU", JsonValue.CreateStringValue("Intel")}, {"CPU", JsonValue.CreateStringValue("Intel")},
{"Drives", new JsonArray { {
"Drives", new JsonArray
{
JsonValue.CreateStringValue("DVD read/writer"), JsonValue.CreateStringValue("DVD read/writer"),
JsonValue.CreateStringValue("500 gigabyte hard drive") JsonValue.CreateStringValue("500 gigabyte hard drive")
} }
@@ -418,7 +421,9 @@ namespace Newtonsoft.Json.Tests.Converters
JObject jObject = new JObject JObject jObject = new JObject
{ {
{"CPU", "Intel"}, {"CPU", "Intel"},
{"Drives", new JArray { {
"Drives", new JArray
{
"DVD read/writer", "DVD read/writer",
"500 gigabyte hard drive" "500 gigabyte hard drive"
} }
@@ -433,7 +438,9 @@ namespace Newtonsoft.Json.Tests.Converters
JsonObject jsonObject = new JsonObject JsonObject jsonObject = new JsonObject
{ {
{"CPU", JsonValue.CreateStringValue("Intel")}, {"CPU", JsonValue.CreateStringValue("Intel")},
{"Drives", new JsonArray { {
"Drives", new JsonArray
{
JsonValue.CreateStringValue("DVD read/writer"), JsonValue.CreateStringValue("DVD read/writer"),
JsonValue.CreateStringValue("500 gigabyte hard drive") JsonValue.CreateStringValue("500 gigabyte hard drive")
} }
@@ -449,4 +456,5 @@ namespace Newtonsoft.Json.Tests.Converters
} }
} }
} }
#endif #endif
@@ -3151,6 +3151,32 @@ null//comment
Assert.IsTrue(reader.Read()); Assert.IsTrue(reader.Read());
} }
[Test]
public void EscapedPathInExceptionMessage()
{
string json = @"{
""frameworks"": {
""aspnetcore50"": {
""dependencies"": {
""System.Xml.ReaderWriter"": {
""source"": !!! !!!
}
}
}
}
}";
ExceptionAssert.Throws<JsonReaderException>(
() =>
{
JsonTextReader reader = new JsonTextReader(new StringReader(json));
while (reader.Read())
{
}
},
"Unexpected character encountered while parsing value: !. Path 'frameworks.aspnetcore50.dependencies.['System.Xml.ReaderWriter'].source', line 6, position 21.");
}
} }
public class ToggleReaderError : TextReader public class ToggleReaderError : TextReader
@@ -643,5 +643,18 @@ namespace Newtonsoft.Json.Tests.Linq.JsonPath
{ {
ExceptionAssert.Throws<JsonException>(() => { new JPath("[1]Blah"); }, "Unexpected character following indexer: B"); ExceptionAssert.Throws<JsonException>(() => { new JPath("[1]Blah"); }, "Unexpected character following indexer: B");
} }
[Test]
public void PropertyFollowingEscapedPropertyName()
{
JPath path = new JPath("frameworks.aspnetcore50.dependencies.['System.Xml.ReaderWriter'].source");
Assert.AreEqual(5, path.Filters.Count);
Assert.AreEqual("frameworks", ((FieldFilter)path.Filters[0]).Name);
Assert.AreEqual("aspnetcore50", ((FieldFilter)path.Filters[1]).Name);
Assert.AreEqual("dependencies", ((FieldFilter)path.Filters[2]).Name);
Assert.AreEqual("System.Xml.ReaderWriter", ((FieldFilter)path.Filters[3]).Name);
Assert.AreEqual("source", ((FieldFilter)path.Filters[4]).Name);
}
} }
} }
@@ -184,6 +184,55 @@ undefined
Assert.AreEqual("person.$id", idProperty.Path); Assert.AreEqual("person.$id", idProperty.Path);
} }
[Test]
public void EscapedPath()
{
string json = @"{
""frameworks"": {
""aspnetcore50"": {
""dependencies"": {
""System.Xml.ReaderWriter"": {
""source"": ""NuGet""
}
}
}
}
}";
JObject o = JObject.Parse(json);
JToken v1 = o["frameworks"]["aspnetcore50"]["dependencies"]["System.Xml.ReaderWriter"]["source"];
Console.WriteLine(v1.Path);
JToken v2 = o.SelectToken(v1.Path);
Assert.AreEqual(v1, v2);
}
[Test]
public void EscapedPathTests()
{
EscapedPathAssert("this has spaces", "['this has spaces']");
EscapedPathAssert("(RoundBraces)", "['(RoundBraces)']");
EscapedPathAssert("[SquareBraces]", "['[SquareBraces]']");
EscapedPathAssert("this.has.dots", "['this.has.dots']");
}
private void EscapedPathAssert(string propertyName, string expectedPath)
{
int v1 = int.MaxValue;
JValue value = new JValue(v1);
JObject o = new JObject(new JProperty(propertyName, value));
Assert.AreEqual(expectedPath, value.Path);
JValue selectedValue = (JValue)o.SelectToken(value.Path);
Assert.AreEqual(value, selectedValue);
}
[Test] [Test]
public void ForEach() public void ForEach()
{ {
@@ -16,6 +16,7 @@
<ProjectTypeGuids>{BC8A1FFA-BEE3-4634-8014-F334798102B3};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids> <ProjectTypeGuids>{BC8A1FFA-BEE3-4634-8014-F334798102B3};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
<PackageCertificateKeyFile>Newtonsoft.Json.Tests.WinRT_TemporaryKey.pfx</PackageCertificateKeyFile> <PackageCertificateKeyFile>Newtonsoft.Json.Tests.WinRT_TemporaryKey.pfx</PackageCertificateKeyFile>
<PackageCertificateThumbprint>3116117C65F73A227A0BDC35F584F04145BA4BB7</PackageCertificateThumbprint> <PackageCertificateThumbprint>3116117C65F73A227A0BDC35F584F04145BA4BB7</PackageCertificateThumbprint>
<AppxAutoIncrementPackageRevision>False</AppxAutoIncrementPackageRevision>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols> <DebugSymbols>true</DebugSymbols>
@@ -381,6 +382,7 @@
<ItemGroup> <ItemGroup>
<Reference Include="System.Collections.Immutable"> <Reference Include="System.Collections.Immutable">
<HintPath>..\packages\Microsoft.Bcl.Immutable.1.1.20-beta\lib\portable-net45+win8\System.Collections.Immutable.dll</HintPath> <HintPath>..\packages\Microsoft.Bcl.Immutable.1.1.20-beta\lib\portable-net45+win8\System.Collections.Immutable.dll</HintPath>
<Private>True</Private>
</Reference> </Reference>
</ItemGroup> </ItemGroup>
<PropertyGroup Condition=" '$(VisualStudioVersion)' == '' or '$(VisualStudioVersion)' &lt; '12.0' "> <PropertyGroup Condition=" '$(VisualStudioVersion)' == '' or '$(VisualStudioVersion)' &lt; '12.0' ">
@@ -74,4 +74,4 @@ using System.Security;
// by using the '*' as shown below: // by using the '*' as shown below:
[assembly: AssemblyVersion("6.0.0.0")] [assembly: AssemblyVersion("6.0.0.0")]
[assembly: AssemblyFileVersion("6.0.6.17817")] [assembly: AssemblyFileVersion("6.0.6.17818")]
@@ -3215,7 +3215,7 @@ Path '', line 1, position 1.");
} }
catch (JsonSerializationException ex) catch (JsonSerializationException ex)
{ {
Assert.IsTrue(ex.Message.StartsWith("Could not convert string 'Newtonsoft.Json.Tests.TestObjects.Person' to dictionary key type 'Newtonsoft.Json.Tests.TestObjects.Person'. Create a TypeConverter to convert from the string to the key type object. Path 'Newtonsoft.Json.Tests.TestObjects.Person'")); Assert.IsTrue(ex.Message.StartsWith("Could not convert string 'Newtonsoft.Json.Tests.TestObjects.Person' to dictionary key type 'Newtonsoft.Json.Tests.TestObjects.Person'. Create a TypeConverter to convert from the string to the key type object. Path '['Newtonsoft.Json.Tests.TestObjects.Person']'"));
} }
} }
+14 -1
View File
@@ -41,6 +41,8 @@ namespace Newtonsoft.Json
internal struct JsonPosition internal struct JsonPosition
{ {
private static readonly char[] SpecialCharacters = {'.', ' ', '[', ']', '(', ')'};
internal JsonContainerType Type; internal JsonContainerType Type;
internal int Position; internal int Position;
internal string PropertyName; internal string PropertyName;
@@ -61,7 +63,18 @@ namespace Newtonsoft.Json
case JsonContainerType.Object: case JsonContainerType.Object:
if (sb.Length > 0) if (sb.Length > 0)
sb.Append('.'); sb.Append('.');
sb.Append(PropertyName);
string propertyName = PropertyName;
if (propertyName.IndexOfAny(SpecialCharacters) != -1)
{
sb.Append(@"['");
sb.Append(propertyName);
sb.Append(@"']");
}
else
{
sb.Append(propertyName);
}
break; break;
case JsonContainerType.Array: case JsonContainerType.Array:
case JsonContainerType.Constructor: case JsonContainerType.Constructor:
+4 -10
View File
@@ -188,7 +188,7 @@ namespace Newtonsoft.Json.Linq
IList<JToken> ancestors = Ancestors().Reverse().ToList(); IList<JToken> ancestors = Ancestors().Reverse().ToList();
ancestors.Add(this); ancestors.Add(this);
StringBuilder sb = new StringBuilder(); IList<JsonPosition> positions = new List<JsonPosition>();
for (int i = 0; i < ancestors.Count; i++) for (int i = 0; i < ancestors.Count; i++)
{ {
JToken current = ancestors[i]; JToken current = ancestors[i];
@@ -204,25 +204,19 @@ namespace Newtonsoft.Json.Linq
{ {
case JTokenType.Property: case JTokenType.Property:
JProperty property = (JProperty)current; JProperty property = (JProperty)current;
positions.Add(new JsonPosition(JsonContainerType.Object) { PropertyName = property.Name });
if (sb.Length > 0)
sb.Append('.');
sb.Append(property.Name);
break; break;
case JTokenType.Array: case JTokenType.Array:
case JTokenType.Constructor: case JTokenType.Constructor:
int index = ((IList<JToken>)current).IndexOf(next); int index = ((IList<JToken>)current).IndexOf(next);
sb.Append('['); positions.Add(new JsonPosition(JsonContainerType.Array) { Position = index });
sb.Append(index);
sb.Append(']');
break; break;
} }
} }
} }
return sb.ToString(); return JsonPosition.BuildPath(positions);
} }
} }
@@ -609,24 +609,17 @@ namespace Newtonsoft.Json.Linq.JsonPath
private PathFilter ParseQuotedField(char indexerCloseChar) private PathFilter ParseQuotedField(char indexerCloseChar)
{ {
//_currentIndex++;
//int start = _currentIndex;
List<string> fields = null; List<string> fields = null;
while (_currentIndex < _expression.Length) while (_currentIndex < _expression.Length)
{ {
string field = ReadQuotedString(); string field = ReadQuotedString();
//_currentIndex++;
//EnsureLength("Path ended with open indexer.");
EatWhitespace(); EatWhitespace();
EnsureLength("Path ended with open indexer."); EnsureLength("Path ended with open indexer.");
if (_expression[_currentIndex] == indexerCloseChar) if (_expression[_currentIndex] == indexerCloseChar)
{ {
_currentIndex++;
if (fields != null) if (fields != null)
{ {
fields.Add(field); fields.Add(field);
@@ -91,5 +91,5 @@ using System.Security;
// by using the '*' as shown below: // by using the '*' as shown below:
[assembly: AssemblyVersion("6.0.0.0")] [assembly: AssemblyVersion("6.0.0.0")]
[assembly: AssemblyFileVersion("6.0.6.17817")] [assembly: AssemblyFileVersion("6.0.6.17818")]
[assembly: CLSCompliant(true)] [assembly: CLSCompliant(true)]