I am writing F# code and need to use existing C# libraries with POCO classes defined, those classes are not marked with DataContractAttribute. I need to inherit from them and add properties to the derived class, then I instantiate the derived class and serialize to Json using Newtonsoft Json.NET. By all means, as there are not DataContract or DataMember attribute used, all the properties should be serializied by default. The problem I have is that I cannot get all the properties serialized
- If I simply serialize the derived object, only the base class' properties are serialized
- If I mark the derived class with DataContractAttribute, nothing get serialized
- If I add DataMemberAttribute to the derived properties, then only the derived properties get serialized
So, to get all the properties (both base and derived) serialized, the only way would be to mark both classes with DataContractAttribute, and all the properties with DataMemberAttribute.
Is there a way to have simply all the properties (both base and derived) serialized without adding any attribute whatsoever?
I use JsonSerializer.Serialize(JsonWriter, obj, obj.GetType())
UPDATE
After few more tests, I think the problem is when my derived class is in F#. I did the following test
//C#
public class A {
public string AA { get; set; } = "AA"
}
public class B : A {
public string BB { get; set; } = "BB"
}
//F#
type C() =
inherit B()
let mutable c = "CC"
member public _.CC
with get() = c
and set value = c <- value
If I try to serialize an instance of C, only AA and BB are serialized, CC doesn't get serialized.
Does someone know how to fix this issue?