/// <summary>
        ///   Ends the object.
        /// </summary>
        /// <param name = "instance">The instance.</param>
        /// <returns></returns>
        public object EndObject(object instance)
        {
            var document = (Document)instance;

            if (DBRef.IsDocumentDBRef(document))
            {
                return(DBRef.FromDocument(document));
            }

            return(document);
        }
示例#2
0
        public void TestIsDocumentDBRef()
        {
            var doc = new Document();

            Assert.IsFalse(DBRef.IsDocumentDBRef(null));
            Assert.IsFalse(DBRef.IsDocumentDBRef(doc));

            doc[DBRef.RefName] = "tests";
            Assert.IsFalse(DBRef.IsDocumentDBRef(doc));

            doc.Remove(DBRef.RefName);
            doc[DBRef.IdName] = "12312131";
            Assert.IsFalse(DBRef.IsDocumentDBRef(doc));

            doc[DBRef.RefName] = "tests";
            Assert.IsTrue(DBRef.IsDocumentDBRef(doc));

            doc[DBRef.MetaName] = new Document();
            Assert.IsTrue(DBRef.IsDocumentDBRef(doc));
        }
        public Object ReadElementType(sbyte typeNum)
        {
            switch ((BsonDataType)typeNum)
            {
            case BsonDataType.Null:
            case BsonDataType.Undefined:
                return(MongoDBNull.Value);

            case BsonDataType.MinKey:
                return(MongoMinKey.Value);

            case BsonDataType.MaxKey:
                return(MongoMaxKey.Value);

            case BsonDataType.Boolean:
                position++;
                return(reader.ReadBoolean());

            case BsonDataType.Integer:
                position += 4;
                return(reader.ReadInt32());

            case BsonDataType.Long:
                position += 8;
                return(reader.ReadInt64());

            case BsonDataType.Date:
                position += 8;
                long millis = reader.ReadInt64();
                return(epoch.AddMilliseconds(millis));

            case BsonDataType.Oid:
                position += 12;
                return(new Oid(reader.ReadBytes(12)));

            case BsonDataType.Number:
                position += 8;
                return(reader.ReadDouble());

            case BsonDataType.String: {
                return(ReadLenString());
            }

            case BsonDataType.Obj: {
                Document doc = this.ReadDocument();
                if (DBRef.IsDocumentDBRef(doc))
                {
                    return(DBRef.FromDocument(doc));
                }
                return(doc);
            }

            case BsonDataType.Array: {
                Document doc = this.ReadDocument();
                return(ConvertToArray(doc));
            }

            case BsonDataType.Regex: {
                MongoRegex r = new MongoRegex();
                r.Expression = this.ReadString();
                r.Options    = this.ReadString();
                return(r);
            }

            case BsonDataType.Code: {
                Code c = new Code();
                c.Value = ReadLenString();
                return(c);
            }

            case BsonDataType.CodeWScope: {
                int startpos = position;
                int size     = reader.ReadInt32();
                position += 4;

                String   val   = this.ReadLenString();
                Document scope = this.ReadDocument();
                if (size != position - startpos)
                {
                    throw new System.IO.InvalidDataException(string.Format("Should have read {0} bytes from stream but read {1} in CodeWScope", size, position - startpos));
                }
                return(new CodeWScope(val, scope));
            }

            case BsonDataType.Binary: {
                int size = reader.ReadInt32();
                position += 4;
                byte subtype = reader.ReadByte();
                position++;
                if (subtype == (byte)Binary.TypeCode.General)
                {
                    size      = reader.ReadInt32();
                    position += 4;
                }
                byte[] bytes = reader.ReadBytes(size);
                position += size;

                // From http://en.wikipedia.org/wiki/Universally_Unique_Identifier
                // The most widespread use of this standard is in Microsoft's Globally Unique Identifiers (GUIDs).
                if (subtype == 3 && 16 == size)
                {
                    return(new Guid(bytes));
                }

                Binary b = new Binary();
                b.Bytes   = bytes;
                b.Subtype = (Binary.TypeCode)subtype;
                return(b);
            }

            default:
                throw new ArgumentOutOfRangeException(String.Format("Type Number: {0} not recognized", typeNum));
            }
        }