public static BSONDocument ValueOf(object val) { if (val == null) { return(new BSONDocument()); } Type vtype = val.GetType(); if (val is BSONDocument) { return((BSONDocument)val); } else if (vtype == typeof(byte[])) { return(new BSONDocument((byte[])val)); } else if (vtype.IsAnonymousType()) { BSONDocument doc = new BSONDocument(); SetAnonType(doc, null, val); return(doc); } throw new InvalidCastException(string.Format("Unsupported cast type: {0}", vtype)); }
public BSONIterator(BSONDocument doc) : this(doc.ToByteArray()) { }
public BSONValue FetchCurrentValue() { CheckDisposed(); if (_entryDataSkipped) { return(_entryDataValue); } _entryDataSkipped = true; switch (_ctype) { case BSONType.EOO: case BSONType.UNDEFINED: case BSONType.NULL: case BSONType.MAXKEY: case BSONType.MINKEY: _entryDataValue = new BSONValue(_ctype, _entryKey); break; case BSONType.OID: Debug.Assert(_entryLen == 12); _entryDataValue = new BSONValue(_ctype, _entryKey, new BSONOid(_input)); break; case BSONType.STRING: case BSONType.CODE: case BSONType.SYMBOL: { Debug.Assert(_entryLen - 1 >= 0); string sv = Encoding.UTF8.GetString(_input.ReadBytes(_entryLen - 1)); _entryDataValue = new BSONValue(_ctype, _entryKey, sv); var rb = _input.ReadByte(); Debug.Assert(rb == 0x00); //trailing zero byte break; } case BSONType.BOOL: _entryDataValue = new BSONValue(_ctype, _entryKey, _input.ReadBoolean()); break; case BSONType.INT: _entryDataValue = new BSONValue(_ctype, _entryKey, _input.ReadInt32()); break; case BSONType.OBJECT: case BSONType.ARRAY: { BSONDocument doc = (_ctype == BSONType.OBJECT ? new BSONDocument() : new BSONArray()); BSONIterator sit = new BSONIterator(this); while (sit.Next() != BSONType.EOO) { doc.Add(sit.FetchCurrentValue()); } _entryDataValue = new BSONValue(_ctype, _entryKey, doc); break; } case BSONType.DOUBLE: _entryDataValue = new BSONValue(_ctype, _entryKey, _input.ReadDouble()); break; case BSONType.LONG: _entryDataValue = new BSONValue(_ctype, _entryKey, _input.ReadInt64()); break; case BSONType.DATE: _entryDataValue = new BSONValue(_ctype, _entryKey, BSONConstants.Epoch.AddMilliseconds(_input.ReadInt64())); break; case BSONType.TIMESTAMP: { int inc = _input.ReadInt32(); int ts = _input.ReadInt32(); _entryDataValue = new BSONValue(_ctype, _entryKey, new BSONTimestamp(inc, ts)); break; } case BSONType.REGEX: { string re = _input.ReadCString(); string opts = _input.ReadCString(); _entryDataValue = new BSONValue(_ctype, _entryKey, new BSONRegexp(re, opts)); break; } case BSONType.BINDATA: { byte subtype = _input.ReadByte(); BSONBinData bd = new BSONBinData(subtype, _entryLen - 1, _input); _entryDataValue = new BSONValue(_ctype, _entryKey, bd); break; } case BSONType.DBREF: { //Unsupported DBREF! SkipData(true); _entryDataValue = new BSONValue(_ctype, _entryKey); break; } case BSONType.CODEWSCOPE: { int cwlen = _entryLen + 4; Debug.Assert(cwlen > 5); int clen = _input.ReadInt32(); //code length string code = Encoding.UTF8.GetString(_input.ReadBytes(clen)); BSONCodeWScope cw = new BSONCodeWScope(code); BSONIterator sit = new BSONIterator(_input, _input.ReadInt32()); while (sit.Next() != BSONType.EOO) { cw.Add(sit.FetchCurrentValue()); } _entryDataValue = new BSONValue(_ctype, _entryKey, cw); break; } } return(_entryDataValue); }
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); } }
public BSONDocument SetDocument(string key, BSONDocument val) { return(SetBSONValue(new BSONValue(BSONType.OBJECT, key, val))); }
public BSONDocument(BSONIterator it, string[] fields) : this() { Array.Sort(fields); BSONType bt; int ind = -1; int nfc = 0; foreach (string f in fields) { if (f != null) { nfc++; } } while ((bt = it.Next()) != BSONType.EOO) { if (nfc < 1) { continue; } string kk = it.CurrentKey; if ((ind = Array.IndexOf(fields, kk)) != -1) { Add(it.FetchCurrentValue()); fields[ind] = null; nfc--; } else if (bt == BSONType.OBJECT || bt == BSONType.ARRAY) { string[] narr = null; for (var i = 0; i < fields.Length; ++i) { var f = fields[i]; if (f == null) { continue; } if (f.IndexOf(kk, StringComparison.Ordinal) == 0 && f.Length > kk.Length + 1 && f[kk.Length] == '.') { if (narr == null) { narr = new string[fields.Length]; } narr[i] = f.Substring(kk.Length + 1); fields[i] = null; nfc--; } } if (narr != null) { BSONIterator nit = new BSONIterator(it); BSONDocument ndoc = new BSONDocument(nit, narr); if (ndoc.KeysCount > 0) { Add(new BSONValue(bt, kk, ndoc)); } } } } it.Dispose(); }
public BSONDocument SetObject(int idx, BSONDocument val) { return(base.SetDocument(idx.ToString(), val)); }