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

Generated serializer for TType does not support deserialize #33

Open
quilin opened this issue Aug 17, 2021 · 1 comment
Open

Generated serializer for TType does not support deserialize #33

quilin opened this issue Aug 17, 2021 · 1 comment

Comments

@quilin
Copy link

quilin commented Aug 17, 2021

What I'm trying to do

I'm working on semi-abstract formatter to deserialize messages from Kafka. Those are messages that are inherited from the same parent, say AuditEventBase, serialized to JSON string, passed via Kafka, and now I need to deserialize those to their according origin type. Messages contain the field, say EventName that is name of the type and by that name the origin type can be discovered.

I created my own formatter and, basically, it runs through the JSON searching for the field with certain name, that aforementioned EventName, gets the value, finds the Type by the value and then calls JsonSerializer.NonGeneric.Deserialize(eventTypeValue, ref reader);

What I get

I don't control the contracts of those AuditEventBase and they have non-empty constructors, such as:

    public class TestAuditEvent : TestAuditEventBase
    {
        public TestAuditEvent(string testString, int testInt)
            : base(15, 25, testString, testInt, "unknown",
                new DateTimeOffset(2020, 04, 12, 00, 00, 00, TimeSpan.Zero))
        {
        }

        public string TestString { get; set; }
        public int TestInt { get; set; }

        public override string GetDescription() => "asda";
    }

Like, even the parent abstract class overrides the default constructor. There is the issue in neuecc's repository: neuecc#216 that tells that the object must have empty constructor. The problem is in this case the deserialization goes fine. But if only I remove one of those two autoprops (TestString / TestInt) I get an exception: generated serializer for UploadDocumentInboundErrorEAPAuditEvent does not support deserialize

Basically, I have this test

        [TestMethod]
        public void WatTest()
        {
            var actual = JsonSerializer.NonGeneric.Deserialize(typeof(TestAuditEvent),
                @"{
""EventName"":""TestAuditEvent"",
""UserName"":""hello"",
""ArticleId"":150
}");
            actual.Should().BeOfType<TestAuditEvent>()
                .And.BeEquivalentTo(new TestAuditEvent("hello", 150));
        }

And it goes red if any of two properties TestString/TestInt are commented and green if both are there. As you can see, they don't even participate in TestAuditEvent, just sit there.

What I expect to get

I understand that the idea of "only empty constructor classes can be deserialized", but maybe either fail fast for every json that is being deserialized to classes without empty constructors or make it work for every case?

@quilin
Copy link
Author

quilin commented Sep 27, 2021

UPD:
After reading the source code I realised the issue was with arguments naming. Would you accept the pull request with changes in readme.md, mentioning how to use objects with constructors with parameters?

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

No branches or pull requests

1 participant