Skip to content

Commit

Permalink
fix: ability to serialize GUIDs (#203)
Browse files Browse the repository at this point in the history
Co-authored-by: Mateusz Kryszczak <[email protected]>
  • Loading branch information
MadSciencist and mateuszkryszczakfenergo authored Mar 28, 2022
1 parent 975f356 commit 4f354c1
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
6 changes: 6 additions & 0 deletions sdk/src/Core/ThirdParty/LitJson/JsonMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -732,6 +732,12 @@ private static void WriteValue (object obj, JsonWriter writer,
return;
}

if (obj is Guid)
{
writer.Write(obj.ToString());
return;
}

if (obj is Array) {
writer.WriteArrayStart ();

Expand Down
27 changes: 27 additions & 0 deletions sdk/test/UnitTests/JsonMapperTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

using Amazon.XRay.Recorder.Core.Sampling.Model;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using ThirdParty.LitJson;

namespace Amazon.XRay.Recorder.UnitTests
Expand Down Expand Up @@ -271,5 +272,31 @@ public void TestUnmarshallSamplingTargetResponseWithNull()

Assert.IsNull(samplingTargetResponseModel);
}

[TestMethod]
public void SerializeObjectContainingEmptyGuid()
{
var obj = new AnythingWithGuid();
var actual = JsonMapper.ToJson(obj);
var expected = "{\"Id\":\"00000000-0000-0000-0000-000000000000\"}";

Assert.AreEqual(expected, actual);
}

[TestMethod]
public void SerializeObjectContainingNonEmptyGuid()
{
var guid = Guid.Parse("b2aee583-770c-42c5-b5d8-d25e0df4d161");
var obj = new AnythingWithGuid { Id = guid };
var actual = JsonMapper.ToJson(obj);
var expected = "{\"Id\":\"b2aee583-770c-42c5-b5d8-d25e0df4d161\"}";

Assert.AreEqual(expected, actual);
}

public class AnythingWithGuid
{
public Guid Id { get; set; }
}
}
}

0 comments on commit 4f354c1

Please sign in to comment.