Skip to content
This repository has been archived by the owner on Aug 30, 2023. It is now read-only.

Commit

Permalink
Added json attributes and refactored in Enum string converter seriali…
Browse files Browse the repository at this point in the history
…zation.
  • Loading branch information
samukce committed Jul 6, 2016
1 parent 40c8c56 commit df1859d
Show file tree
Hide file tree
Showing 8 changed files with 102 additions and 17 deletions.
10 changes: 10 additions & 0 deletions src/app/SharpRaven/Data/BreadcrumbsLevel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace SharpRaven.Data
{
/// <summary>
/// This defines the level of the event. If not provided it defaults to info which is the middle level. In the order of priority from highest to lowest the levels are critical, error, warning, info and debug. Levels are used in the UI to emphasize and deemphasize the crumb.
/// </summary>
public enum BreadcrumbsLevel
{
Critical, Error, Warning, Info, Debug
}
}
27 changes: 20 additions & 7 deletions src/app/SharpRaven/Data/BreadcrumbsRecord.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
using System;
using System.Collections.Generic;

using Newtonsoft.Json;

using SharpRaven.Serialization;

namespace SharpRaven.Data {
/// <summary>
/// BreadcrumbsRecord trail.
/// </summary>
public class BreadcrumbsRecord {
private readonly BreadcrumbsType? breadcrumbsType;
private readonly DateTime timestamp;

public BreadcrumbsRecord() {
Expand All @@ -15,18 +19,27 @@ public BreadcrumbsRecord() {


public BreadcrumbsRecord(BreadcrumbsType type) {
breadcrumbsType = type;
Type = type;
}

[JsonProperty(PropertyName = "type", NullValueHandling = NullValueHandling.Ignore)]
[JsonConverter(typeof(LowerInvariantConverter))]
public BreadcrumbsType? Type { get; }

public BreadcrumbsType? Type {
get { return breadcrumbsType; }
}

[JsonProperty(PropertyName = "category", NullValueHandling = NullValueHandling.Ignore)]
public string Category { get; set; }

public DateTime Timestamp { get { return timestamp; } }
[JsonProperty(PropertyName = "timestamp", NullValueHandling = NullValueHandling.Ignore)]
public DateTime Timestamp => this.timestamp;

[JsonProperty(PropertyName = "message", NullValueHandling = NullValueHandling.Ignore)]
public string Message { get; set; }

[JsonProperty(PropertyName = "data", NullValueHandling = NullValueHandling.Ignore)]
public IDictionary<string, string> Data { get; set; }

[JsonProperty(PropertyName = "level", NullValueHandling = NullValueHandling.Ignore)]
[JsonConverter(typeof(LowerInvariantConverter))]
public BreadcrumbsLevel? Level { get; set; }
}
}
2 changes: 1 addition & 1 deletion src/app/SharpRaven/Data/JsonPacket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ private JsonPacket()
/// Defaults to error.
/// </summary>
[JsonProperty(PropertyName = "level", NullValueHandling = NullValueHandling.Ignore, Required = Required.Always)]
[JsonConverter(typeof(ErrorLevelConverter))]
[JsonConverter(typeof(LowerInvariantConverter))]
public ErrorLevel Level { get; set; }

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@

#endregion

using System;

using Newtonsoft.Json;
using Newtonsoft.Json.Converters;

Expand All @@ -36,9 +38,9 @@
namespace SharpRaven.Serialization
{
/// <summary>
/// Converts <see cref="ErrorLevel"/> to a <see cref="System.String"/>.
/// Converts enum to a <see cref="System.String"/>.ToLowerInvariant.
/// </summary>
public class ErrorLevelConverter : StringEnumConverter
public class LowerInvariantConverter : StringEnumConverter
{
/// <summary>
/// Writes the JSON representation of the object.
Expand All @@ -48,11 +50,13 @@ public class ErrorLevelConverter : StringEnumConverter
/// <param name="serializer">The calling serializer.</param>
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
if (!(value is ErrorLevel))
base.WriteJson(writer, value, serializer);
if (!(value is Enum))
{
return;
}

var level = value.ToString().ToLowerInvariant();
writer.WriteValue(level);
var stringEnum = value.ToString().ToLowerInvariant();
writer.WriteValue(stringEnum);
}
}
}
3 changes: 2 additions & 1 deletion src/app/SharpRaven/SharpRaven.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
<Compile Include="..\..\CommonAssemblyInfo.cs">
<Link>Properties\CommonAssemblyInfo.cs</Link>
</Compile>
<Compile Include="Data\BreadcrumbsLevel.cs" />
<Compile Include="Data\BreadcrumbsType.cs" />
<Compile Include="Data\BreadcrumbsRecord.cs" />
<Compile Include="Data\ExceptionData.cs" />
Expand Down Expand Up @@ -108,7 +109,7 @@
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="RavenClient.Net45.cs" />
<Compile Include="RavenClient.cs" />
<Compile Include="Serialization\ErrorLevelConverter.cs" />
<Compile Include="Serialization\LowerInvariantConverter.cs" />
<Compile Include="Configuration.cs" />
<Compile Include="Utilities\GzipUtil.cs" />
<Compile Include="Utilities\PacketBuilder.cs" />
Expand Down
4 changes: 2 additions & 2 deletions src/tests/SharpRaven.UnitTests/Data/BreadcrumbsRecordTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public void Constructor_BreadcrumbsRecord_CategoryDefaultLog()
public void Constructor_BreadcrumbsRecord_TimestampNow()
{
var now = DateTime.UtcNow;

var breadcrumbsRecord = new BreadcrumbsRecord();

Assert.That(breadcrumbsRecord.Timestamp, Is.GreaterThanOrEqualTo(now));
Expand Down Expand Up @@ -79,8 +80,7 @@ public void Constructor_BreadcrumbsRecord_TypeNull()
[TestCase(" foo message ")]
public void Should_Retain_Espaces_in_Message(string message)
{
var breadcrumbsRecord = new BreadcrumbsRecord();
breadcrumbsRecord.Message = message;
var breadcrumbsRecord = new BreadcrumbsRecord { Message = message };

Assert.That(breadcrumbsRecord.Message, Is.EqualTo(message));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using Newtonsoft.Json;

using NSubstitute;

using NUnit.Framework;

using SharpRaven.Serialization;

namespace SharpRaven.UnitTests.Serialization {
[TestFixture]
public class LowerInvariantConverterTests {
[TestCase(EnumTestable.One, "one")]
[TestCase(EnumTestable.Two, "two")]
[TestCase(EnumTestable.ThreE, "three")]
public void Should_Convert_Enum_To_Lowercase(EnumTestable enumTestable, string result)
{
var lowerInvariantConverter = new LowerInvariantConverter();

var jsonWriter = Substitute.For<JsonWriter>();

lowerInvariantConverter.WriteJson(jsonWriter, enumTestable, Substitute.For<JsonSerializer>());

jsonWriter.Received().WriteValue(result);
}

[Test]
public void Should_Not_Call_Write_Null_value()
{
var lowerInvariantConverter = new LowerInvariantConverter();

var jsonWriter = Substitute.For<JsonWriter>();

lowerInvariantConverter.WriteJson(jsonWriter, null, Substitute.For<JsonSerializer>());

jsonWriter.DidNotReceiveWithAnyArgs().WriteValue("");
}

[Test]
public void Should_Not_Call_Write_If_Not_Enum_Object()
{
var lowerInvariantConverter = new LowerInvariantConverter();

var jsonWriter = Substitute.For<JsonWriter>();

lowerInvariantConverter.WriteJson(jsonWriter, "any string", Substitute.For<JsonSerializer>());

jsonWriter.DidNotReceiveWithAnyArgs().WriteValue("");
}


public enum EnumTestable
{
One, Two, ThreE
}
}
}
1 change: 1 addition & 0 deletions src/tests/SharpRaven.UnitTests/SharpRaven.UnitTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@
<Compile Include="RavenClientTests\CaptureMessageTests.cs" />
<Compile Include="RavenClientTests\CaptureTester.cs" />
<Compile Include="RavenClientTests\RavenClientTests.cs" />
<Compile Include="Serialization\LowerInvariantConverterTests.cs" />
<Compile Include="Utilities\HttpSimulator.cs" />
<Compile Include="Utilities\PacketBuilderTests.cs" />
<Compile Include="Utilities\ReflectionHelper.cs" />
Expand Down

0 comments on commit df1859d

Please sign in to comment.