-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:
@@ -33,4 +33,4 @@ using System.Runtime.InteropServices;
|
||||
// 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")]
|
||||
|
||||
@@ -88,7 +88,7 @@ namespace Newtonsoft.Json.Tests.Converters
|
||||
JsonTextReader writer = new JsonTextReader(new StringReader(json));
|
||||
|
||||
JsonValueConverter converter = new JsonValueConverter();
|
||||
JsonObject o = (JsonObject)converter.ReadJson(writer, typeof(JsonObject), null, null);
|
||||
JsonObject o = (JsonObject) converter.ReadJson(writer, typeof (JsonObject), null, null);
|
||||
|
||||
Assert.AreEqual(2, o.Count);
|
||||
Assert.AreEqual("Intel", o.GetNamedString("CPU"));
|
||||
@@ -110,7 +110,7 @@ namespace Newtonsoft.Json.Tests.Converters
|
||||
JsonTextReader writer = new JsonTextReader(new StringReader(json));
|
||||
|
||||
JsonValueConverter converter = new JsonValueConverter();
|
||||
JsonObject o = (JsonObject)converter.ReadJson(writer, typeof(JsonObject), null, null);
|
||||
JsonObject o = (JsonObject) converter.ReadJson(writer, typeof (JsonObject), null, null);
|
||||
|
||||
Assert.AreEqual(2, o.Count);
|
||||
Assert.AreEqual("Intel", o.GetNamedString("CPU"));
|
||||
@@ -126,7 +126,7 @@ namespace Newtonsoft.Json.Tests.Converters
|
||||
JsonTextReader writer = new JsonTextReader(new StringReader(json));
|
||||
|
||||
JsonValueConverter converter = new JsonValueConverter();
|
||||
JsonValue v = (JsonValue)converter.ReadJson(writer, typeof(JsonValue), null, null);
|
||||
JsonValue v = (JsonValue) converter.ReadJson(writer, typeof (JsonValue), null, null);
|
||||
|
||||
Assert.AreEqual(JsonValueType.Null, v.ValueType);
|
||||
}
|
||||
@@ -142,7 +142,7 @@ namespace Newtonsoft.Json.Tests.Converters
|
||||
|
||||
ExceptionAssert.Throws<JsonException>(() =>
|
||||
{
|
||||
converter.ReadJson(writer, typeof(JsonValue), null, null);
|
||||
converter.ReadJson(writer, typeof (JsonValue), null, null);
|
||||
}, "Unexpected or unsupported token: Undefined. Path '', line 1, position 9.");
|
||||
}
|
||||
|
||||
@@ -157,7 +157,7 @@ namespace Newtonsoft.Json.Tests.Converters
|
||||
|
||||
ExceptionAssert.Throws<JsonException>(() =>
|
||||
{
|
||||
converter.ReadJson(writer, typeof(JsonValue), null, null);
|
||||
converter.ReadJson(writer, typeof (JsonValue), null, null);
|
||||
}, "Unexpected end. Path '', line 1, position 1.");
|
||||
}
|
||||
|
||||
@@ -172,7 +172,7 @@ namespace Newtonsoft.Json.Tests.Converters
|
||||
|
||||
ExceptionAssert.Throws<JsonException>(() =>
|
||||
{
|
||||
converter.ReadJson(writer, typeof(JsonValue), null, null);
|
||||
converter.ReadJson(writer, typeof (JsonValue), null, null);
|
||||
}, "Unexpected end. Path '', line 1, position 13.");
|
||||
}
|
||||
|
||||
@@ -187,7 +187,7 @@ namespace Newtonsoft.Json.Tests.Converters
|
||||
|
||||
ExceptionAssert.Throws<JsonException>(() =>
|
||||
{
|
||||
converter.ReadJson(writer, typeof(JsonValue), null, null);
|
||||
converter.ReadJson(writer, typeof (JsonValue), null, null);
|
||||
}, "Unexpected end. Path 'hi', line 1, position 6.");
|
||||
}
|
||||
|
||||
@@ -202,8 +202,9 @@ namespace Newtonsoft.Json.Tests.Converters
|
||||
|
||||
ExceptionAssert.Throws<JsonException>(() =>
|
||||
{
|
||||
converter.ReadJson(writer, typeof(JsonObject), null, null);
|
||||
}, "Could not convert 'Windows.Data.Json.JsonValue' to 'Windows.Data.Json.JsonObject'. Path '', line 1, position 4.");
|
||||
converter.ReadJson(writer, typeof (JsonObject), null, null);
|
||||
},
|
||||
"Could not convert 'Windows.Data.Json.JsonValue' to 'Windows.Data.Json.JsonObject'. Path '', line 1, position 4.");
|
||||
}
|
||||
|
||||
[Test]
|
||||
@@ -314,7 +315,7 @@ namespace Newtonsoft.Json.Tests.Converters
|
||||
|
||||
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]
|
||||
@@ -361,7 +362,7 @@ namespace Newtonsoft.Json.Tests.Converters
|
||||
|
||||
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]
|
||||
@@ -394,7 +395,7 @@ namespace Newtonsoft.Json.Tests.Converters
|
||||
// LINQ to JSON
|
||||
// ------------
|
||||
JObject jObject = JObject.Parse(json);
|
||||
string itemTitle2 = (string)jObject["channel"]["item"][0]["title"];
|
||||
string itemTitle2 = (string) jObject["channel"]["item"][0]["title"];
|
||||
}
|
||||
|
||||
[Test]
|
||||
@@ -405,7 +406,9 @@ namespace Newtonsoft.Json.Tests.Converters
|
||||
JsonObject jsonObject = new JsonObject
|
||||
{
|
||||
{"CPU", JsonValue.CreateStringValue("Intel")},
|
||||
{"Drives", new JsonArray {
|
||||
{
|
||||
"Drives", new JsonArray
|
||||
{
|
||||
JsonValue.CreateStringValue("DVD read/writer"),
|
||||
JsonValue.CreateStringValue("500 gigabyte hard drive")
|
||||
}
|
||||
@@ -418,7 +421,9 @@ namespace Newtonsoft.Json.Tests.Converters
|
||||
JObject jObject = new JObject
|
||||
{
|
||||
{"CPU", "Intel"},
|
||||
{"Drives", new JArray {
|
||||
{
|
||||
"Drives", new JArray
|
||||
{
|
||||
"DVD read/writer",
|
||||
"500 gigabyte hard drive"
|
||||
}
|
||||
@@ -433,7 +438,9 @@ namespace Newtonsoft.Json.Tests.Converters
|
||||
JsonObject jsonObject = new JsonObject
|
||||
{
|
||||
{"CPU", JsonValue.CreateStringValue("Intel")},
|
||||
{"Drives", new JsonArray {
|
||||
{
|
||||
"Drives", new JsonArray
|
||||
{
|
||||
JsonValue.CreateStringValue("DVD read/writer"),
|
||||
JsonValue.CreateStringValue("500 gigabyte hard drive")
|
||||
}
|
||||
@@ -444,9 +451,10 @@ namespace Newtonsoft.Json.Tests.Converters
|
||||
JObject o = JObject.FromObject(jsonObject);
|
||||
|
||||
// convert LINQ to JSON to Windows.Data.Json
|
||||
JArray a = (JArray)o["Drives"];
|
||||
JArray a = (JArray) o["Drives"];
|
||||
JsonArray jsonArray = a.ToObject<JsonArray>();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -3151,6 +3151,32 @@ null//comment
|
||||
|
||||
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
|
||||
|
||||
@@ -643,5 +643,18 @@ namespace Newtonsoft.Json.Tests.Linq.JsonPath
|
||||
{
|
||||
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);
|
||||
}
|
||||
|
||||
[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]
|
||||
public void ForEach()
|
||||
{
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
<ProjectTypeGuids>{BC8A1FFA-BEE3-4634-8014-F334798102B3};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
|
||||
<PackageCertificateKeyFile>Newtonsoft.Json.Tests.WinRT_TemporaryKey.pfx</PackageCertificateKeyFile>
|
||||
<PackageCertificateThumbprint>3116117C65F73A227A0BDC35F584F04145BA4BB7</PackageCertificateThumbprint>
|
||||
<AppxAutoIncrementPackageRevision>False</AppxAutoIncrementPackageRevision>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
|
||||
<DebugSymbols>true</DebugSymbols>
|
||||
@@ -381,6 +382,7 @@
|
||||
<ItemGroup>
|
||||
<Reference Include="System.Collections.Immutable">
|
||||
<HintPath>..\packages\Microsoft.Bcl.Immutable.1.1.20-beta\lib\portable-net45+win8\System.Collections.Immutable.dll</HintPath>
|
||||
<Private>True</Private>
|
||||
</Reference>
|
||||
</ItemGroup>
|
||||
<PropertyGroup Condition=" '$(VisualStudioVersion)' == '' or '$(VisualStudioVersion)' < '12.0' ">
|
||||
|
||||
@@ -74,4 +74,4 @@ using System.Security;
|
||||
// by using the '*' as shown below:
|
||||
|
||||
[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)
|
||||
{
|
||||
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']'"));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -41,6 +41,8 @@ namespace Newtonsoft.Json
|
||||
|
||||
internal struct JsonPosition
|
||||
{
|
||||
private static readonly char[] SpecialCharacters = {'.', ' ', '[', ']', '(', ')'};
|
||||
|
||||
internal JsonContainerType Type;
|
||||
internal int Position;
|
||||
internal string PropertyName;
|
||||
@@ -61,7 +63,18 @@ namespace Newtonsoft.Json
|
||||
case JsonContainerType.Object:
|
||||
if (sb.Length > 0)
|
||||
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;
|
||||
case JsonContainerType.Array:
|
||||
case JsonContainerType.Constructor:
|
||||
|
||||
@@ -188,7 +188,7 @@ namespace Newtonsoft.Json.Linq
|
||||
IList<JToken> ancestors = Ancestors().Reverse().ToList();
|
||||
ancestors.Add(this);
|
||||
|
||||
StringBuilder sb = new StringBuilder();
|
||||
IList<JsonPosition> positions = new List<JsonPosition>();
|
||||
for (int i = 0; i < ancestors.Count; i++)
|
||||
{
|
||||
JToken current = ancestors[i];
|
||||
@@ -204,25 +204,19 @@ namespace Newtonsoft.Json.Linq
|
||||
{
|
||||
case JTokenType.Property:
|
||||
JProperty property = (JProperty)current;
|
||||
|
||||
if (sb.Length > 0)
|
||||
sb.Append('.');
|
||||
|
||||
sb.Append(property.Name);
|
||||
positions.Add(new JsonPosition(JsonContainerType.Object) { PropertyName = property.Name });
|
||||
break;
|
||||
case JTokenType.Array:
|
||||
case JTokenType.Constructor:
|
||||
int index = ((IList<JToken>)current).IndexOf(next);
|
||||
|
||||
sb.Append('[');
|
||||
sb.Append(index);
|
||||
sb.Append(']');
|
||||
positions.Add(new JsonPosition(JsonContainerType.Array) { Position = index });
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return sb.ToString();
|
||||
return JsonPosition.BuildPath(positions);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -609,24 +609,17 @@ namespace Newtonsoft.Json.Linq.JsonPath
|
||||
|
||||
private PathFilter ParseQuotedField(char indexerCloseChar)
|
||||
{
|
||||
//_currentIndex++;
|
||||
//int start = _currentIndex;
|
||||
|
||||
List<string> fields = null;
|
||||
|
||||
while (_currentIndex < _expression.Length)
|
||||
{
|
||||
string field = ReadQuotedString();
|
||||
|
||||
//_currentIndex++;
|
||||
//EnsureLength("Path ended with open indexer.");
|
||||
EatWhitespace();
|
||||
EnsureLength("Path ended with open indexer.");
|
||||
|
||||
if (_expression[_currentIndex] == indexerCloseChar)
|
||||
{
|
||||
_currentIndex++;
|
||||
|
||||
if (fields != null)
|
||||
{
|
||||
fields.Add(field);
|
||||
|
||||
@@ -91,5 +91,5 @@ using System.Security;
|
||||
// by using the '*' as shown below:
|
||||
|
||||
[assembly: AssemblyVersion("6.0.0.0")]
|
||||
[assembly: AssemblyFileVersion("6.0.6.17817")]
|
||||
[assembly: AssemblyFileVersion("6.0.6.17818")]
|
||||
[assembly: CLSCompliant(true)]
|
||||
|
||||
Reference in New Issue
Block a user