Skip to content

Commit b2a557e

Browse files
committed
handle double quote escaping
1 parent 57ade15 commit b2a557e

2 files changed

Lines changed: 14 additions & 1 deletion

File tree

src/ServiceStack.Common/Script/JsToken.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,10 @@ public static bool FirstCharEquals(this string literal, char c) =>
146146
public static char SafeGetChar(this ReadOnlySpan<char> literal, int index) =>
147147
index >= 0 && index < literal.Length ? literal[index] : default(char);
148148

149+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
150+
public static bool SafeCharEquals(this ReadOnlySpan<char> literal, int index, char c) =>
151+
index >= 0 && index < literal.Length && literal[index] == c;
152+
149153
[MethodImpl(MethodImplOptions.AggressiveInlining)]
150154
public static char SafeGetChar(this ReadOnlyMemory<char> literal, int index) => literal.Span.SafeGetChar(index);
151155

@@ -356,7 +360,8 @@ public static ReadOnlySpan<char> ParseJsToken(this ReadOnlySpan<char> literal, o
356360
c = literal[i];
357361
if (c == quoteChar)
358362
{
359-
if (literal.SafeGetChar(i - 1) != '\\' || literal.SafeGetChar(i - 2) == '\\')
363+
if (!literal.SafeCharEquals(i - 1,'\\') ||
364+
(literal.SafeCharEquals(i - 2,'\\') && !literal.SafeCharEquals(i - 3,'\\')))
360365
break;
361366
}
362367

tests/ServiceStack.WebHost.Endpoints.Tests/ScriptTests/JsonTests.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,13 @@ public void Does_escape_strings_in_JSON()
1313

1414
Assert.That(obj["app.settings"], Is.EqualTo("debug true\nname MyApp\n"));
1515
}
16+
17+
[Test]
18+
public void Can_parse_escaped_json()
19+
{
20+
var json = "{\"content\": \"warn(\\n false,\\n \\\"props in \\\\\\\"\\\" + (route.path) + \\\"\\\\\\\" is a \\\" + (typeof config) + \\\", \\\" +\\n \\\"expecting an object, function or boolean.\\\"\\n );\"}";
21+
var obj = (Dictionary<string,object>)JSON.parse(json);
22+
Assert.That(obj.ContainsKey("content"));
23+
}
1624
}
1725
}

0 commit comments

Comments
 (0)