Пример #1
0
        internal static IEnumerable <object> ReadDocumentsAs(Type documentType, string filePath, FileFormat format)
        {
            if (format == FileFormat.Auto)
            {
                format = filePath.EndsWith(".json", StringComparison.OrdinalIgnoreCase) ? FileFormat.Json : FileFormat.Bson;
            }

            var serializer = BsonSerializer.LookupSerializer(documentType);
            var options    = DocumentSerializationOptions.Defaults;

            if (format == FileFormat.Json)
            {
                var  jb    = new JsonBuffer(File.ReadAllText(filePath));
                bool array = false;
                for (; ;)
                {
                    // skip white
                    int c;
                    for (; ;)
                    {
                        // end or white?
                        if ((c = jb.Read()) <= 32)
                        {
                            // end?
                            if (c < 0)
                            {
                                goto end;
                            }

                            // white
                            continue;
                        }

                        // document
                        if (c == '{')
                        {
                            jb.UnRead(c);
                            break;
                        }

                        // array
                        if (c == ',')
                        {
                            if (array)
                            {
                                continue;
                            }
                        }
                        else if (c == ']')
                        {
                            if (array)
                            {
                                array = false;
                                continue;
                            }
                        }
                        else if (c == '[')
                        {
                            if (!array)
                            {
                                array = true;
                                continue;
                            }
                        }

                        throw new FormatException(string.Format(null, "Unexpected character '{0}' at position {1}.", (char)c, jb.Position - 1));
                    }

                    using (var bsonReader = BsonReader.Create(jb))
                        yield return(serializer.Deserialize(bsonReader, documentType, options));
                }
                end :;
            }
            else
            {
                using (var stream = File.OpenRead(filePath))
                {
                    long length = stream.Length;

                    while (stream.Position < length)
                    {
                        using (var bsonReader = BsonReader.Create(stream))
                            yield return(serializer.Deserialize(bsonReader, documentType, options));
                    }
                }
            }
        }