Aviationexam.GeneratedJsonConverters.Abstractions.NetStandard
0.7.3
Prefix Reserved
dotnet add package Aviationexam.GeneratedJsonConverters.Abstractions.NetStandard --version 0.7.3
NuGet\Install-Package Aviationexam.GeneratedJsonConverters.Abstractions.NetStandard -Version 0.7.3
<PackageReference Include="Aviationexam.GeneratedJsonConverters.Abstractions.NetStandard" Version="0.7.3" />
<PackageVersion Include="Aviationexam.GeneratedJsonConverters.Abstractions.NetStandard" Version="0.7.3" />
<PackageReference Include="Aviationexam.GeneratedJsonConverters.Abstractions.NetStandard" />
paket add Aviationexam.GeneratedJsonConverters.Abstractions.NetStandard --version 0.7.3
#r "nuget: Aviationexam.GeneratedJsonConverters.Abstractions.NetStandard, 0.7.3"
#:package Aviationexam.GeneratedJsonConverters.Abstractions.NetStandard@0.7.3
#addin nuget:?package=Aviationexam.GeneratedJsonConverters.Abstractions.NetStandard&version=0.7.3
#tool nuget:?package=Aviationexam.GeneratedJsonConverters.Abstractions.NetStandard&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 | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. 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 was computed. 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 was computed. 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. |
| .NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
| .NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | tizen40 was computed. tizen60 was computed. |
| Xamarin.iOS | xamarinios was computed. |
| Xamarin.Mac | xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- Microsoft.Bcl.AsyncInterfaces (>= 8.0.0)
- System.Buffers (>= 4.6.1)
- System.Memory (>= 4.6.3)
- System.Numerics.Vectors (>= 4.6.1)
- System.Runtime.CompilerServices.Unsafe (>= 6.1.2)
- System.Text.Encodings.Web (>= 8.0.0)
- System.Text.Json (>= 8.0.5)
- System.Threading.Tasks.Extensions (>= 4.6.3)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on Aviationexam.GeneratedJsonConverters.Abstractions.NetStandard:
| Package | Downloads |
|---|---|
|
Aviationexam.GeneratedJsonConverters.SourceGenerator
Package Description |
GitHub repositories
This package is not used by any popular GitHub repositories.