Aviationexam.GeneratedJsonConverters.Abstractions
0.7.3
Prefix Reserved
dotnet add package Aviationexam.GeneratedJsonConverters.Abstractions --version 0.7.3
NuGet\Install-Package Aviationexam.GeneratedJsonConverters.Abstractions -Version 0.7.3
<PackageReference Include="Aviationexam.GeneratedJsonConverters.Abstractions" Version="0.7.3" />
<PackageVersion Include="Aviationexam.GeneratedJsonConverters.Abstractions" Version="0.7.3" />
<PackageReference Include="Aviationexam.GeneratedJsonConverters.Abstractions" />
paket add Aviationexam.GeneratedJsonConverters.Abstractions --version 0.7.3
#r "nuget: Aviationexam.GeneratedJsonConverters.Abstractions, 0.7.3"
#:package Aviationexam.GeneratedJsonConverters.Abstractions@0.7.3
#addin nuget:?package=Aviationexam.GeneratedJsonConverters.Abstractions&version=0.7.3
#tool nuget:?package=Aviationexam.GeneratedJsonConverters.Abstractions&version=0.7.3
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));
}
}
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 is compatible. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net10.0
- No dependencies.
-
net8.0
- No dependencies.
-
net9.0
- No dependencies.
NuGet packages (1)
Showing the top 1 NuGet packages that depend on Aviationexam.GeneratedJsonConverters.Abstractions:
| Package | Downloads |
|---|---|
|
Aviationexam.GeneratedJsonConverters.SourceGenerator
Package Description |
GitHub repositories
This package is not used by any popular GitHub repositories.