Deserializing Guids using Json.Net to ExpandoObject loses type and is a string

1.6k Views Asked by At

Given the following class:

public class Entity
{
    public Guid UniqueId
    {
        get;
        set;
    }
}

The following test fails:

    [Test]
    public void GuidTest()
    {
        var entity = new Entity { UniqueId = Guid.NewGuid() };
        var entityJson = JsonConvert.SerializeObject(entity);
        dynamic reconstitutedEntity = JsonConvert.DeserializeObject<ExpandoObject>(entityJson);
        Assert.That(reconstitutedEntity.UniqueId, Is.EqualTo(entity.UniqueId));
    }

With the error:

Expected: 35ac3081-07cb-41dd-bf40-22e2ff47863c But was: "35ac3081-07cb-41dd-bf40-22e2ff47863c"

Is this a bug in Json.Net or expected behavior? How do I get the test to pass using ExpandoObject?

If I replace ExpandoObject with Entity, it works just fine. Do I have to write a custom JsonConverter? I attempted this but for some reason, ReadJson was never called though CanConvert and WriteJson were (successfully).

0

There are 0 best solutions below