Skip to content

Commit

Permalink
Fixed issue where Uri/Guid couldn't be serialized
Browse files Browse the repository at this point in the history
  • Loading branch information
phillip-haydon committed Sep 17, 2024
1 parent 594600b commit 14619eb
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 22 deletions.
24 changes: 13 additions & 11 deletions Mindscape.Raygun4Net.Core/SimpleJson.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1020,19 +1020,21 @@ static bool SerializeValue(IJsonSerializerStrategy jsonSerializerStrategy, objec
return false;
}

if (visited.Contains(value))
{
return SerializeString(CYCLIC_MESSAGE, builder);
}

visited.Push(value);

bool success = true;
string stringValue = value as string;
if (stringValue != null)
{
success = SerializeString(stringValue, builder);
}
else
{
if (visited.Contains(value))
{
return SerializeString(CYCLIC_MESSAGE, builder);
}

visited.Push(value);

if (value.GetType().IsArray)
{
success = SerializeArray(jsonSerializerStrategy, value as IEnumerable, builder, visited);
Expand Down Expand Up @@ -1080,11 +1082,11 @@ static bool SerializeValue(IJsonSerializerStrategy jsonSerializerStrategy, objec
}
}
}
}

if (visited.Count > 0) // Just to be safe
{
visited.Pop();
if (visited.Count > 0) // Just to be safe
{
visited.Pop();
}
}

return success;
Expand Down
24 changes: 13 additions & 11 deletions Mindscape.Raygun4Net.NetCore.Common/SimpleJson.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1020,19 +1020,21 @@ static bool SerializeValue(IJsonSerializerStrategy jsonSerializerStrategy, objec
return false;
}

if (visited.Contains(value))
{
return SerializeString(CYCLIC_MESSAGE, builder); ;
}

visited.Push(value);

bool success = true;
string stringValue = value as string;
if (stringValue != null)
{
success = SerializeString(stringValue, builder);
}
else
{
if (visited.Contains(value))
{
return SerializeString(CYCLIC_MESSAGE, builder);
}

visited.Push(value);

if (value.GetType().IsArray)
{
success = SerializeArray(jsonSerializerStrategy, value as IEnumerable, builder, visited);
Expand Down Expand Up @@ -1080,11 +1082,11 @@ static bool SerializeValue(IJsonSerializerStrategy jsonSerializerStrategy, objec
}
}
}
}

if (visited.Count > 0) // Just to be safe
{
visited.Pop();
if (visited.Count > 0) // Just to be safe
{
visited.Pop();
}
}

return success;
Expand Down
36 changes: 36 additions & 0 deletions Mindscape.Raygun4Net.NetCore.Tests/SimpleJsonTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -179,5 +179,41 @@ public void HandleCircularObjectStructureInMultipleBranches_Dictionary()
string json = SimpleJson.SerializeObject(cyclicObject);
Assert.That("{\"Array\":[null],\"Dictionary\":{\"Key1\":\"" + SimpleJson.CYCLIC_MESSAGE + "\"," + "\"Key2\":\"" + SimpleJson.CYCLIC_MESSAGE + "\"," + "\"Key3\":\"" + SimpleJson.CYCLIC_MESSAGE + "\"," + "\"Key4\":\"" + SimpleJson.CYCLIC_MESSAGE + "\"},\"GenericDictionary\":{}}", Is.EqualTo(json));
}

[Test]
public void HandleUri()
{
var testUri = new TestUri
{
Test = new Uri("http://www.google.com")
};

var result = SimpleJson.SerializeObject(testUri);

Assert.That("{\"Test\":\"http://www.google.com/\"}", Is.EqualTo(result));
}

[Test]
public void HandleGuid()
{
var testGuid = new TestGuid
{
Test = Guid.NewGuid()
};

var result = SimpleJson.SerializeObject(testGuid);

Assert.That("{\"Test\":\"" + testGuid.Test + "\"}", Is.EqualTo(result));
}

public class TestGuid
{
public Guid Test { get; set; }
}

public class TestUri
{
public Uri Test { get; set; }
}
}
}

0 comments on commit 14619eb

Please sign in to comment.