# Newtonsoft BSON Serializer This sample uses the Newtonsoft serializer [NServiceBus.Newtonsoft.Json](https://github.com/Particular/NServiceBus.Newtonsoft.Json) and configures it to use [BSON](http://bsonspec.org/). ## Configuration ```cs var serialization = endpointConfiguration.UseSerialization(); serialization.ContentTypeKey("application/bson"); serialization.ReaderCreator(stream => new BsonDataReader(stream)); serialization.WriterCreator(stream => new BsonDataWriter(stream)); ``` ## Diagnostic mutator This helper will write out the contents of any incoming message so that the serialization can be observed. ```cs public class MessageBodyWriter(ILogger logger) : IMutateIncomingTransportMessages { public Task MutateIncoming(MutateIncomingTransportMessageContext context) { var bodyAsString = Encoding.UTF8 .GetString(context.Body.ToArray()); logger.LogInformation ("Serialized Message Body:"); logger.LogInformation(bodyAsString); return Task.CompletedTask; } } ``` This registers the mutator: ```cs builder.Services.AddSingleton(); // Then later get it from the service provider when needed var serviceProvider = builder.Services.BuildServiceProvider(); var messageBodyWriter = serviceProvider.GetRequiredService(); endpointConfiguration.RegisterMessageMutator(messageBodyWriter); ``` ## Sending a message ```cs var message = new CreateOrder { OrderId = 9, Date = DateTime.Now, CustomerId = 12, OrderItems = [ new OrderItem { ItemId = 6, Quantity = 2 }, new OrderItem { ItemId = 5, Quantity = 4 } ] }; await messageSession.SendLocal(message); ```