-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #587 from SteveDunn/565-support-for-trimming-no-re…
…flection-in-systemtextjson Support for trimmed/no-reflection/AOT with System.Text.Json
- Loading branch information
Showing
22,202 changed files
with
22,576 additions
and
22,219 deletions.
The diff you're trying to view is too large. We only load the first 3000 changed files.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
<UseLocallyBuiltPackage>true</UseLocallyBuiltPackage> | ||
<RootNamespace>MyApp</RootNamespace> | ||
<RuntimeIdentifier>win-x64</RuntimeIdentifier> | ||
|
||
<PublishTrimmed>true</PublishTrimmed> | ||
<PublishAot>true</PublishAot> | ||
<TrimMode>partial</TrimMode> | ||
<JsonSerializerIsReflectionEnabledByDefault>false</JsonSerializerIsReflectionEnabledByDefault> | ||
</PropertyGroup> | ||
|
||
<ItemGroup Condition=" '$(UseLocallyBuiltPackage)' != ''"> | ||
<PackageReference Include="Vogen" Version="999.9.*" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup Condition=" '$(UseLocallyBuiltPackage)' == ''"> | ||
<PackageReference Include="Vogen" Version="999.9.10219943" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
using Vogen; | ||
|
||
// to *not* generate the factory, use: | ||
// [assembly: VogenDefaults(systemTextJsonConverterFactoryGeneration: SystemTextJsonConverterFactoryGeneration.Omit)] | ||
|
||
Person person = new() | ||
{ | ||
Name = Name.From("Fred Flintstone"), | ||
Age = Age.From(44), | ||
Address = Address.From("201 Cobblestone Lane"), | ||
}; | ||
|
||
var options = new JsonSerializerOptions | ||
{ | ||
WriteIndented = true, | ||
Converters = | ||
{ | ||
new VogenTypesFactory() | ||
} | ||
}; | ||
|
||
var ctx = new JsonSourceGenerationContext(options); | ||
|
||
var json = JsonSerializer.Serialize(person, ctx.Person); | ||
Person person2 = JsonSerializer.Deserialize(json, ctx.Person)!; | ||
|
||
Console.WriteLine(json); | ||
Console.WriteLine($"{person2.Name} is {person2.Age}, and lives at {person2.Address}"); | ||
|
||
public class Person | ||
{ | ||
public Age Age { get; set; } | ||
public Name Name { get; set; } | ||
public Address Address { get; set; } | ||
} | ||
|
||
[ValueObject<int>] | ||
public partial struct Age {} | ||
|
||
[ValueObject<string>] | ||
public partial struct Name {} | ||
|
||
[ValueObject<string>] | ||
public partial struct Address {} | ||
|
||
[JsonSourceGenerationOptions(WriteIndented = true)] | ||
[JsonSerializable(typeof(Person))] | ||
internal partial class JsonSourceGenerationContext : JsonSerializerContext | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
An example of using Vogen with System.Text.Json (STJ) source-generated serialization. | ||
|
||
Reflection cannot be used in trimmed, and/or AOT compiled .NET applications. | ||
|
||
Additionally, STJ converters, as produced by Vogen, aren't available | ||
when STJ does its source generation. | ||
|
||
The solution to this is for Vogen to source-generate a 'factory', which tells | ||
STJ the respective converters for each value object generated. | ||
|
||
A typical example is something like: | ||
|
||
```c# | ||
var options = new JsonSerializerOptions | ||
{ | ||
WriteIndented = true, | ||
Converters = | ||
{ | ||
new VogenTypesFactory() | ||
} | ||
}; | ||
|
||
var ctx = new JsonSourceGenerationContext(options); | ||
|
||
var json = JsonSerializer.Serialize(person, ctx.Person); | ||
Person person2 = JsonSerializer.Deserialize(json, ctx.Person)!; | ||
``` | ||
|
||
This sample produces a self-contained AOT binary for `win-x64`. | ||
|
||
**Note** | ||
If you _don't_ want the factory built, then this is configurable in the global config with: | ||
|
||
`[assembly: VogenDefaults( | ||
systemTextJsonConverterFactoryGeneration: SystemTextJsonConverterFactoryGeneration.Omit)]` |
22 changes: 22 additions & 0 deletions
22
src/Vogen.SharedTypes/SystemTextJsonConverterFactoryGeneration.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
using System.Diagnostics; | ||
|
||
namespace Vogen; | ||
|
||
/// <summary> | ||
/// Defines if a JSON converter 'factory' is generated containing value objects with System.Text.Json converters. | ||
/// These factories can be used in various scenarios with System.Text.Json, including source-generation. | ||
/// </summary> | ||
public enum SystemTextJsonConverterFactoryGeneration | ||
{ | ||
[DebuggerBrowsable(DebuggerBrowsableState.Never)] | ||
Unspecified = -1, | ||
/// <summary> | ||
/// Do not generate the factory. | ||
/// </summary> | ||
Omit = 0, | ||
|
||
/// <summary> | ||
/// Generate the factory. | ||
/// </summary> | ||
Generate = 1 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
src/Vogen/Templates/AnyOtherType/AnyOtherType_SystemTextJsonConverter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
src/Vogen/Templates/DateTime/DateTime_SystemTextJsonConverter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
src/Vogen/Templates/DateTimeOffset/DateTimeOffset_SystemTextJsonConverter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.