コード例 #1
0
        public static ObjectBuilder Deserialize(TextReader reader, Options options)
        {
            var ret = new ObjectBuilder(options);

            _DeserializeMember(reader, ret);

            Methods.ConsumeWhiteSpace(reader);
            var c = reader.Peek();

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

            return(ret);
        }
コード例 #2
0
        static void DeserializeArray(TextReader reader, ObjectBuilder builder)
        {
            int c;

            builder.StartArray();

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

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

                _DeserializeMember(reader, builder);
                Methods.ConsumeWhiteSpace(reader);
                c = reader.Read();

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

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

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

            builder.EndArray();
        }
コード例 #3
0
        static void _DeserializeMember(TextReader reader, ObjectBuilder builder)
        {
            Methods.ConsumeWhiteSpace(reader);

            var c = reader.Read();

            switch (c)
            {
            case '"': DeserializeString(reader, builder); return;

            case '[': DeserializeArray(reader, builder); return;

            case '{': DeserializeObject(reader, builder); return;

            case 'n': DeserializeNull(reader, builder); return;

            case 't': DeserializeTrue(reader, builder); return;

            case 'f': DeserializeFalse(reader, builder); return;

            case '-': DeserializeNumber('-', reader, builder); return;
            }

            if (c >= '0' && c <= '9')
            {
                DeserializeNumber((char)c, reader, builder);
                return;
            }

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

            throw new DeserializationException("Expected \", [, {, n, t, f, -, 0, 1, 2, 3, 4, 5, 6, 7, 8, or 9; found " + (char)c, reader, false);
        }
コード例 #4
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();
        }