Example #1
0
        static void DeserializeObject(TextReader reader, ObjectBuilder builder)
        {
            int c;

            builder.StartObject();

            while (true)
            {
                Methods.ConsumeWhiteSpace(reader);

                c = reader.Peek();
                if (c == -1)
                {
                    throw new DeserializationException("Unexpected end of stream", reader, true);
                }
                if (c == '}')
                {
                    reader.Read();  // skip }
                    break;
                }

                c = reader.Read();
                if (c == -1)
                {
                    throw new DeserializationException("Unexpected end of stream", reader, true);
                }
                if (c != '"')
                {
                    throw new DeserializationException("Expected \", found " + (char)c, reader, false);
                }

                builder.StartObjectMember();
                DeserializeString(reader, builder);

                Methods.ConsumeWhiteSpace(reader);
                c = reader.Read();
                if (c == -1)
                {
                    throw new DeserializationException("Unexpected end of stream", reader, true);
                }
                if (c != ':')
                {
                    throw new DeserializationException("Expected :, found " + (char)c, reader, false);
                }

                _DeserializeMember(reader, builder);

                builder.EndObjectMember();

                Methods.ConsumeWhiteSpace(reader);
                c = reader.Read();

                if (c == ',')
                {
                    continue;
                }
                if (c == '}')
                {
                    break;
                }

                if (c == -1)
                {
                    throw new DeserializationException("Unexpected end of stream", reader, true);
                }

                throw new DeserializationException("Expected , or }, found " + (char)c, reader, false);
            }

            builder.EndObject();
        }
Example #2
0
        static void DeserializeNumber(char leadingChar, TextReader reader, ObjectBuilder builder)
        {
            if (!UseFastNumberParsing)
            {
                var number = Methods.ReadDouble(leadingChar, reader, ref builder.CommonStringBuffer);

                builder.PutNumber(number);

                return;
            }

            bool  negative;
            ulong beforeDot;
            long  afterE;
            uint? afterDot;
            ulong?afterDotLong;
            byte  afterDotLen;
            byte  extraOrdersOfMagnitude;

            if (leadingChar == '-')
            {
                var next = reader.Read();
                if (next != '-' && !(next >= '0' && next <= '9'))
                {
                    throw new DeserializationException("Expected -, or digit", reader, next == -1);
                }

                leadingChar = (char)next;
                negative    = true;
            }
            else
            {
                negative = false;
            }

            beforeDot = Methods.ReadULong(leadingChar, reader, out extraOrdersOfMagnitude);
            var c = reader.Peek();

            if (c == '.')
            {
                reader.Read();
                c = reader.Read();
                if (c < '0' || c > '9')
                {
                    throw new DeserializationException("Expected digit", reader, c == -1);
                }

                Methods.ReadUIntOrULong((char)c, reader, out afterDotLen, out afterDot, out afterDotLong);

                c = reader.Peek();
            }
            else
            {
                afterDotLong = afterDot = afterDotLen = 0;
            }

            if (c == 'e' || c == 'E')
            {
                reader.Read();
                c = reader.Read();
                if (c == '+')
                {
                    c = reader.Read();
                }
                if (c != '-' && !(c >= '0' || c <= '9'))
                {
                    throw new DeserializationException("Expected -, +, or digit", reader, c == -1);
                }
                afterE = Methods.ReadLong((char)c, reader);
            }
            else
            {
                afterE = 0;
            }

            if (extraOrdersOfMagnitude != 0)
            {
                try
                {
                    checked
                    {
                        afterE += extraOrdersOfMagnitude;
                    }
                }
                catch (OverflowException)
                {
                    throw new DeserializationException("Number too large to be parsed encountered", reader, false);
                }
            }

            // TODO: handle the case where afterDot is null, instead using the value in afterDotLong
            ulong afterDotVal = afterDot.HasValue ? afterDot.Value : afterDotLong.Value;

            builder.PutFastNumber(negative, beforeDot, afterDotVal, afterDotLen, afterE);
        }
Example #3
0
        static void DeserializeString(TextReader reader, ObjectBuilder builder)
        {
            var str = Methods.ReadEncodedStringWithBuffer(reader, builder.CommonCharBuffer, ref builder.CommonStringBuffer);

            builder.PutString(str);
        }