Skip to content

Generated serializer for TType does not support deserialize #33

@quilin

Description

@quilin

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions