Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow tabs in property names when parsing JSON #898

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions Jint.Tests/Runtime/JsonTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using Xunit;

namespace Jint.Tests.Runtime
{
public class JsonTests
{
[Fact]
public void CanParseTabsInProperties()
{
var engine = new Engine();
const string script = @"JSON.parse(""{\""abc\\tdef\"": \""42\""}"");";
var obj = engine.Execute(script).GetCompletionValue().AsObject();
Assert.True(obj.HasOwnProperty("abc\tdef"));
}
}
}
14 changes: 6 additions & 8 deletions Jint/Native/Json/JsonParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -621,7 +621,7 @@ private void ThrowError(Token token, string messageFormat, params object[] argum
index: token.Range[0],
position: new Position(token.LineNumber ?? _lineNumber, token.Range[0] - _lineStart + 1));
var exception = new ParserException("Line " + lineNumber + ": " + msg, error);

throw exception;
}

Expand Down Expand Up @@ -711,7 +711,7 @@ public ObjectInstance ParseJsonObject()
}

var name = Lex().Value.ToString();
if (PropertyNameContainsInvalidChar0To31(name))
if (PropertyNameContainsInvalidCharacters(name))
{
ExceptionHelper.ThrowSyntaxError(_engine, $"Invalid character in property name '{name}'");
}
Expand All @@ -732,14 +732,12 @@ public ObjectInstance ParseJsonObject()
return obj;
}

private static bool PropertyNameContainsInvalidChar0To31(string s)
private static bool PropertyNameContainsInvalidCharacters(string propertyName)
{
const int max = 31;

for (var i = 0; i < s.Length; i++)
const char max = (char) 31;
foreach (var c in propertyName)
{
var val = (int)s[i];
if (val <= max)
if (c != '\t' && c <= max)
return true;
}
return false;
Expand Down