Patch: support for \xNN and octal escaped strings

Feb 2, 2011 at 9:13 AM

Hi, I'm writing a Google Reader client, but some strings are encoded in octal.

Here's a patch to add support for octal and hex (\x, not \u) escaped strings

JsonTextReader.cs

 

                case 'u':
                  // ...
                case 'x':
                  hexValues = new char[2];
                  for (int i = 0; i < hexValues.Length; i++)
                  {
                      if ((currentChar = MoveNext()) != '\0' || !_end)
                          hexValues[i] = currentChar;
                      else
                          throw CreateJsonReaderException("Unexpected end while parsing unicode character. Line {0}, position {1}.", _currentLineNumber, _currentLinePosition);
                  }

                  hexChar = Convert.ToChar(int.Parse(new string(hexValues), NumberStyles.HexNumber, NumberFormatInfo.InvariantInfo));
                  _buffer.Append(hexChar);
                  break;

                default:
                  var octValues = new char[3];
                  var octLength = 0;
                  for (int i = 0; i < octValues.Length; i++)
                  {
                      var octalChar = i==0 ? currentChar : PeekNext();
                      if ((octalChar > 1 || !_end) && octalChar>='0' && octalChar<'8')
                      {
                          octValues[i] = (char)octalChar;
                          if(i!=0) MoveNext();
                          octLength++;
                      }
                      else
                      {
                          break;
                      }
                  }
                  if (octLength>0)
                  {
                      hexChar = Convert.ToChar(Convert.ToInt32(new string(octValues, 0, octLength), 8));
                      _buffer.Append(hexChar);
                      break;
                  }

                  throw CreateJsonReaderException("Bad JSON escape sequence: {0}. Line {1}, position {2}.", @"\" + currentChar, _currentLineNumber, _currentLinePosition);
              }
            }
            else
            {
              throw CreateJsonReaderException("Unterminated string. Expected delimiter: {0}. Line {1}, position {2}.", quote, _currentLineNumber, _currentLinePosition);
            }
            break;

 

 

 

JsonReaderTest:

 

    [Test]
    public void ReadXEscapedString()
    {
        string json = @"'\xFFf\xff\x00'";
        var reader = new JsonTextReader(new StringReader(json));
        reader.Read();
        Assert.AreEqual((string)reader.Value, "ÿfÿ\0");
    }

    [Test]
    public void ReadOctalEscapedString()
    {
        string json = @"'\377 \768 \6'";
        var reader = new JsonTextReader(new StringReader(json));
        reader.Read();
        Assert.AreEqual((string)reader.Value, "ÿ >8 \x06");
    }

 

 

Mar 21, 2011 at 11:43 PM

I applied this patch against Json.NET 4.0 Release 1 (source + binary + documentation), and it worked great for me.