Skip to content

Commit

Permalink
Stop using Json.NET default serialiser settings
Browse files Browse the repository at this point in the history
  • Loading branch information
mattwcole committed Jan 10, 2019
1 parent d9d47d3 commit dc47c88
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 5 deletions.
11 changes: 6 additions & 5 deletions src/Gelf.Extensions.Logging/GelfMessageExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@ private static bool IsNumeric(object value)

public static string ToJson(this GelfMessage message)
{
var messageJson = JObject.FromObject(message);
var messageJson = JObject.FromObject(message, new JsonSerializer
{
NullValueHandling = NullValueHandling.Ignore,
Formatting = Formatting.None
});

foreach (var field in message.AdditionalFields)
{
Expand All @@ -36,10 +40,7 @@ public static string ToJson(this GelfMessage message)
}
}

return JsonConvert.SerializeObject(messageJson, Formatting.None, new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore
});
return messageJson.ToString(Formatting.None);
}
}
}
35 changes: 35 additions & 0 deletions test/Gelf.Extensions.Logging.Tests/GelfMessageExtensionsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System;
using System.Collections.Generic;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Xunit;

namespace Gelf.Extensions.Logging.Tests
{
public class GelfMessageExtensionsTests
{
[Fact]
public void Serialises_to_JSON_string_with_correct_settings()
{
JsonConvert.DefaultSettings = () => new JsonSerializerSettings
{
Converters =
{
new StringEnumConverter()
}
};

var message = new GelfMessage
{
Level = SyslogSeverity.Emergency,
AdditionalFields = new Dictionary<string, object>()
};

var messageJson = message.ToJson();

Assert.DoesNotContain("Emergency", messageJson);
Assert.DoesNotContain(Environment.NewLine, messageJson);
Assert.DoesNotContain("null", messageJson);
}
}
}

0 comments on commit dc47c88

Please sign in to comment.