Пример #1
0
        private string ReadString()
        {
            _buffer.Clear();
            var escaped = false;

            while (true)
            {
                var next = ReadNextChar();

                if (next == -1 || next == '\n')
                {
                    throw new JsonDeserializerException(
                              JsonDeserializerResource.JSON_OpenString,
                              _line, _column);
                }
                else if (escaped)
                {
                    if ((next == '"') || (next == '\\') || (next == '/'))
                    {
                        _buffer.Append((char)next);
                    }
                    else if (next == 'b')
                    {
                        // '\b' backspace
                        _buffer.Append('\b');
                    }
                    else if (next == 'f')
                    {
                        // '\f' form feed
                        _buffer.Append('\f');
                    }
                    else if (next == 'n')
                    {
                        // '\n' line feed
                        _buffer.Append('\n');
                    }
                    else if (next == 'r')
                    {
                        // '\r' carriage return
                        _buffer.Append('\r');
                    }
                    else if (next == 't')
                    {
                        // '\t' tab
                        _buffer.Append('\t');
                    }
                    else if (next == 'u')
                    {
                        // '\uXXXX' unicode
                        var unicodeLine   = _line;
                        var unicodeColumn = _column;

                        _codePointBuffer.Clear();
                        for (int i = 0; i < 4; ++i)
                        {
                            next = ReadNextChar();
                            if (next == -1)
                            {
                                throw new JsonDeserializerException(
                                          JsonDeserializerResource.JSON_InvalidEnd,
                                          unicodeLine,
                                          unicodeColumn);
                            }
                            else
                            {
                                _codePointBuffer[i] = (char)next;
                            }
                        }

                        try
                        {
                            var unicodeValue = int.Parse(_codePointBuffer.ToString(), NumberStyles.HexNumber, CultureInfo.InvariantCulture);
                            _buffer.Append((char)unicodeValue);
                        }
                        catch (FormatException ex)
                        {
                            throw new JsonDeserializerException(
                                      JsonDeserializerResource.Format_InvalidUnicode(_codePointBuffer.ToString()),
                                      ex,
                                      unicodeLine,
                                      unicodeColumn);
                        }
                    }
                    else
                    {
                        throw new JsonDeserializerException(
                                  JsonDeserializerResource.Format_InvalidSyntaxNotExpected("charactor escape", "\\" + next),
                                  _line,
                                  _column);
                    }

                    escaped = false;
                }
                else if (next == '\\')
                {
                    escaped = true;
                }
                else if (next == '"')
                {
                    break;
                }
                else
                {
                    _buffer.Append((char)next);
                }
            }

            return(_buffer.ToString());
        }
Пример #2
0
        private static JsonObject DeserializeObject(JsonToken head, JsonBuffer buffer)
        {
            var dictionary = new Dictionary <string, JsonValue>();

            // Loop through each JSON entry in the input object
            while (true)
            {
                var next = buffer.Read();
                if (next.Type == JsonTokenType.EOF)
                {
                    throw new JsonDeserializerException(
                              JsonDeserializerResource.Format_InvalidSyntaxExpectation("JSON object", '}'),
                              next);
                }

                if (next.Type == JsonTokenType.Colon)
                {
                    throw new JsonDeserializerException(
                              JsonDeserializerResource.Format_InvalidSyntaxNotExpected("JSON object", ':'),
                              next);
                }
                else if (next.Type == JsonTokenType.RightCurlyBracket)
                {
                    break;
                }
                else
                {
                    if (next.Type != JsonTokenType.String)
                    {
                        throw new JsonDeserializerException(
                                  JsonDeserializerResource.Format_InvalidSyntaxExpectation("JSON object member name", "JSON string"),
                                  next);
                    }

                    var memberName = next.Value;
                    if (dictionary.ContainsKey(memberName))
                    {
                        throw new JsonDeserializerException(
                                  JsonDeserializerResource.Format_DuplicateObjectMemberName(memberName),
                                  next);
                    }

                    next = buffer.Read();
                    if (next.Type != JsonTokenType.Colon)
                    {
                        throw new JsonDeserializerException(
                                  JsonDeserializerResource.Format_InvalidSyntaxExpectation("JSON object", ':'),
                                  next);
                    }

                    dictionary[memberName] = DeserializeInternal(buffer.Read(), buffer);

                    next = buffer.Read();
                    if (next.Type == JsonTokenType.RightCurlyBracket)
                    {
                        break;
                    }
                    else if (next.Type != JsonTokenType.Comma)
                    {
                        throw new JsonDeserializerException(
                                  JsonDeserializerResource.Format_InvalidSyntaxExpectation("JSON object", ',', '}'),
                                  next);
                    }
                }
            }

            return(new JsonObject(dictionary, head.Line, head.Column));
        }