-
Couldn't load subscription status.
- Fork 174
Description
Thanks for this really nice implementation of MessagePack!
We use a lot of immutable objects in our code, and these are currently not very compatible with this implementation. It would be nice if the serializer could detect the possibility of deserializing an object via the constructor if there are no settable properties. Current, this implementation throws the following exception in this scenario:
Cannot serialize type 'ConstructorInjectionDemo' because it does not have any serializable fields nor properties
I've written a sample test that demonstrates the feature I'm proposing here:
using System.IO;
using MsgPack.Serialization;
using NUnit.Framework;
public class ConstructorInjectionDemo
{
public string Value { get; private set; }
public ConstructorInjectionDemo(string value)
{
Value = value;
}
}
[TestFixture]
public class MessagePackConstructorInjectionTests
{
[Test]
public void Given_a_class_without_public_setters_When_MessagePack_deserializes_the_object_Then_the_constructor_should_be_used()
{
var serializer = MessagePackSerializer.Get<ConstructorInjectionDemo>();
using(var stream = new MemoryStream())
{
serializer.Pack(stream, new ConstructorInjectionDemo("Hello!"));
stream.Position = 0;
var deserialisedObject = serializer.Unpack(stream);
Assert.That(deserialisedObject.Value, Is.EqualTo("Hello!"));
}
}
}Is there any appetite to implement this feature?
Thanks,
Bart