0

I tried to change from Newtonsoft.Json to System.Text.Json, Im using custom converters for a custom class MyType.

In both cases I implement a converter with JsonConverter MyType as a base class and it works fine if I try to serialize a class which has a property of the type MyType (so far).

But there is a major difference if I change my property from MyType to List of MyType it will still work just fine in Newtonsoft.Json and my converter is called for every list entry, but with System.Text.Json my converter doesn't work anymore, because my converter CanConvert is called with the type parameter List instead of MyType, so my converter returns false and will not be used for the serialization.

So it looks like Newtonsoft is just smarter and detects that the property is a list and will invoke the converter for every list member instead of trying to pass the whole list to the converter.

Is this a bug or a "feature"? Is there a workaround? Because it's a major issues since the same thing worked in Newtonsoft and I think it would be silly to reimplement the serialization logic for lists and even add a special check in my converter to detect if I want to serialize MyType itself or List because it should work in both cases (and it did work in Newtonsoft by default).

R1PFake
  • 410
  • 2
  • 8
  • 1
    Show us your JSON and code – Pavel Anikhouski Oct 04 '20 at 17:14
  • Your question lacks a [mcve], can you please share one? In your Newtonsoft code were you possibly using [`JsonPropertyAttribute.ItemConverterType`](https://www.newtonsoft.com/json/help/html/P_Newtonsoft_Json_JsonPropertyAttribute_ItemConverterType.htm) instead of [`[JsonConverterAttribute(Type)]`](https://www.newtonsoft.com/json/help/html/T_Newtonsoft_Json_JsonConverterAttribute.htm) to define a converter for the list items? – dbc Oct 04 '20 at 17:38
  • I will post a example tomorrow, but I didn't use any special attributes or setting, just the defaults. I added the converter "by hand" JsonSerializerOptions.Converter.Add for Text.Json and JsonSerializer.Converters.Add for Newtonsoft.Json. – R1PFake Oct 04 '20 at 18:00
  • Can't reproduce, see https://dotnetfiddle.net/ZhR79l. `MyTypeConverter.Read()` and `MyTypeConverter.Write()` are both called for an example `MyType` contained in a `List` property. Please [edit] your question to share a [mcve]. – dbc Oct 04 '20 at 19:10
  • @dbc I think I found the "issue" with your example code, my list property is get only (with a default initial list) and Newtonsoft fills this property just fine, but it looks like Text.Json will ignore this get only list property without any error/warning. Using your example code, remove the public setter from the List property in RootObject at line 48 and run your code, the resulting dump will run fine and show a empty list. – R1PFake Oct 04 '20 at 19:44

1 Answers1

0

Thanks to @dbc example code, I found the "problem". My List property was get only without a setter, which works just fine with Newtonsoft, because it will use the existing (intial) List and fill it, but this isn't supported by System.Text.Json yet, it will just ignore the List property without any error/warning. This was already asked, for example here:

R1PFake
  • 410
  • 2
  • 8