It is simpler and more efficient to work with polymorphic JSON serialization and deserialization with.NET 8. previous iterations of the system. Polymorphism was a problem for Text.Json, but.NET 8 now has built-in support for it, so there’s no need for workarounds or extra libraries.
Polymorphic Serialization
Polymorphic serialization involves converting an object to JSON using its base class or interface, while also retaining information (such as its actual type) to ensure the correct derived class is restored during deserialization.
Polymorphic Deserialization
Polymorphic deserialization involves converting JSON back into the correct derived class, rather than just the base class.
.NET 8 builds on the capabilities introduced in .NET 7 by further enhancing support for polymorphic serialization through the JsonDerivedType attribute, JsonPolymorphismOptions, and JsonPolymorphic.
In our example, the MessageRequest JSON includes a nested Message object, which can represent either an Email or an Sms, each with its own specific fields. The MessageType property is used to identify the type of message.
This explanation covers a simple approach known as declarative polymorphism, which leverages attributes.
To properly handle serialization and deserialization.
- We define a base class, Message, and
- Apply the [JsonPolymorphic] attribute with MessageType as the discriminator.
- Apply the [JsonDerivedType] attribute to the base class Message, specifying the derived classes Email and Sms, along with their corresponding MessageType values (Email or Sms).
Note. You don’t need to explicitly define the MessageType field in the Email and Sms classes. It is automatically included during serialization and leveraged during deserialization to determine the appropriate class.
Common Mistakes to Avoid
- Missing Type Field: If the “MessageType” field is missing, the system will not know which specific class to convert the data into.
- Case Sensitivity: The “MessageType” value must match exactly, including uppercase and lowercase letters, unless you handle it differently.
- Interface vs. Abstract Class: This works with both interfaces and abstract classes, but make sure you use one style consistently.
I just ran the code and captured screenshots for both deserialization and serialization.
Deserialization
Serialization
Conclusion
Polymorphic deserialization in .NET 8 is now a built-in, first-class feature. It makes handling different object types easier in scenarios like message processing, APIs, or document-based storage.
Happy Coding!
Best ASP.NET Core 10.0 Hosting
The feature and reliability are the most important things when choosing a good ASP.NET Core 10.0 hosting. HostForLIFE is the leading provider of Windows hosting and affordable ASP.NET Core , their servers are optimized for PHP web applications such as the latest ASP.NET Core 10.0 version. The performance and the uptime of the ASP.NET Core hosting service are excellent, and the features of the web hosting plan are even greater than what many hosting providers ask you to pay for. At HostForLIFEASP.NET, customers can also experience fast ASP.NET Core hosting. The company invested a lot of money to ensure the best and fastest performance of the datacenters, servers, network and other facilities. Its data centers are equipped with top equipment like cooling system, fire detection, high-speed Internet connection, and so on. That is why HostForLIFEASP.NET guarantees 99.9% uptime for ASP.NET Core . And the engineers do regular maintenance and monitoring works to assure its ASP.NET Core hosting are security and always up.