forked from dotnet/runtime
-
Notifications
You must be signed in to change notification settings - Fork 1
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 dotnet#5 from kevinwkt/JsonCodeGenSkeleton
Json code gen skeleton
- Loading branch information
Showing
9 changed files
with
205 additions
and
5 deletions.
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
17 changes: 17 additions & 0 deletions
17
...ries/System.Text.Json/System.Text.Json.SourceGeneration.Tests/JsonSourceGeneratorTests.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,17 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using Xunit; | ||
|
||
namespace System.Text.Json.SourceGeneration.Tests | ||
{ | ||
public class JsonSerializerSouceGeneratorTests | ||
{ | ||
[Fact] | ||
public static void TestGeneratedCode() | ||
{ | ||
Assert.Equal("Hello", HelloWorldGenerated.HelloWorld.SayHello()); | ||
} | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
...on/System.Text.Json.SourceGeneration.Tests/System.Text.Json.SourceGeneration.Tests.csproj
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,19 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<PropertyGroup> | ||
<TargetFrameworks>$(NetCoreAppCurrent);$(NetFrameworkCurrent)</TargetFrameworks> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\System.Text.Json.SourceGeneration\System.Text.Json.SourceGeneration.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<Compile Include="JsonSourceGeneratorTests.cs" /> | ||
</ItemGroup> | ||
|
||
<Target Name="FixIncrementalCoreCompileWithAnalyzers" BeforeTargets="CoreCompile"> | ||
<ItemGroup> | ||
<CustomAdditionalCompileInputs Include="@(Analyzer)" /> | ||
</ItemGroup> | ||
</Target> | ||
</Project> |
33 changes: 33 additions & 0 deletions
33
.../System.Text.Json/System.Text.Json.SourceGeneration.UnitTests/JsonSourceGeneratorTests.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,33 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.Diagnostics; | ||
using Xunit; | ||
|
||
namespace System.Text.Json.SourceGeneration.UnitTests | ||
{ | ||
public static class GeneratorTests | ||
{ | ||
[Fact] | ||
public static void SourceGeneratorInitializationPass() | ||
{ | ||
} | ||
|
||
[Fact] | ||
public static void SourceGeneratorInitializationFail() | ||
{ | ||
} | ||
|
||
[Fact] | ||
public static void SourceGeneratorExecutionPass() | ||
{ | ||
} | ||
|
||
[Fact] | ||
public static void SourceGeneratorExecutionFail() | ||
{ | ||
} | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
...m.Text.Json.SourceGeneration.UnitTests/System.Text.Json.SourceGeneration.UnitTests.csproj
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,21 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<PropertyGroup> | ||
<TargetFrameworks>$(NetCoreAppCurrent);$(NetFrameworkCurrent)</TargetFrameworks> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.CodeAnalysis" Version="$(MicrosoftCodeAnalysisVersion)" PrivateAssets="all" /> | ||
<ProjectReference Include="..\System.Text.Json.SourceGeneration\System.Text.Json.SourceGeneration.csproj" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<Compile Include="JsonSourceGeneratorTests.cs" /> | ||
</ItemGroup> | ||
|
||
<Target Name="FixIncrementalCoreCompileWithAnalyzers" BeforeTargets="CoreCompile"> | ||
<ItemGroup> | ||
<CustomAdditionalCompileInputs Include="@(Analyzer)" /> | ||
</ItemGroup> | ||
</Target> | ||
|
||
</Project> |
62 changes: 62 additions & 0 deletions
62
src/libraries/System.Text.Json/System.Text.Json.SourceGeneration/JsonSourceGenerator.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,62 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.Text; | ||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
using System.Collections.Generic; | ||
|
||
namespace System.Text.Json.SourceGeneration | ||
{ | ||
/// <summary> | ||
/// Base JsonSerializerSourceGenerator. This class will invoke CodeGenerator within Execute | ||
/// to generate wanted output code for JsonSerializers. | ||
/// </summary> | ||
[Generator] | ||
public class JsonSerializerSourceGenerator : ISourceGenerator | ||
{ | ||
public void Execute(SourceGeneratorContext context) | ||
{ | ||
// Foreach type found, call code generator. | ||
StringBuilder sourceBuilder = new StringBuilder(@" | ||
using System; | ||
namespace HelloWorldGenerated | ||
{ | ||
public static class HelloWorld | ||
{ | ||
public static string SayHello() | ||
{ | ||
return ""Hello""; | ||
"); | ||
|
||
sourceBuilder.Append(@" | ||
} | ||
} | ||
}"); | ||
|
||
context.AddSource("helloWorldGenerated", SourceText.From(sourceBuilder.ToString(), Encoding.UTF8)); | ||
} | ||
|
||
public void Initialize(InitializationContext context) | ||
{ | ||
context.RegisterForSyntaxNotifications(() => new JsonSerializableSyntaxReceiver()); | ||
} | ||
|
||
// Temporary function for now that reads all types. Should search types with attribute JsonSerializable. | ||
internal class JsonSerializableSyntaxReceiver : ISyntaxReceiver | ||
{ | ||
public List<TypeDeclarationSyntax> GeneratorInputTypes = new List<TypeDeclarationSyntax>(); | ||
|
||
public void OnVisitSyntaxNode(SyntaxNode syntaxNode) | ||
{ | ||
// Get all the type decl in all syntax tree. | ||
if (syntaxNode is TypeDeclarationSyntax tds) | ||
{ | ||
GeneratorInputTypes.Add(tds); | ||
} | ||
} | ||
} | ||
|
||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
...stem.Text.Json/System.Text.Json.SourceGeneration/System.Text.Json.SourceGeneration.csproj
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,24 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFrameworks>netstandard2.0</TargetFrameworks> | ||
<CLSCompliant>false</CLSCompliant> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="$(MicrosoftCodeAnalysisCSharpWorkspacesVersion)" PrivateAssets="all" /> | ||
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="$(MicrosoftCodeAnalysisAnalyzersVersion)" PrivateAssets="all" /> | ||
<PackageReference Include="Microsoft.DotNet.Build.Tasks.Packaging" Version="$(MicrosoftDotNetBuildTasksPackagingVersion)" PrivateAssets="all" /> | ||
|
||
<!-- update dependencies brought in transitively from CodeAnalysis to avoid referencing 1.x packages --> | ||
<PackageReference Include="NETStandard.Library" Version="2.0.3" ExcludeAssets="All" /> | ||
<PackageReference Include="System.Composition" Version="1.4.1" /> | ||
|
||
<PackageDestination Include="analyzers\dotnet\cs" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<Compile Include="JsonSourceGenerator.cs" /> | ||
</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
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