JSON deserialization (proper way?)
Hi,
I'm writing a client that consumes some JSON from a websocket.
The socket can give multiple types of messages over it, what would be the most efficient (fastest) way to deserialize the JSON objects to classes? All the type of messages are known beforehand.
The way I'm doing it now is:
12 Replies
How to serialize properties of derived classes with System.Text.Jso...
Learn how to serialize polymorphic objects while serializing to and deserializing from JSON in .NET.
I'm trying that
C# Online Compiler | .NET Fiddle
Test your C# code online with .NET Fiddle code editor.
But it isn't working as I expected, am I doing something wrong?
Define "not working as expected"
See the fiddle, I'd expect the given JSON to be deserialized as an "More"
Given the example: https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json/polymorphism?pivots=dotnet-8-0#polymorphic-type-discriminators I'd say I'm doing it right?
How to serialize properties of derived classes with System.Text.Jso...
Learn how to serialize polymorphic objects while serializing to and deserializing from JSON in .NET.
Or does it not work for JSON that hasn't been serialized by the serializer?
IIRC you need some type discriminator in your JSON
What do you mean?
Example from the docs
But that needs to be in the source JSON?
As their example doesn't include it
Ah, it does it seems..
Well I'll try to get this to work. Thx