Skip to main content

JsonPolymorphicGenerator by surgicalcoder

Nuget / site data

Nuget GitHub last commit GitHub Repo stars

Details

Info

info

Name: JsonPolymorphicGenerator

Source Code Generator for System.Text.Json JsonDerivedType attributes on polymorphic classes

Author: surgicalcoder

NuGet: https://www.nuget.org/packages/GoLive.Generator.JsonPolymorphicGenerator/

You can find more details at https://github.com/surgicalcoder/JsonPolymorphicGenerator

Source : https://github.com/surgicalcoder/JsonPolymorphicGenerator

Original Readme

note

JsonPolymorphicGenerator

c# / .net Source Code Generator for System.Text.Json JsonDerivedType attributes on polymorphic classes

Usage

For this, your base classes need the partial and abstract key words, and be decorated with JsonPolymorphic, and there need to be derived types in that same assembly for this to work.

An example of this is:

[JsonPolymorphic]
public abstract partial class BaseClass
{
public string Property1 { get; set; }
}

This will then generate a partial class, that is decorated with the JsonDerivedType attribute, and use the class name as the discriminator.

[JsonDerivedType(typeof(GoLive.JsonPolymorphicGenerator.Playground.InheritedClass1), "InheritedClass1")]
[JsonDerivedType(typeof(GoLive.JsonPolymorphicGenerator.Playground.InheritedClass2), "InheritedClass2")]
public partial class BaseClass
{
}

You can now transform the text of the attributes that gets spat out! You have a number of options, that gets added to an .editorconfig, such as:

root = true

[*.cs]
jsonpolymorphicgenerator.text_preappend = JSON_
jsonpolymorphicgenerator.text_transform = return classname.GetHashCode().ToString()
jsonpolymorphicgenerator.text_postappend = _A

For the jsonpolymorphicgenerator.text_transform option, you have to provide valid c# code, that returns a string - there are 2 input variables - classname and namespacename

About

note

Generating JsonDerivedType to be added to the base class

How to use

Example ( source csproj, source files )

This is the CSharp Project that references JsonPolymorphicGenerator

<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="GoLive.Generator.JsonPolymorphicGenerator" Version="1.0.4">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>

<PropertyGroup>
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
<CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GX</CompilerGeneratedFilesOutputPath>
</PropertyGroup>

</Project>

Generated Files

Those are taken from $(BaseIntermediateOutputPath)\GX

using System.Text.Json.Serialization;

namespace JsonPolymorphicGeneratorDemo
{
[JsonDerivedType(typeof(JsonPolymorphicGeneratorDemo.Teacher), "Teacher")]
[JsonDerivedType(typeof(JsonPolymorphicGeneratorDemo.Student), "Student")]
public partial class Person
{
}
}



Usefull

Download Example (.NET C# )

Share JsonPolymorphicGenerator

https://ignatandrei.github.io/RSCG_Examples/v2/docs/JsonPolymorphicGenerator

In the same category (Serializer) - 4 other generators

GenPack

jsonConverterSourceGenerator

ProtobufSourceGenerator

System.Text.Json