private void WriteNested(BsonWriterAdapter adapter, Action writer) { adapter.WriteStartObject(); adapter.WritePropertyName("x"); writer(); adapter.WriteEndObject(); }
public void constructor_should_initialize_instance() { var wrappedWriter = Substitute.For <IBsonWriter>(); var result = new BsonWriterAdapter(wrappedWriter); result.WrappedWriter.Should().BeSameAs(wrappedWriter); }
public void WrappedWriter_get_should_return_expected_result() { var wrappedWriter = Substitute.For <IBsonWriter>(); var subject = new BsonWriterAdapter(wrappedWriter); var result = subject.WrappedWriter; result.Should().BeSameAs(wrappedWriter); }
public void Flush_should_flush_wrapped_writer() { var wrappedWriter = Substitute.For <IBsonWriter>(); var subject = new BsonWriterAdapter(wrappedWriter); subject.Flush(); wrappedWriter.Received(1).Flush(); }
protected string WriteJsonUsingWrappedJsonWriter(Newtonsoft.Json.JsonConverter converter, object value) { using (var stringWriter = new StringWriter()) using (var wrappedWriter = new JsonWriter(stringWriter)) using (var writer = new BsonWriterAdapter(wrappedWriter)) { WriteJson(converter, value, writer); return(stringWriter.ToString()); } }
public void Close_should_close_wrapped_writer_when_CloseOutput_is_true(bool closeOutput) { var wrappedWriter = Substitute.For <IBsonWriter>(); var subject = new BsonWriterAdapter(wrappedWriter); subject.CloseOutput = closeOutput; subject.Close(); wrappedWriter.Received(closeOutput ? 1 : 0).Close(); }
protected byte[] WriteJsonUsingWrappedBsonWriter(Newtonsoft.Json.JsonConverter converter, object value, bool mustBeNested = false, GuidRepresentation guidRepresentation = GuidRepresentation.CSharpLegacy) { var wrappedWriterSettings = new BsonBinaryWriterSettings { GuidRepresentation = guidRepresentation }; using (var stream = new MemoryStream()) using (var wrappedWriter = new BsonBinaryWriter(stream, wrappedWriterSettings)) using (var writer = new BsonWriterAdapter(wrappedWriter)) { WriteJson(converter, value, writer, mustBeNested); return(stream.ToArray()); } }
protected byte[] SerializeUsingNewtonsoftWriter <T>(T value, bool mustBeNested = false, GuidRepresentation guidRepresentation = GuidRepresentation.Unspecified) { using (var memoryStream = new MemoryStream()) /* * * The Newtonsoft.Json.Bson.BsonDataWriter cannot be used for Guids with `GuidRepresentation.CSharpLegacy` * because it will always write Guids with with BinaryData subtype `0x04` that map to `BsonBinaryType.Uuid`. * `BsonBinaryType.OldUuid` would write the required BinaryData subtype (`0x03`), but there is no way to * force its use through the public API surface (the subtype it is actually hardcoded to `BsonBinaryType.Uuid` * in the JsonNet codebase). * * The MongoDb BSON writer instead use BsonBinarySubType.UuidLegacy (`0x03`) when the type * is `GuidRepresentation.CSharpLegacy`. * The BinaryData subtype `0x04` map to `BsonBinarySubType.UuidStandard`, but it cannot be used * because then the `Guid` bytes are written in network order (BigEndian) introducting more differences. * * This code has been modified to use the `BsonWriterAdapter` and to verify that Guids roundtrip * correctly when using the adapter. * */ //using (var newtonsoftWriter = new Newtonsoft.Json.Bson.BsonDataWriter(memoryStream)) using (var writer = new BsonBinaryWriter(memoryStream, new BsonBinaryWriterSettings { GuidRepresentation = GuidRepresentation.CSharpLegacy })) using (var newtonsoftWriter = new BsonWriterAdapter(writer)) // { if (mustBeNested) { newtonsoftWriter.WriteStartObject(); newtonsoftWriter.WritePropertyName("x"); } var newtonsoftSerializer = new Newtonsoft.Json.JsonSerializer(); newtonsoftSerializer.Serialize(newtonsoftWriter, value); if (mustBeNested) { newtonsoftWriter.WriteEndObject(); } return(memoryStream.ToArray()); } }
// private methods private void AssertBsonEquals(BsonWriterAdapter adapter, string json) { var wrappedWriter = (BsonBinaryWriter)adapter.WrappedWriter; var stream = (MemoryStream)wrappedWriter.BaseStream; var bson = stream.ToArray(); byte[] expectedBson; using (var bsonReader = new JsonReader(json, new JsonReaderSettings { GuidRepresentation = GuidRepresentation.Unspecified })) { var context = BsonDeserializationContext.CreateRoot(bsonReader); var document = BsonDocumentSerializer.Instance.Deserialize(context); expectedBson = document.ToBson(writerSettings: new BsonBinaryWriterSettings { GuidRepresentation = GuidRepresentation.Unspecified }); } bson.Should().Equal(expectedBson); }