Пример #1
0
 public static BsonType Create(BsonDataType type)
 {
     BsonType ret = null;
     if(type == BsonDataType.Number){
         ret = new BsonNumber();
     }else if(type == BsonDataType.Number){
         throw new NotImplementedException();
     }else if(type == BsonDataType.String){
         ret = new BsonString();
     }else if(type == BsonDataType.Obj){
         ret = new BsonDocument();
     }else if(type == BsonDataType.Array){
         ret = new BsonArray();
     }else if(type == BsonDataType.Integer){
         ret = new BsonInteger();
     }else if(type == BsonDataType.Long){
         ret = new BsonLong();
     }else if(type == BsonDataType.Boolean){
         ret = new BsonBoolean();
     }else if(type == BsonDataType.Oid){
         ret = new BsonOid();
     }else if(type == BsonDataType.Date){
         ret = new BsonDate();
     }else if(type == BsonDataType.Regex){
         ret = new BsonRegex();
     }else{
         throw new ArgumentOutOfRangeException("Type: " + type + " not recognized");
     }
     return ret;
 }
Пример #2
0
        public void Write(Document doc)
        {
            int size = CalculateSize(doc);

            writer.Write(size);
            foreach (String key in doc.Keys)
            {
                Object       val = doc[key];
                BsonDataType t   = TranslateToBsonType(val);
                writer.Write((byte)t);
                this.WriteString(key);
                this.WriteValue(t, val);
            }
            writer.Write((byte)0);
        }
Пример #3
0
        public void WriteArray(IEnumerable arr)
        {
            int size = CalculateSize(arr);

            writer.Write(size);
            int keyname = 0;

            foreach (Object val in arr)
            {
                BsonDataType t = TranslateToBsonType(val);
                writer.Write((byte)t);
                this.WriteString(keyname.ToString());
                this.WriteValue(t, val);
                keyname++;
            }
            writer.Write((byte)0);
        }
Пример #4
0
 public static BsonType Create(BsonDataType type)
 {
     BsonType ret = null;
     if(type == BsonDataType.Number){
         ret = new BsonNumber();
     }else if(type == BsonDataType.Number){
         throw new NotImplementedException();
     }else if(type == BsonDataType.String){
         ret = new BsonString();
     }else if(type == BsonDataType.Obj){
         ret = new BsonDocument();
     }else if(type == BsonDataType.Array){
         ret = new BsonArray();
     }else if(type == BsonDataType.Integer){
         ret = new BsonInteger();
     }else if(type == BsonDataType.Long){
         ret = new BsonLong();
     }else if(type == BsonDataType.Boolean){
         ret = new BsonBoolean();
     }else if(type == BsonDataType.Oid){
         ret = new BsonOid();
     }else if(type == BsonDataType.Date){
         ret = new BsonDate();
     }else if(type == BsonDataType.Regex){
         ret = new BsonRegex();
     }else if(type == BsonDataType.Undefined){
         ret = new BsonUndefined();
     }else if(type == BsonDataType.Null){
         ret = new BsonNull();
     }else if(type == BsonDataType.Code){
         ret = new BsonCode();
     }else if(type == BsonDataType.CodeWScope){
         ret = new BsonCodeWScope();
     }else{
         string typename = Enum.GetName(typeof(BsonDataType), type);
         throw new ArgumentOutOfRangeException("type",typename + "is not recognized");
     }
     return ret;
 }
Пример #5
0
 public void WriteValue(BsonDataType dt, Object obj)
 {
     switch (dt){
         case BsonDataType.MinKey:
         case BsonDataType.MaxKey:
         case BsonDataType.Null:
             return;
         case BsonDataType.Boolean:
             writer.Write((bool)obj);
             return;
         case BsonDataType.Integer:
             writer.Write((int)obj);
             return;
         case BsonDataType.Long:
             writer.Write((long)obj);
             return;
         case BsonDataType.Date:
             DateTime d = (DateTime)obj;
             TimeSpan diff = d.ToUniversalTime() - BsonInfo.Epoch;
             double time = Math.Floor(diff.TotalMilliseconds);
             writer.Write((long)time);
             return;
         case BsonDataType.Oid:
             Oid id = (Oid) obj;
             writer.Write(id.ToByteArray());
             return;
         case BsonDataType.Number:
             writer.Write((double)obj);
             return;
         case BsonDataType.String:{
             String str = (String)obj;
             writer.Write(CalculateSize(str,false));
             this.WriteString(str);
             return;
             }
         case BsonDataType.Obj:
             if(obj is Document){
                 this.Write((Document)obj);
             }else if(obj is DBRef){
                 this.Write((Document)((DBRef)obj));
             }
             return;
         case BsonDataType.Array:
             this.WriteArray((IEnumerable)obj);
             return;
         case BsonDataType.Regex:{
             MongoRegex r = (MongoRegex)obj;
             this.WriteString(r.Expression);
             this.WriteString(r.Options);
             return;
         }
         case BsonDataType.Code:{
             Code c = (Code)obj;
             this.WriteValue(BsonDataType.String,c.Value);
             return;
         }
         case BsonDataType.CodeWScope:{
             CodeWScope cw = (CodeWScope)obj;
             writer.Write(CalculateSize(cw));
             this.WriteValue(BsonDataType.String,cw.Value);
             this.WriteValue(BsonDataType.Obj,cw.Scope);
             return;
         }
         case BsonDataType.Binary:{
             if (obj is Guid) {
                 writer.Write((int)16);
                 writer.Write((byte)3);
                 writer.Write(((Guid)obj).ToByteArray());
             } else {
                 Binary b = (Binary)obj;
                 if(b.Subtype == Binary.TypeCode.General){
                     writer.Write(b.Bytes.Length + 4);
                     writer.Write((byte)b.Subtype);
                     writer.Write(b.Bytes.Length);
                 }else{
                     writer.Write(b.Bytes.Length);
                     writer.Write((byte)b.Subtype);
                 }
                 writer.Write(b.Bytes);
             }
             return;
         }
         default:
             throw new NotImplementedException(String.Format("Writing {0} types not implemented.",obj.GetType().Name));
     }
 }
Пример #6
0
        public void WriteValue(BsonDataType dt, Object obj)
        {
            switch (dt)
            {
            case BsonDataType.MinKey:
            case BsonDataType.MaxKey:
            case BsonDataType.Null:
                return;

            case BsonDataType.Boolean:
                writer.Write((bool)obj);
                return;

            case BsonDataType.Integer:
                writer.Write((int)obj);
                return;

            case BsonDataType.Long:
                writer.Write((long)obj);
                return;

            case BsonDataType.Date:
                DateTime d    = (DateTime)obj;
                TimeSpan diff = d.ToUniversalTime() - epoch;
                double   time = Math.Floor(diff.TotalMilliseconds);
                writer.Write((long)time);
                return;

            case BsonDataType.Oid:
                Oid id = (Oid)obj;
                writer.Write(id.Value);
                return;

            case BsonDataType.Number:
                writer.Write((double)obj);
                return;

            case BsonDataType.String: {
                String str = (String)obj;
                writer.Write(CalculateSize(str, false));
                this.WriteString(str);
                return;
            }

            case BsonDataType.Obj:
                if (obj is Document)
                {
                    this.Write((Document)obj);
                }
                else if (obj is DBRef)
                {
                    this.Write((Document)((DBRef)obj));
                }
                return;

            case BsonDataType.Array:
                this.WriteArray((IEnumerable)obj);
                return;

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

            case BsonDataType.Code: {
                Code c = (Code)obj;
                this.WriteValue(BsonDataType.String, c.Value);
                return;
            }

            case BsonDataType.CodeWScope: {
                CodeWScope cw = (CodeWScope)obj;
                writer.Write(CalculateSize(cw));
                this.WriteValue(BsonDataType.String, cw.Value);
                this.WriteValue(BsonDataType.Obj, cw.Scope);
                return;
            }

            case BsonDataType.Binary: {
                if (obj is Guid)
                {
                    writer.Write((int)16);
                    writer.Write((byte)3);
                    writer.Write(((Guid)obj).ToByteArray());
                }
                else
                {
                    Binary b = (Binary)obj;
                    if (b.Subtype == Binary.TypeCode.General)
                    {
                        writer.Write(b.Bytes.Length + 4);
                        writer.Write((byte)b.Subtype);
                        writer.Write(b.Bytes.Length);
                    }
                    else
                    {
                        writer.Write(b.Bytes.Length);
                        writer.Write((byte)b.Subtype);
                    }
                    writer.Write(b.Bytes);
                }
                return;
            }

            default:
                throw new NotImplementedException(String.Format("Writing {0} types not implemented.", obj.GetType().Name));
            }
        }