Пример #1
0
 public BSONDocument SetOID(string key, BSONOid oid)
 {
     return(SetBSONValue(new BSONValue(BSONType.OID, key, oid)));
 }
Пример #2
0
        protected void WriteBSONValue(BSONValue bv, ExtBinaryWriter bw)
        {
            BSONType bt = bv.BSONType;

            switch (bt)
            {
            case BSONType.EOO:
                break;

            case BSONType.NULL:
            case BSONType.UNDEFINED:
            case BSONType.MAXKEY:
            case BSONType.MINKEY:
                WriteTypeAndKey(bv, bw);
                break;

            case BSONType.OID:
            {
                WriteTypeAndKey(bv, bw);
                BSONOid oid = (BSONOid)bv.Value;
                Debug.Assert(oid._bytes.Length == 12);
                bw.Write(oid._bytes);
                break;
            }

            case BSONType.STRING:
            case BSONType.CODE:
            case BSONType.SYMBOL:
                WriteTypeAndKey(bv, bw);
                bw.WriteBSONString((string)bv.Value);
                break;

            case BSONType.BOOL:
                WriteTypeAndKey(bv, bw);
                bw.Write((bool)bv.Value);
                break;

            case BSONType.INT:
                WriteTypeAndKey(bv, bw);
                bw.Write((int)bv.Value);
                break;

            case BSONType.LONG:
                WriteTypeAndKey(bv, bw);
                bw.Write((long)bv.Value);
                break;

            case BSONType.ARRAY:
            case BSONType.OBJECT:
            {
                BSONDocument doc = (BSONDocument)bv.Value;
                WriteTypeAndKey(bv, bw);
                doc.Serialize(bw.BaseStream);
                break;
            }

            case BSONType.DATE:
            {
                DateTime dt   = (DateTime)bv.Value;
                var      diff = dt.ToLocalTime() - BSONConstants.Epoch;
                long     time = (long)Math.Floor(diff.TotalMilliseconds);
                WriteTypeAndKey(bv, bw);
                bw.Write(time);
                break;
            }

            case BSONType.DOUBLE:
                WriteTypeAndKey(bv, bw);
                bw.Write((double)bv.Value);
                break;

            case BSONType.REGEX:
            {
                BSONRegexp rv = (BSONRegexp)bv.Value;
                WriteTypeAndKey(bv, bw);
                bw.WriteCString(rv.Re ?? "");
                bw.WriteCString(rv.Opts ?? "");
                break;
            }

            case BSONType.BINDATA:
            {
                BSONBinData bdata = (BSONBinData)bv.Value;
                WriteTypeAndKey(bv, bw);
                bw.Write(bdata.Data.Length);
                bw.Write(bdata.Subtype);
                bw.Write(bdata.Data);
                break;
            }

            case BSONType.DBREF:
                //Unsupported DBREF!
                break;

            case BSONType.TIMESTAMP:
            {
                BSONTimestamp ts = (BSONTimestamp)bv.Value;
                WriteTypeAndKey(bv, bw);
                bw.Write(ts.Inc);
                bw.Write(ts.Ts);
                break;
            }

            case BSONType.CODEWSCOPE:
            {
                BSONCodeWScope cw = (BSONCodeWScope)bv.Value;
                WriteTypeAndKey(bv, bw);
                using (var cwwr = new ExtBinaryWriter(new MemoryStream())) {
                    cwwr.WriteBSONString(cw.Code);
                    cw.Scope.Serialize(cwwr.BaseStream);
                    byte[] cwdata = ((MemoryStream)cwwr.BaseStream).ToArray();
                    bw.Write(cwdata.Length);
                    bw.Write(cwdata);
                }
                break;
            }

            default:
                throw new InvalidBSONDataException("Unknown entry type: " + bt);
            }
        }
Пример #3
0
 public BSONDocument SetOID(int idx, BSONOid oid)
 {
     return(base.SetOID(idx.ToString(), oid));
 }