0

Since the currect version of the System.Text.Json Serializer does not support inherited properties on interfaces I'm looking for the neatest way to mock it's behavior in the Write method.

Right now I'm calling the Serialize method, e.g.

public override void Write(Utf8JsonWriter writer, T value, JsonSerializerOptions options)
{
        JsonSerializer.Serialize(writer, value, interfaceType, options)
}

but, as mentioned, this does not respect inherited properties. I could easily lookup the interface's properties myself, but what would be the nicest way to write these properties now with all the cases the default Serializer would respect?

tris
  • 863
  • 4
  • 9
  • 22
  • Do you mean you want to serialize all the properties of `value` rather than just the properties declared in type `interfaceType`? If so can't you just pass `value.GetType()` to `JsonSerializer.Serialize()`? Or have I misunderstood your problem, and you are having some difficulty with recursion? If the problem is with recursion see [How to use default serialization in a custom System.Text.Json JsonConverter?](https://stackoverflow.com/q/65430420/3744182). Otherwise, can you share a [mcve]? – dbc Mar 08 '21 at 19:46
  • Or maybe your problem is that base interface properties are not getting serialized when `interfaceType` inherits from some base interfaces(s), as is described in [When an interface inherits an interface, System.Text.Json.JsonSerializer.Serialize only encodes the properties of the current interface into the json #48768](https://github.com/dotnet/runtime/issues/48768#issuecomment-786135151)? – dbc Mar 08 '21 at 19:51
  • Yes, the second one is my problem - thank you very much! Since I have no idea when inherited properties would be respected I thought about collecting the properties and serialize the object myself. So I'm looking for the cleanest way to do so. – tris Mar 09 '21 at 08:30
  • I'm not sure there is a clean way to work around Issue #48768. Though the problem with Issue #48768 is not that inherited properties are not serialized, it's that **base interface properties** are not serialized, so I'm still a little confused as to your problem. Can you share a [mcve]? Since the problem only presents itself when an object as serialized as an interface type, would serializing it as its class type help? Are you trying to filter all class properties not belonging to a specific interface or its base interfaces? – dbc Mar 09 '21 at 13:28
  • I cannot use its "GetType()" method since I do not want all properties to be serialized! Only the interface's properties should be generated. So I guess I'm stuck with STJ atm and need to go back to Newtonsoft? – tris Mar 09 '21 at 13:32
  • It looks to be inconvenient to do what you want as long as Issue #48768 is open. Of course you could always write your own generic custom converter that uses reflection. You'd basically be writing your own contract resolver at that point though. Or maybe something like https://github.com/dahomey-technologies/Dahomey.Json would help, it has [converters](https://github.com/dahomey-technologies/Dahomey.Json/blob/master/src/Dahomey.Json/Serialization/Converters/ObjectConverter.cs) that already do that, see e.g. [this answer](https://stackoverflow.com/a/59294644/3744182). – dbc Mar 09 '21 at 13:39
  • Yeah, guess you're right - wait for #48768 to be resolved - thank you! – tris Mar 09 '21 at 14:01

0 Answers0