neuecc/Utf8Json

can't deserialize string ending with backslash

vilinski opened this issue · 3 comments

Repro - add this to tests/Utf8Json.Tests/StringEscapeTest.cs

            str = "\"\\\"";
            JsonSerializer.Deserialize<string>(str).Is("\\");

the string, containing a single backslash can't be deserialized. The same bug is also reproducebla for other strings with a backslash at end.

public ArraySegment<byte> ReadStringSegmentRaw()
{
ArraySegment<byte> key = default(ArraySegment<byte>);
if (ReadIsNull())
{
key = nullTokenSegment;
}
else
{
// SkipWhiteSpace is already called from IsNull
if (bytes[offset++] != '\"') throw CreateParsingException("\"");
var from = offset;
for (int i = offset; i < bytes.Length; i++)
{
if (bytes[i] == (char)'\"')
{
// is escape?
if (bytes[i - 1] == (char)'\\')
{
continue;
}
else
{
offset = i + 1;
goto OK;
}
}
}
throw CreateParsingExceptionMessage("not found end string.");
OK:
key = new ArraySegment<byte>(bytes, from, offset - from - 1); // remove \"
}
return key;
}

Maybe this code is causing this bug.
This code works on policy like "Find first double quote,but if it is leaded by backslash,it is not a end of string.Otherwise,it is a end of string"
This policy doesn't work well in case of this.

#220
I made this pull request.
Actually,your issue was not correct.
You need "\"\\\\\"" for representing single backslash on Json.
But it was useful to find another issue.Thanks!

Cool, hope it will be merged