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?
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, sayEventNamethat 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 theTypeby the value and then callsJsonSerializer.NonGeneric.Deserialize(eventTypeValue, ref reader);What I get
I don't control the contracts of those
AuditEventBaseand they have non-empty constructors, such as: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 deserializeBasically, I have this test
And it goes red if any of two properties
TestString/TestIntare commented and green if both are there. As you can see, they don't even participate inTestAuditEvent, 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?