Aviationexam.GeneratedJsonConverters.SourceGenerator 0.7.3

Prefix Reserved
dotnet add package Aviationexam.GeneratedJsonConverters.SourceGenerator --version 0.7.3
                    
NuGet\Install-Package Aviationexam.GeneratedJsonConverters.SourceGenerator -Version 0.7.3
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Aviationexam.GeneratedJsonConverters.SourceGenerator" Version="0.7.3" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Aviationexam.GeneratedJsonConverters.SourceGenerator" Version="0.7.3" />
                    
Directory.Packages.props
<PackageReference Include="Aviationexam.GeneratedJsonConverters.SourceGenerator" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Aviationexam.GeneratedJsonConverters.SourceGenerator --version 0.7.3
                    
#r "nuget: Aviationexam.GeneratedJsonConverters.SourceGenerator, 0.7.3"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Aviationexam.GeneratedJsonConverters.SourceGenerator@0.7.3
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Aviationexam.GeneratedJsonConverters.SourceGenerator&version=0.7.3
                    
Install as a Cake Addin
#tool nuget:?package=Aviationexam.GeneratedJsonConverters.SourceGenerator&version=0.7.3
                    
Install as a Cake Tool

Build Status NuGet MyGet feedz.io

Aviationexam.GeneratedJsonConverters.SourceGenerator

Motivation for this library are polymorphic contracts with discriminator property not present as first property.

i.e. this JSON

{
    "baseProperty": 1,
    "$type": 2,
    "anotherLeafProperty": 2
}

is deserialized correctly into AnotherLeafContract using this library.

And string based enum serialization.

Install

<ItemGroup>
    <PackageReference Include="Aviationexam.GeneratedJsonConverters.SourceGenerator" Version="0.1.0" />
</ItemGroup>

The generated converters derive from public types in Aviationexam.GeneratedJsonConverters.Abstractions, which the generator package brings in as a dependency. netstandard2.0 projects get Aviationexam.GeneratedJsonConverters.Abstractions.NetStandard instead, built from the same sources under a different assembly name. Do not mark the reference PrivateAssets="all": projects referencing yours need the abstractions assembly at runtime. The analyzer and its build properties stay private by default.

A netstandard2.0-only library used by net8.0+ applications must reference the netstandard2.0 abstractions itself, because those applications restore the generator for net8.0 and get only Aviationexam.GeneratedJsonConverters.Abstractions. The reference reaches them as a runtime-only dependency, so they never compile against both assemblies. A library that also targets net8.0 (for example netstandard2.0;net8.0) does not need it.

<ItemGroup>
    <PackageReference Include="Aviationexam.GeneratedJsonConverters.Abstractions.NetStandard" Version="0.1.0" PrivateAssets="compile;contentfiles;analyzers;build" />
</ItemGroup>

How to use library

<PropertyGroup>
    
    <AVI_EJC_DefaultJsonSerializerContext_ClassAccessibility>public</AVI_EJC_DefaultJsonSerializerContext_ClassAccessibility>
    <AVI_EJC_DefaultJsonSerializerContext_Namespace>NamespaceOf.My.Json.Serializer.Context</AVI_EJC_DefaultJsonSerializerContext_Namespace>
    <AVI_EJC_DefaultJsonSerializerContext_ClassName>MyJsonSerializerContext</AVI_EJC_DefaultJsonSerializerContext_ClassName>

    
    <AVI_EJC_DefaultEnumSerializationStrategy>BackingType</AVI_EJC_DefaultEnumSerializationStrategy>

    
    <AVI_EJC_DefaultEnumDeserializationStrategy>UseBackingType|UseEnumName</AVI_EJC_DefaultEnumDeserializationStrategy>
</PropertyGroup>
// file=contracts.cs
using Aviationexam.GeneratedJsonConverters.Attributes;

[JsonPolymorphic] // notice, that attributes are from `Aviationexam.GeneratedJsonConverters.Attributes` namespace, not `System.Text.Json.Serialization`
[JsonDerivedType(typeof(LeafContract), typeDiscriminator: nameof(LeafContract))]
[JsonDerivedType(typeof(AnotherLeafContract), typeDiscriminator: 2)]
[JsonDerivedType<GenericLeafContract>(typeDiscriminator: nameof(GenericLeafContract))]
public abstract class BaseContract
{
    public int BaseProperty { get; set; }
}
public sealed class LeafContract : BaseContract
{
    public int LeafProperty { get; set; }
}
public sealed class AnotherLeafContract : BaseContract
{
    public int AnotherLeafProperty { get; set; }
}
public sealed class GenericLeafContract : BaseContract
{
    public int Property { get; set; }
}

[EnumJsonConverter] // this use project defined configuration
public enum EMyEnum
{
    [EnumMember(Value = "C")]
    A,
    [EnumMember(Value = "D")]
    B,
}

[EnumJsonConverter(
    SerializationStrategy = EnumSerializationStrategy.FirstEnumName,
    DeserializationStrategy = EnumDeserializationStrategy.UseEnumName
)]
public enum EMyEnumWithExplicitConfiguration
{
    [EnumMember(Value = "C")]
    A,
    [EnumMember(Value = "D")]
    B,
}

[DisableEnumJsonConverter]
public enum EMyIgnoredEnum
{
    C,
    D,
}

// file=MyJsonSerializerContext.cs
using System.Text.Json.Serialization;

[JsonSerializable(typeof(BaseContract))] // this line is neccesary, generator searches for JsonSerializableAttribute with argument type decorated by JsonPolymorphicAttribute
[JsonSerializable(typeof(LeafContract))] // notice, it's necessary to specify leaf types
[JsonSerializable(typeof(AnotherLeafContract))]
[JsonSerializable(typeof(GenericLeafContract))]

[JsonSerializable(typeof(EMyEnum))] // only necessary for not referenced enums from other contracts
[JsonSerializable(typeof(EMyEnumWithExplicitConfiguration))]
public partial class MyJsonSerializerContext : JsonSerializerContext
{
    static MyJsonSerializerContext()
    {
        // register generated converters to options
        UsePolymorphicConverters(s_defaultOptions.Converters);
        UseEnumConverters(s_defaultOptions.Converters);

        Default = new MyJsonSerializerContext(new System.Text.Json.JsonSerializerOptions(s_defaultOptions));
    }
}
There are no supported framework assets in this package.

Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
0.7.3 124 10/16/2026
0.7.2 96 10/16/2026
0.7.1 106 10/15/2026
0.6.4 95 10/15/2026
0.6.3 678 3/19/2026
0.6.2 169 3/16/2026
0.6.1 156 3/16/2026
0.6.0 178 3/13/2026
0.5.4 386 1/6/2026
0.5.3 189 1/6/2026
0.5.2 193 1/5/2026
0.5.1 175 1/5/2026
0.5.0 180 1/5/2026
0.4.0 499 11/12/2025
0.3.2 357 11/12/2025
0.3.1 814 3/1/2025
0.3.0 267 2/26/2025
0.2.1 450 12/18/2024
0.2.0 514 11/18/2024
0.1.19 362 10/10/2024
Loading failed