Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No BOM for serialized output #54

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/NServiceBus.Newtonsoft.Json/JsonMessageSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class JsonMessageSerializer : IMessageSerializer
Func<Stream, JsonReader> readerCreator;
Func<Stream, JsonWriter> writerCreator;
NewtonSerializer jsonSerializer;
static readonly Encoding utf8NoBOM = new UTF8Encoding(false);
jbogard marked this conversation as resolved.
Show resolved Hide resolved

public JsonMessageSerializer(
IMessageMapper messageMapper,
Expand All @@ -33,7 +34,7 @@ public JsonMessageSerializer(

this.writerCreator = writerCreator ?? (stream =>
{
var streamWriter = new StreamWriter(stream, Encoding.UTF8);
var streamWriter = new StreamWriter(stream, utf8NoBOM);
return new JsonTextWriter(streamWriter)
{
Formatting = Formatting.None
Expand Down
37 changes: 37 additions & 0 deletions src/Tests/No_UTF8_BOM.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using System.IO;
using System.Text;
using NServiceBus.MessageInterfaces.MessageMapper.Reflection;
using NServiceBus.Newtonsoft.Json;
using NUnit.Framework;

[TestFixture]
public class No_UTF8_BOM
jbogard marked this conversation as resolved.
Show resolved Hide resolved
{

[Test]
public void Run()
jbogard marked this conversation as resolved.
Show resolved Hide resolved
{
var messageMapper = new MessageMapper();
var serializer = new JsonMessageSerializer(messageMapper, null, null, null, null);
var message = new SimpleMessage();
using (var stream = new MemoryStream())
{
serializer.Serialize(message, stream);

stream.Position = 0;

var result = stream.ToArray();
var utf8bom = new UTF8Encoding(true).GetPreamble();

for (var i = 0; i < utf8bom.Length; i++)
{
Assert.AreNotEqual(utf8bom[i], result[i]);
}
}
}
public class SimpleMessage
{
public string SomeProperty { get; set; }
}

}
33 changes: 33 additions & 0 deletions src/Tests/With_UTF8_BOM.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System.IO;
using System.Text;
using NServiceBus.MessageInterfaces.MessageMapper.Reflection;
using NServiceBus.Newtonsoft.Json;
using NUnit.Framework;

[TestFixture]
public class With_UTF8_BOM
jbogard marked this conversation as resolved.
Show resolved Hide resolved
{

[Test]
public void Run()
jbogard marked this conversation as resolved.
Show resolved Hide resolved
{
var messageMapper = new MessageMapper();
var serializer = new JsonMessageSerializer(messageMapper, null, null, null, null);

var serialized = new UTF8Encoding(true).GetBytes($"{{\"{nameof(SimpleMessage.SomeProperty)}\":\"John\"}}");
jbogard marked this conversation as resolved.
Show resolved Hide resolved

using (var stream = new MemoryStream(serialized))
{
stream.Position = 0;

var result = (SimpleMessage)serializer.Deserialize(stream, new[] { typeof(SimpleMessage) })[0];

Assert.AreEqual("John", result.SomeProperty);
}
}
public class SimpleMessage
{
public string SomeProperty { get; set; }
}

}