Exemplo n.º 1
0
        private void ReadStringIntoBuffer(char quote)
        {
            while (true)
            {
                bool flag = true;
                char c    = MoveNext();
                switch (c)
                {
                case '\0':
                    if (_end)
                    {
                        throw CreateJsonReaderException("Unterminated string. Expected delimiter: {0}. Line {1}, position {2}.", quote, _currentLineNumber, _currentLinePosition);
                    }
                    _buffer.Append('\0');
                    break;

                case '\\':
                    if ((c = MoveNext()) != 0 || !_end)
                    {
                        switch (c)
                        {
                        case 'b':
                            _buffer.Append('\b');
                            break;

                        case 't':
                            _buffer.Append('\t');
                            break;

                        case 'n':
                            _buffer.Append('\n');
                            break;

                        case 'f':
                            _buffer.Append('\f');
                            break;

                        case 'r':
                            _buffer.Append('\r');
                            break;

                        case '\\':
                            _buffer.Append('\\');
                            break;

                        case '"':
                        case '\'':
                        case '/':
                            _buffer.Append(c);
                            break;

                        case 'u':
                        {
                            char[] array = new char[4];
                            for (int i = 0; i < array.Length; i++)
                            {
                                if ((c = MoveNext()) != 0 || !_end)
                                {
                                    array[i] = c;
                                    continue;
                                }
                                throw CreateJsonReaderException("Unexpected end while parsing unicode character. Line {0}, position {1}.", _currentLineNumber, _currentLinePosition);
                            }
                            char value = Convert.ToChar(int.Parse(new string(array), NumberStyles.HexNumber, NumberFormatInfo.InvariantInfo));
                            _buffer.Append(value);
                            break;
                        }

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

                case '"':
                case '\'':
                    if (c == quote)
                    {
                        return;
                    }
                    _buffer.Append(c);
                    break;

                default:
                    _buffer.Append(c);
                    break;
                }
            }
        }
        private void ReadStringIntoBuffer(char quote)
        {
            while (true)
            {
                char currentChar = MoveNext();

                switch (currentChar)
                {
                case '\0':
                    if (_end)
                    {
                        throw CreateJsonReaderException("Unterminated string. Expected delimiter: {0}. Line {1}, position {2}.", quote, _currentLineNumber, _currentLinePosition);
                    }

                    _buffer.Append('\0');
                    break;

                case '\\':
                    if ((currentChar = MoveNext()) != '\0' || !_end)
                    {
                        switch (currentChar)
                        {
                        case 'b':
                            _buffer.Append('\b');
                            break;

                        case 't':
                            _buffer.Append('\t');
                            break;

                        case 'n':
                            _buffer.Append('\n');
                            break;

                        case 'f':
                            _buffer.Append('\f');
                            break;

                        case 'r':
                            _buffer.Append('\r');
                            break;

                        case '\\':
                            _buffer.Append('\\');
                            break;

                        case '"':
                        case '\'':
                        case '/':
                            _buffer.Append(currentChar);
                            break;

                        case 'u':
                            char[] hexValues = new char[4];
                            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);
                                }
                            }

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

                        default:
                            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;

                case '"':
                case '\'':
                    if (currentChar == quote)
                    {
                        return;
                    }
                    else
                    {
                        _buffer.Append(currentChar);
                    }
                    break;

                default:
                    _buffer.Append(currentChar);
                    break;
                }
            }
        }
Exemplo n.º 3
0
        private void ReadStringIntoBuffer(char quote, int?untilPosition)
        {
            bool stringTerminated = false;
            bool hexNumber        = false;
            int  hexCount         = 0;

            while (!stringTerminated && (untilPosition == null || _buffer.Position < untilPosition) && MoveNext())
            {
                if (hexNumber)
                {
                    hexCount++;
                }

                switch (_currentChar)
                {
                case '\\':
                    if (MoveNext())
                    {
                        switch (_currentChar)
                        {
                        case 'b':
                            _buffer.Append('\b');
                            break;

                        case 't':
                            _buffer.Append('\t');
                            break;

                        case 'n':
                            _buffer.Append('\n');
                            break;

                        case 'f':
                            _buffer.Append('\f');
                            break;

                        case 'r':
                            _buffer.Append('\r');
                            break;

                        case 'u':
                            // note the start of a hex character
                            hexNumber = true;
                            break;

                        default:
                            _buffer.Append(_currentChar);
                            break;
                        }
                    }
                    else
                    {
                        throw CreateJsonReaderException("Unterminated string. Expected delimiter: {0}. Line {1}, position {2}.", quote, _currentLineNumber, _currentLinePosition);
                    }
                    break;

                case '"':
                case '\'':
                    if (_currentChar == quote)
                    {
                        stringTerminated = true;
                    }
                    else
                    {
                        goto default;
                    }
                    break;

                default:
                    _buffer.Append(_currentChar);
                    break;
                }

                if (hexCount == 4)
                {
                    // remove hex characters from buffer, convert to char and then add
                    string hexString = _buffer.ToString(_buffer.Position - 4, 4);
                    char   hexChar   = Convert.ToChar(int.Parse(hexString, NumberStyles.HexNumber, NumberFormatInfo.InvariantInfo));

                    _buffer.Position = _buffer.Position - 4;
                    _buffer.Append(hexChar);

                    hexNumber = false;
                    hexCount  = 0;
                }
            }

            if (!stringTerminated)
            {
                throw CreateJsonReaderException("Unterminated string. Expected delimiter: {0}. Line {1}, position {2}.", quote, _currentLineNumber, _currentLinePosition);
            }

            ClearCurrentChar();
        }
Exemplo n.º 4
0
        private void ParseString(char quote)
        {
            bool stringTerminated = false;
            bool hexNumber        = false;
            int  hexCount         = 0;

            while (!stringTerminated && MoveNext())
            {
                if (hexNumber)
                {
                    hexCount++;
                }

                switch (_currentChar)
                {
                case '\\':
                    if (MoveNext())
                    {
                        switch (_currentChar)
                        {
                        case 'b':
                            _buffer.Append('\b');
                            break;

                        case 't':
                            _buffer.Append('\t');
                            break;

                        case 'n':
                            _buffer.Append('\n');
                            break;

                        case 'f':
                            _buffer.Append('\f');
                            break;

                        case 'r':
                            _buffer.Append('\r');
                            break;

                        case 'u':
                            // note the start of a hex character
                            hexNumber = true;
                            break;

                        default:
                            _buffer.Append(_currentChar);
                            break;
                        }
                    }
                    else
                    {
                        throw CreateJsonReaderException("Unterminated string. Expected delimiter: {0}. Line {1}, position {2}.", quote, _currentLineNumber, _currentLinePosition);
                    }
                    break;

                case '"':
                case '\'':
                    if (_currentChar == quote)
                    {
                        stringTerminated = true;
                    }
                    else
                    {
                        goto default;
                    }
                    break;

                default:
                    _buffer.Append(_currentChar);
                    break;
                }

                if (hexCount == 4)
                {
                    // remove hex characters from buffer, convert to char and then add
                    string hexString = _buffer.ToString(_buffer.Position - 4, 4);
                    char   hexChar   = Convert.ToChar(int.Parse(hexString, NumberStyles.HexNumber, NumberFormatInfo.InvariantInfo));

                    _buffer.Position = _buffer.Position - 4;
                    _buffer.Append(hexChar);

                    hexNumber = false;
                    hexCount  = 0;
                }
            }

            if (!stringTerminated)
            {
                throw CreateJsonReaderException("Unterminated string. Expected delimiter: {0}. Line {1}, position {2}.", quote, _currentLineNumber, _currentLinePosition);
            }

            ClearCurrentChar();
            string text = _buffer.ToString();

            _buffer.Position = 0;

            if (text.StartsWith("/Date(", StringComparison.Ordinal) && text.EndsWith(")/", StringComparison.Ordinal))
            {
                ParseDate(text);
            }
            else
            {
                SetToken(JsonToken.String, text);
                QuoteChar = quote;
            }
        }
Exemplo n.º 5
0
        private void ParseString(char quote)
        {
            bool stringTerminated = false;

            while (!stringTerminated && MoveNext())
            {
                switch (_currentChar)
                {
                //case 0:
                //case 0x0A:
                //case 0x0D:
                //    throw new JsonReaderException("Unterminated string");
                case '\\':
                    if (MoveNext())
                    {
                        switch (_currentChar)
                        {
                        case 'b':
                            _buffer.Append('\b');
                            break;

                        case 't':
                            _buffer.Append('\t');
                            break;

                        case 'n':
                            _buffer.Append('\n');
                            break;

                        case 'f':
                            _buffer.Append('\f');
                            break;

                        case 'r':
                            _buffer.Append('\r');
                            break;

                        case 'u':
                            //_buffer.Append((char) Integer.parseInt(next(4), 16));
                            break;

                        case 'x':
                            //_buffer.Append((char) Integer.parseInt(next(2), 16));
                            break;

                        default:
                            _buffer.Append(_currentChar);
                            break;
                        }
                    }
                    else
                    {
                        throw new JsonReaderException("Unterminated string. Expected delimiter: " + quote);
                    }
                    break;

                case '"':
                case '\'':
                    if (_currentChar == quote)
                    {
                        stringTerminated = true;
                    }
                    else
                    {
                        goto default;
                    }
                    break;

                default:
                    _buffer.Append(_currentChar);
                    break;
                }
            }

            if (!stringTerminated)
            {
                throw new JsonReaderException("Unterminated string. Expected delimiter: " + quote);
            }

            ClearCurrentChar();
            _currentState    = State.PostValue;
            _token           = JsonToken.String;
            _value           = _buffer.ToString();
            _buffer.Position = 0;
            _valueType       = typeof(string);
            _quoteChar       = quote;
        }