-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
+15 -5
View File
@@ -26,6 +26,7 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Text;
using Newtonsoft.Json.Utilities;
@@ -41,7 +42,7 @@ namespace Newtonsoft.Json
internal struct JsonPosition
{
private static readonly char[] SpecialCharacters = { '.', ' ', '[', ']', '(', ')' };
private static readonly char[] SpecialCharacters = { '.', ' ', '\'', '/', '"', '[', ']', '(', ')', '\t', '\n', '\r', '\f', '\b', '\\', '\u0085', '\u2028', '\u2029' };
internal JsonContainerType Type;
internal int Position;
@@ -70,7 +71,7 @@ namespace Newtonsoft.Json
}
}
internal void WriteTo(StringBuilder sb)
internal void WriteTo(StringBuilder sb, ref StringWriter writer, ref char[] buffer)
{
switch (Type)
{
@@ -79,7 +80,14 @@ namespace Newtonsoft.Json
if (propertyName.IndexOfAny(SpecialCharacters) != -1)
{
sb.Append(@"['");
sb.Append(propertyName);
if (writer == null)
{
writer = new StringWriter(sb);
}
JavaScriptUtils.WriteEscapedJavaScriptString(writer, propertyName, '\'', false, JavaScriptUtils.SingleQuoteCharEscapeFlags, StringEscapeHandling.Default, null, ref buffer);
sb.Append(@"']");
}
else
@@ -122,16 +130,18 @@ namespace Newtonsoft.Json
}
StringBuilder sb = new StringBuilder(capacity);
StringWriter writer = null;
char[] buffer = null;
if (positions != null)
{
foreach (JsonPosition state in positions)
{
state.WriteTo(sb);
state.WriteTo(sb, ref writer, ref buffer);
}
}
if (currentPosition != null)
{
currentPosition.GetValueOrDefault().WriteTo(sb);
currentPosition.GetValueOrDefault().WriteTo(sb, ref writer, ref buffer);
}
return sb.ToString();
+29 -13
View File
@@ -661,28 +661,44 @@ namespace Newtonsoft.Json.Linq.JsonPath
if (currentChar == '\\' && _currentIndex + 1 < _expression.Length)
{
_currentIndex++;
currentChar = _expression[_currentIndex];
if (_expression[_currentIndex] == '\'')
char resolvedChar;
switch (currentChar)
{
sb.Append('\'');
}
else if (_expression[_currentIndex] == '\\')
{
sb.Append('\\');
}
else
{
throw new JsonException(@"Unknown escape character: \" + _expression[_currentIndex]);
case 'b':
resolvedChar = '\b';
break;
case 't':
resolvedChar = '\t';
break;
case 'n':
resolvedChar = '\n';
break;
case 'f':
resolvedChar = '\f';
break;
case 'r':
resolvedChar = '\r';
break;
case '\\':
case '"':
case '\'':
case '/':
resolvedChar = currentChar;
break;
default:
throw new JsonException(@"Unknown escape character: \" + _expression[_currentIndex]);
}
sb.Append(resolvedChar);
_currentIndex++;
}
else if (currentChar == '\'')
{
_currentIndex++;
{
return sb.ToString();
}
return sb.ToString();
}
else
{