-Fixed escaping special characters in generated JSONPaths

-Fixed reading escaped special characters in JSONPaths
This commit is contained in:
James Newton-King
2018-01-27 18:39:44 +13:00
parent 0b975a2087
commit 6360cc4c4b
4 changed files with 142 additions and 18 deletions
@@ -0,0 +1,65 @@
#region License
// Copyright (c) 2007 James Newton-King
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
#endregion
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Serialization;
#if DNXCORE50
using Xunit;
using Test = Xunit.FactAttribute;
using Assert = Newtonsoft.Json.Tests.XUnitAssert;
#else
using NUnit.Framework;
#endif
namespace Newtonsoft.Json.Tests.Issues
{
[TestFixture]
public class Issue1592 : TestFixtureBase
{
[Test]
public void Test()
{
string json = @"{
""test customer's"": ""testing""
}";
StringReader stringReader = new StringReader(json);
JsonTextReader reader = new JsonTextReader(stringReader);
reader.Read();
reader.Read();
reader.Read();
Assert.AreEqual("testing", reader.Value);
Assert.AreEqual("['test customer\\'s']", reader.Path);
}
}
}
@@ -34,6 +34,7 @@ using Newtonsoft.Json.Converters;
using Xunit;
using Test = Xunit.FactAttribute;
using Assert = Newtonsoft.Json.Tests.XUnitAssert;
using TestCase = Xunit.InlineDataAttribute;
#else
using NUnit.Framework;
#endif
@@ -1290,5 +1291,37 @@ namespace Newtonsoft.Json.Tests.Linq
ExceptionAssert.Throws<JsonReaderException>(() => JToken.Parse(json),
"Additional text encountered after finished reading JSON content: {. Path '', line 3, position 0.");
}
#if DNXCORE50
[Theory]
#endif
[TestCase("test customer", "['test customer']")]
[TestCase("test customer's", "['test customer\\'s']")]
[TestCase("testcustomer's", "['testcustomer\\'s']")]
[TestCase("testcustomer", "testcustomer")]
[TestCase("test.customer", "['test.customer']")]
[TestCase("test\rcustomer", "['test\\rcustomer']")]
[TestCase("test\ncustomer", "['test\\ncustomer']")]
[TestCase("test\tcustomer", "['test\\tcustomer']")]
[TestCase("test\bcustomer", "['test\\bcustomer']")]
[TestCase("test\fcustomer", "['test\\fcustomer']")]
[TestCase("test/customer", "['test/customer']")]
[TestCase("test\\customer", "['test\\\\customer']")]
[TestCase("\"test\"customer", "['\"test\"customer']")]
public void PathEscapingTest(string name, string expectedPath)
{
JValue v = new JValue("12345");
JObject o = new JObject
{
[name] = v
};
string path = v.Path;
Assert.AreEqual(expectedPath, path);
JToken token = o.SelectToken(path);
Assert.AreEqual(v, token);
}
}
}