Example #1
0
        /// <summary>
        /// Reads a BSON document from the specified EasyReader.
        /// </summary>
        /// <param name="reader">The reader that will be used to read this document.</param>
        /// <returns>The document that was read.</returns>
        internal static BsonDocument Read(EasyReader reader, BsonDocument parent = null, bool inArray = false)
        {
            var stringTableMode = BsonStringTableMode.None;
            Dictionary <int, string> stringTable = null;

            if (parent == null)
            {
                var includesStringTable = reader.ReadBoolean();
                if (includesStringTable)
                {
                    stringTable     = new Dictionary <int, string>();
                    stringTableMode = (BsonStringTableMode)reader.ReadByte();
                    var version = reader.ReadByte();
                    if (version != STRING_TABLE_VERSION)
                    {
                        throw new InvalidDataException("Unsupported string table version: " + version);
                    }
                    var tableLength  = reader.ReadInt32();
                    var tableEntries = reader.ReadInt32();
                    for (var i = 0; i < tableEntries; i++)
                    {
                        var num = reader.ReadInt32();
                        var val = reader.ReadString(Encoding.UTF8);
                        stringTable[num] = val;
                    }
                }
            }
            else
            {
                stringTable     = parent.ReverseStringTable;
                stringTableMode = parent.StringTableMode;
            }
            var document = new BsonDocument(stringTableMode, stringTable);

            var length = reader.ReadInt32();

            while (!reader.EndOfStream)
            {
                var code = reader.ReadByte();
                if (code == 0x00) // end of document
                {
                    break;
                }
                var name = reader.ReadCString();
                if (!inArray && document.StringTableMode != BsonStringTableMode.None)
                {
                    name = document.ReverseStringTable[int.Parse(name)];
                }
                var data = ReadItem(code, document, reader);
                document.Top[name] = data;
            }
            return(document);
        }
Example #2
0
        /// <summary>
        /// Reads a BSON document from the specified EasyReader.
        /// </summary>
        /// <param name="reader">The reader that will be used to read this document.</param>
        /// <returns>The document that was read.</returns>
        internal static BsonDocument Read(EasyReader reader, BsonDocument parent = null, bool inArray = false)
        {
            var stringTableMode = BsonStringTableMode.None;
            Dictionary<int, string> stringTable = null;
            if (parent == null)
            {
                var includesStringTable = reader.ReadBoolean();
                if (includesStringTable)
                {
                    stringTable = new Dictionary<int, string>();
                    stringTableMode = (BsonStringTableMode)reader.ReadByte();
                    var version = reader.ReadByte();
                    if (version != STRING_TABLE_VERSION)
                        throw new InvalidDataException("Unsupported string table version: " + version);
                    var tableLength = reader.ReadInt32();
                    var tableEntries = reader.ReadInt32();
                    for (var i = 0; i < tableEntries; i++)
                    {
                        var num = reader.ReadInt32();
                        var val = reader.ReadString(Encoding.UTF8);
                        stringTable[num] = val;
                    }
                }
            }
            else
            {
                stringTable = parent.ReverseStringTable;
                stringTableMode = parent.StringTableMode;
            }
            var document = new BsonDocument(stringTableMode, stringTable);

            var length = reader.ReadInt32();
            while(!reader.EndOfStream)
            {
                var code = reader.ReadByte();
                if (code == 0x00) // end of document
                    break;
                var name = reader.ReadCString();
                if (!inArray && document.StringTableMode != BsonStringTableMode.None)
                    name = document.ReverseStringTable[int.Parse(name)];
                var data = ReadItem(code, document, reader);
                document.Top[name] = data;
            }
            return document;
        }