Replies: 1 comment 1 reply
-
IMO, the current design of STJ is a good balance on functionality and performance. All your examples are only based on easy(but rare, for me) usage and also a lot of reflection.
Well, if you have only one derived class var la = new List<A>
{
new B() { I = 1, J = 2 },
new C() { I = 1, J = 2, K = 3 },
new D() { I = 1, J = 2, N = new F() { I = 1, X = 11, Y = 12 } }
}; do you often meet this requirement that you want a json-array whose elements aren't consistent? It's not common enough.
Does it look good? Is it what C# advocates for? It will definitely become an abuse if supported.
You have already been able to achieve this by |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Newtonsoft.Json can serialize this successfully while System.Text.Json doesn't do it:
There is a page describing how to work with polymorphic objects: https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json/polymorphism?pivots=dotnet-7-0 .
However, it required writing down all possible types.
Newtonsoft.Json writes an object simply as dictionary thus making possible to write any type regardless the hierarachy.
I suggest adding an option either by options or by attribute allowing serializing polymorphic type the same way Newtonsoft.Json does.
Another option is to use TypeInfoResolver for detecting derived type:
Alternatively everything can be defined as
object
which is not so nice making your code dynamically typed.Or serializing every object manually as key-value:
Beta Was this translation helpful? Give feedback.
All reactions