/// <summary> /// Writes a raw BSON array. /// </summary> /// <param name="slice">The byte buffer containing the raw BSON array.</param> public virtual void WriteRawBsonArray(IByteBuffer slice) { // overridden in BsonBinaryWriter to write the raw bytes to the stream // for all other streams, deserialize the raw bytes and serialize the resulting array instead var documentLength = slice.Length + 8; using (var memoryStream = new MemoryStream(documentLength)) { // wrap the array in a fake document so we can deserialize it var streamWriter = new BsonStreamWriter(memoryStream, Utf8Helper.StrictUtf8Encoding); streamWriter.WriteInt32(documentLength); streamWriter.WriteBsonType(BsonType.Array); streamWriter.WriteByte((byte)'x'); streamWriter.WriteByte(0); slice.WriteTo(streamWriter.BaseStream); streamWriter.WriteByte(0); memoryStream.Position = 0; using (var bsonReader = new BsonBinaryReader(memoryStream, BsonBinaryReaderSettings.Defaults)) { var deserializationContext = BsonDeserializationContext.CreateRoot <BsonDocument>(bsonReader); bsonReader.ReadStartDocument(); bsonReader.ReadName("x"); var array = deserializationContext.DeserializeWithChildContext(BsonArraySerializer.Instance); bsonReader.ReadEndDocument(); var serializationContext = BsonSerializationContext.CreateRoot <BsonArray>(this); BsonArraySerializer.Instance.Serialize(serializationContext, array); } } }
/// <summary> /// Writes a raw BSON array. /// </summary> /// <param name="slice">The byte buffer containing the raw BSON array.</param> public virtual void WriteRawBsonArray(IByteBuffer slice) { // overridden in BsonBinaryWriter to write the raw bytes to the stream // for all other streams, deserialize the raw bytes and serialize the resulting array instead var documentLength = slice.Length + 8; using (var memoryStream = new MemoryStream(documentLength)) { // wrap the array in a fake document so we can deserialize it var streamWriter = new BsonStreamWriter(memoryStream, Utf8Helper.StrictUtf8Encoding); streamWriter.WriteInt32(documentLength); streamWriter.WriteBsonType(BsonType.Array); streamWriter.WriteByte((byte)'x'); streamWriter.WriteByte(0); slice.WriteTo(streamWriter.BaseStream); streamWriter.WriteByte(0); memoryStream.Position = 0; using (var bsonReader = new BsonBinaryReader(memoryStream, BsonBinaryReaderSettings.Defaults)) { var deserializationContext = BsonDeserializationContext.CreateRoot<BsonDocument>(bsonReader); bsonReader.ReadStartDocument(); bsonReader.ReadName("x"); var array = deserializationContext.DeserializeWithChildContext(BsonArraySerializer.Instance); bsonReader.ReadEndDocument(); var serializationContext = BsonSerializationContext.CreateRoot<BsonArray>(this); BsonArraySerializer.Instance.Serialize(serializationContext, array); } } }
#pragma warning disable 618 // about obsolete BsonBinarySubType.OldBinary /// <summary> /// Writes BSON binary data to the writer. /// </summary> /// <param name="binaryData">The binary data.</param> public override void WriteBinaryData(BsonBinaryData binaryData) { if (Disposed) { throw new ObjectDisposedException("BsonBinaryWriter"); } if (State != BsonWriterState.Value) { ThrowInvalidState("WriteBinaryData", BsonWriterState.Value); } var bytes = binaryData.Bytes; var subType = binaryData.SubType; var guidRepresentation = binaryData.GuidRepresentation; switch (subType) { case BsonBinarySubType.OldBinary: if (_settings.FixOldBinarySubTypeOnOutput) { subType = BsonBinarySubType.Binary; // replace obsolete OldBinary with new Binary sub type } break; case BsonBinarySubType.UuidLegacy: case BsonBinarySubType.UuidStandard: if (_settings.GuidRepresentation != GuidRepresentation.Unspecified) { var expectedSubType = (_settings.GuidRepresentation == GuidRepresentation.Standard) ? BsonBinarySubType.UuidStandard : BsonBinarySubType.UuidLegacy; if (subType != expectedSubType) { var message = string.Format( "The GuidRepresentation for the writer is {0}, which requires the subType argument to be {1}, not {2}.", _settings.GuidRepresentation, expectedSubType, subType); throw new BsonSerializationException(message); } if (guidRepresentation != _settings.GuidRepresentation) { var message = string.Format( "The GuidRepresentation for the writer is {0}, which requires the the guidRepresentation argument to also be {0}, not {1}.", _settings.GuidRepresentation, guidRepresentation); throw new BsonSerializationException(message); } } break; } _streamWriter.WriteBsonType(BsonType.Binary); WriteNameHelper(); if (subType == BsonBinarySubType.OldBinary) { // sub type OldBinary has two sizes (for historical reasons) _streamWriter.WriteInt32(bytes.Length + 4); _streamWriter.WriteByte((byte)subType); _streamWriter.WriteInt32(bytes.Length); } else { _streamWriter.WriteInt32(bytes.Length); _streamWriter.WriteByte((byte)subType); } _streamWriter.WriteBytes(bytes); State = GetNextState(); }