Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Object serialization does not respect runtime types #22

Open
Anton92nd opened this issue Apr 20, 2020 · 2 comments
Open

Object serialization does not respect runtime types #22

Anton92nd opened this issue Apr 20, 2020 · 2 comments

Comments

@Anton92nd
Copy link
Contributor

GroBuf only respect primitive types, object[] and Hashtable when serializing a property of the object type, but does not respect, for example, Dictionary<TKey, TValue>.

[Test]
public void TestDictionaryInObject()
{
    var a = new A {S = "zzz", Dict = new Dictionary<string, string> {["key"] = "value"}};
    var data = serializer.Serialize(a);

    var deserializedA = serializer.Deserialize<A>(data);
    Assert.AreEqual("zzz", deserializedA.S);
    Assert.NotNull(deserializedA.Dict);
    Assert.That(deserializedA.Dict, Is.TypeOf<Dictionary<string, string>>());
    Assert.AreEqual("value", ((Dictionary<string, string>)deserializedA.Dict)["key"]);
}

public class A
{
    public object S { get; set; }
    public object Dict { get; set; }
}

This test fails on Assert.NotNull operation.

@Anton92nd
Copy link
Contributor Author

It seems to me that it may be hard to fix because GroBuf serialization considers types known at compile-time, but it has no mechanism to call a Writer for a runtime type.

@Anton92nd
Copy link
Contributor Author

This failing test proves that the problem is in serialization process.

        [Test]
        public void TestDictionaryInObjectSize()
        {
            var first = new A {S = "zzz", Z = new Dictionary<string, string> {["key"] = "value"}};
            var firstBytes = serializer.Serialize(first);
            
            var second = new A {S = "zzz", Z = null};
            var secondBytes = serializer.Serialize(second);
            
            Assert.AreNotEqual(firstBytes.Length, secondBytes.Length);
        }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant