コード例 #1
0
        private void WriteString(JsonValue value)
        {
            if (value.Raw == null)
            {
                throw new JsonUnexpectedRawValueException("not null string", value.Raw);
            }

            string escaped = JsonFormatUtility.Escape(value.Raw);

            Writer.Write('\"');
            Writer.Write(escaped);
            Writer.Write('\"');
        }
コード例 #2
0
        private void SkipWhiteSpaceOrComment()
        {
            char ch = Peek();

            while (CanRead() && (char.IsWhiteSpace(ch) || ch == '/'))
            {
                if (char.IsWhiteSpace(ch))
                {
                    JsonFormatUtility.SkipWhiteSpaces(Reader);
                }

                ch = Peek();

                if (ch == '/')
                {
                    SkipComment();
                }

                ch = Peek();
            }
        }
コード例 #3
0
        private JsonValue ReadString()
        {
            var builder = new StringBuilder();

            ReadAndValidate('"');

            while (CanRead())
            {
                char ch = ReadNext();

                switch (ch)
                {
                case '\\':
                {
                    builder.Append(ch);
                    builder.Append(ReadNext());
                    break;
                }

                case '"':
                {
                    string raw       = builder.ToString();
                    string unescaped = JsonFormatUtility.Unescape(raw);

                    return(new JsonValue(JsonValueType.String, unescaped));
                }

                default:
                {
                    builder.Append(ch);
                    break;
                }
                }
            }

            throw new JsonUnexpectedSymbolException("'\"' at the end of the string", (char)Reader.Peek());
        }
コード例 #4
0
        private JsonValue ReadNumber()
        {
            var builder = new StringBuilder();

            char ch = Peek();

            if (!JsonFormatUtility.IsDigit(ch) && ch != '-')
            {
                throw new JsonUnexpectedSymbolException("'0-9' digit or '-' sign at the beginning of the number", ch);
            }

            if (ch == '-')
            {
                builder.Append(ReadNext());
                ch = Peek();

                if (!JsonFormatUtility.IsDigit(ch))
                {
                    throw new JsonUnexpectedSymbolException("'0-9' digit after '-' sign at the beginning of the number", ch);
                }

                builder.Append(ReadNext());
                ch = Peek();
            }

            while (JsonFormatUtility.IsDigit(ch))
            {
                builder.Append(ReadNext());
                ch = Peek();
            }

            if (ch == '.')
            {
                builder.Append(ReadNext());
                ch = Peek();

                if (!JsonFormatUtility.IsDigit(ch))
                {
                    throw new JsonUnexpectedSymbolException("'0-9' digit after '.' decimal separator", ch);
                }

                builder.Append(ReadNext());
                ch = Peek();

                while (JsonFormatUtility.IsDigit(ch))
                {
                    builder.Append(ReadNext());
                    ch = Peek();
                }
            }

            if (ch == 'e' || ch == 'E')
            {
                builder.Append(ReadNext());
                ch = Peek();

                if (ch != '-' && ch != '+')
                {
                    throw new JsonUnexpectedSymbolException("'-' or '+' after exponent symbol", ch);
                }

                builder.Append(ReadNext());
                ch = Peek();

                if (!JsonFormatUtility.IsDigit(ch))
                {
                    throw new JsonUnexpectedSymbolException("'0-9' digit after exponent sign", ch);
                }

                builder.Append(ReadNext());
                ch = Peek();

                while (JsonFormatUtility.IsDigit(ch))
                {
                    builder.Append(ReadNext());
                    ch = Peek();
                }
            }

            return(new JsonValue(JsonValueType.Number, builder.ToString()));
        }