Example #1
0
        private static void WriteElement(ByteWriter writer, string key, BsonValue value)
        {
            // cast RawValue to avoid one if on As<Type>
            switch (value.Type)
            {
            case BsonType.Double:
                writer.Write((byte)0x01);
                WriteCString(writer, key);
                writer.Write((Double)value.RawValue);
                break;

            case BsonType.String:
                writer.Write((byte)0x02);
                WriteCString(writer, key);
                WriteString(writer, (String)value.RawValue);
                break;

            case BsonType.Document:
                writer.Write((byte)0x03);
                WriteCString(writer, key);
                WriteDocument(writer, (BsonDocument)value);
                break;

            case BsonType.Array:
                writer.Write((byte)0x04);
                WriteCString(writer, key);
                WriteArray(writer, new BsonArray((List <BsonValue>)value.RawValue));
                break;

            case BsonType.Binary:
                writer.Write((byte)0x05);
                WriteCString(writer, key);
                var bytes = (byte[])value.RawValue;
                writer.Write(bytes.Length);
                writer.Write((byte)0x00);     // subtype 00 - Generic binary subtype
                writer.Write(bytes);
                break;

            case BsonType.Guid:
                writer.Write((byte)0x05);
                WriteCString(writer, key);
                var guid = ((Guid)value.RawValue).ToByteArray();
                writer.Write(guid.Length);
                writer.Write((byte)0x04);     // UUID
                writer.Write(guid);
                break;

            case BsonType.ObjectId:
                writer.Write((byte)0x07);
                WriteCString(writer, key);
                writer.Write(((ObjectId)value.RawValue).ToByteArray());
                break;

            case BsonType.Boolean:
                writer.Write((byte)0x08);
                WriteCString(writer, key);
                writer.Write((byte)(((Boolean)value.RawValue) ? 0x01 : 0x00));
                break;

            case BsonType.DateTime:
                writer.Write((byte)0x09);
                WriteCString(writer, key);
                var date = (DateTime)value.RawValue;
                // do not convert to UTC min/max date values - #19
                var utc = (date == DateTime.MinValue || date == DateTime.MaxValue) ? date : date.ToUniversalTime();
                var ts  = utc - BsonValue.UnixEpoch;
                writer.Write(Convert.ToInt64(ts.TotalMilliseconds));
                break;

            case BsonType.Null:
                writer.Write((byte)0x0A);
                WriteCString(writer, key);
                break;

            case BsonType.Int32:
                writer.Write((byte)0x10);
                WriteCString(writer, key);
                writer.Write((Int32)value.RawValue);
                break;

            case BsonType.Int64:
                writer.Write((byte)0x12);
                WriteCString(writer, key);
                writer.Write((Int64)value.RawValue);
                break;

            case BsonType.Decimal:
                writer.Write((byte)0x13);
                WriteCString(writer, key);
                writer.Write((Decimal)value.RawValue);
                break;

            case BsonType.MinValue:
                writer.Write((byte)0xFF);
                WriteCString(writer, key);
                break;

            case BsonType.MaxValue:
                writer.Write((byte)0x7F);
                WriteCString(writer, key);
                break;
            }
        }