So you hate attributes? Well, you're in luck!
The latest version of .NET 7 (preview 5) introduces Polymorphic serialization and deserialization features that should help folks utilizing inheritance hierarchies in their object models serialize and deserialize JSON more easily than they previously could.
One complaint, which .NET developers have, is the use of Attributes
. Personally, I think they're fine and using them as a form of metaprogramming ultimately helps direct intent. That said, "haters gonna hate", and I'm here to help solve problems.
using System.Text.Json;
namespace Polymporphic.Models;
[JsonDerivedType(typeof(Programmer), typeDiscriminator: "programmer")]
[JsonDerivedType(typeof(Wrestler), typeDiscriminator: "wrestler")]
[JsonDerivedType(typeof(Doctor), typeDiscriminator: "doctor")]
[JsonDerivedType(typeof(Models.Gamer), typeDiscriminator: "gamer")]
public partial record Person { }
One thing that may be annoying about this particular approach is its ceremony. Every new object in your inheritance hierarchy must be added to the base type definition. This can lead to mistakes and potential bugs. As developers, we don't like bugs. Instead, we should have enough metadata in our code to make informed decisions and generate these attributes.
There are a few steps you need to take to use this source generator.
- Create a base type decorated with the
JsonDerivedTypes
attribute
[JsonDerivedTypes]
public partial record Person(string Name);
- Create several objects that inherit from your base type.
// it's cool, go ahead add more
public record Programmer(string Name, string Language) : Person(Name);
public record Wrestler(string Name, string FinishingMove) : Person(Name);
public record Doctor(string Name, string Specialty): Person(Name);
public record Gamer(string Name, string Game): Person(Name);
- Sit back and let the source generators work!
// <auto-generated/>
namespace Polymorphic.Models;
[System.Text.Json.Serialization.JsonDerivedType(typeof(Polymorphic.Models.Programmer), typeDiscriminator: "programmer")]
[System.Text.Json.Serialization.JsonDerivedType(typeof(Polymorphic.Models.Wrestler), typeDiscriminator: "wrestler")]
[System.Text.Json.Serialization.JsonDerivedType(typeof(Polymorphic.Models.Doctor), typeDiscriminator: "doctor")]
[System.Text.Json.Serialization.JsonDerivedType(typeof(Polymorphic.Models.Gamer), typeDiscriminator: "gamer")]
public partial record Person { }
When you serialize the collection of persons using System.Text.Json
you will get a $type
field.
[
{
"$type": "doctor",
"specialty": "Pharmaceutical Chemistry",
"name": "Doogie Howser, M.D."
},
{
"$type": "gamer",
"game": "Marvel Vs. Capcom 2",
"name": "Justin Wong"
},
{
"$type": "programmer",
"language": "FLOW-MATIC",
"name": "Grace Hopper"
},
{
"$type": "wrestler",
"finishingMove": "The People's Elbow",
"name": "The Rock"
}
]
There you have it, a few less attributes in your C# projects. Cheers.
The MIT License (MIT) Copyright © 2022 Khalid Abuhakmeh
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.