Skip to content

Commit

Permalink
Merge pull request #128 from ignatandrei/127-jsonpolymorphicgenerator
Browse files Browse the repository at this point in the history
  • Loading branch information
ignatandrei authored Oct 4, 2023
2 parents c2e8fa3 + 09ddb49 commit 7896639
Show file tree
Hide file tree
Showing 19 changed files with 579 additions and 0 deletions.
59 changes: 59 additions & 0 deletions v2/book/examples/JsonPolymorphicGenerator.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@

<h1>RSCG nr 68 : JsonPolymorphicGenerator</h1>

<h2>Info</h2>
Nuget : <a href="https://www.nuget.org/packages/GoLive.Generator.JsonPolymorphicGenerator/" target="_blank">https://www.nuget.org/packages/GoLive.Generator.JsonPolymorphicGenerator/</a>

<p>You can find more details at : <a href="https://github.com/surgicalcoder/JsonPolymorphicGenerator" target="_blank"> https://github.com/surgicalcoder/JsonPolymorphicGenerator</a></p>

<p>Author :surgicalcoder</p>

<p>Source : <a href="https://github.com/surgicalcoder/JsonPolymorphicGenerator" target="_blank">https://github.com/surgicalcoder/JsonPolymorphicGenerator</a> </p>

<h2>About</h2>

Generating JsonDerivedType to be added to the base class

<h2>
How to use
</h2>
<h3>
Add reference to the <a href="https://www.nuget.org/packages/GoLive.Generator.JsonPolymorphicGenerator/" target="_blank">JsonPolymorphicGenerator</a> in the csproj
</h3>
<img src="images/JsonPolymorphicGenerator/JsonPolymorphicGeneratorDemo.csproj.png" width="580" height="580" />

<h3>This was for me the <b>starting</b> code</h3>

<br />
I have <b>coded</b> the file Program.cs
<br />
<img src="images/JsonPolymorphicGenerator/csFiles/Program.cs.png" width="580" height="580" />
<hr />

<br />
I have <b>coded</b> the file Person.cs
<br />
<img src="images/JsonPolymorphicGenerator/csFiles/Person.cs.png" width="580" height="580" />
<hr />
<h3>And here are the <i>generated</i> files</h3>

<br />
The file <i>generated</i> is Person.g.cs
<br />
<img src="images/JsonPolymorphicGenerator/generated/Person.g.cs.png" width="580" height="580" />

<p>
You can download the code and this page as pdf from
<a target="_blank" href='https://ignatandrei.github.io/RSCG_Examples/v2/docs/JsonPolymorphicGenerator'>
https://ignatandrei.github.io/RSCG_Examples/v2/docs/JsonPolymorphicGenerator
</a>
</p>


<p>
You can see the whole list at
<a target="_blank" href='https://ignatandrei.github.io/RSCG_Examples/v2/docs/List-of-RSCG'>
https://ignatandrei.github.io/RSCG_Examples/v2/docs/List-of-RSCG
</a>
</p>

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="GoLive.Generator.JsonPolymorphicGenerator" Version="1.0.4">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>

<PropertyGroup>
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
<CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GX</CompilerGeneratedFilesOutputPath>
</PropertyGroup>

</Project>
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System.Text.Json.Serialization;

namespace JsonPolymorphicGeneratorDemo;

[JsonPolymorphic]
public abstract partial class Person
{

public string? Name { get; set; }
public abstract string Data();
}

public class Teacher : Person
{
public override string Data()
{
return "Class Teacher:" + Name;
}
}
public class Student : Person
{
public override string Data()
{
return "Class Student:" + Name;
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json/polymorphism?pivots=dotnet-7-0
using JsonPolymorphicGeneratorDemo;
using System.Text.Json;

Person[] persons = new Person[2];
persons[0] = new Student() { Name="Student Ignat"};

persons[1] = new Teacher() { Name = "Teacher Ignat" };
JsonSerializerOptions opt = new ()
{
WriteIndented = true
};
var ser = JsonSerializer.Serialize(persons, opt);
Console.WriteLine(ser);
var p = JsonSerializer.Deserialize<Person[]>(ser);
if(p != null)
foreach (var item in p)
{
Console.WriteLine(item.Data());
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System.Text.Json.Serialization;

namespace JsonPolymorphicGeneratorDemo
{
[JsonDerivedType(typeof(JsonPolymorphicGeneratorDemo.Teacher), "Teacher")]
[JsonDerivedType(typeof(JsonPolymorphicGeneratorDemo.Student), "Student")]
public partial class Person
{
}
}


20 changes: 20 additions & 0 deletions v2/rscg_examples/JsonPolymorphicGenerator/description.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"generator":{
"name":"JsonPolymorphicGenerator",
"nuget":[
"https://www.nuget.org/packages/GoLive.Generator.JsonPolymorphicGenerator/"
],
"link":"https://github.com/surgicalcoder/JsonPolymorphicGenerator",
"author":"surgicalcoder",
"source":"https://github.com/surgicalcoder/JsonPolymorphicGenerator"
},
"data":{
"goodFor":["Generating JsonDerivedType to be added to the base class"],
"csprojDemo":"JsonPolymorphicGeneratorDemo.csproj",
"csFiles":["Program.cs","Person.cs"]
},
"links":{
"blog":"",
"video":""
}
}
1 change: 1 addition & 0 deletions v2/rscg_examples/JsonPolymorphicGenerator/nuget.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Source Code Generator for System.Text.Json JsonDerivedType attributes on polymorphic classes
39 changes: 39 additions & 0 deletions v2/rscg_examples/JsonPolymorphicGenerator/readme.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# JsonPolymorphicGenerator
c# / .net Source Code Generator for System.Text.Json JsonDerivedType attributes on polymorphic classes

## Usage

For this, your base classes need the `partial` and `abstract` key words, and be decorated with `JsonPolymorphic`, and there need to be derived types in that same assembly for this to work.

An example of this is:

```
[JsonPolymorphic]
public abstract partial class BaseClass
{
public string Property1 { get; set; }
}
```

This will then generate a partial class, that is decorated with the `JsonDerivedType` attribute, and use the class name as the discriminator.

```
[JsonDerivedType(typeof(GoLive.JsonPolymorphicGenerator.Playground.InheritedClass1), "InheritedClass1")]
[JsonDerivedType(typeof(GoLive.JsonPolymorphicGenerator.Playground.InheritedClass2), "InheritedClass2")]
public partial class BaseClass
{
}
```

You can now transform the text of the attributes that gets spat out! You have a number of options, that gets added to an `.editorconfig`, such as:

```
root = true

[*.cs]
jsonpolymorphicgenerator.text_preappend = JSON_
jsonpolymorphicgenerator.text_transform = return classname.GetHashCode().ToString()
jsonpolymorphicgenerator.text_postappend = _A
```

For the `jsonpolymorphicgenerator.text_transform` option, you have to provide valid c# code, that returns a string - there are 2 input variables - `classname` and `namespacename`
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.7.34031.279
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "JsonPolymorphicGeneratorDemo", "JsonPolymorphicGeneratorDemo\JsonPolymorphicGeneratorDemo.csproj", "{154ABA32-3173-4D0E-AD7C-0E18FE31627A}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{154ABA32-3173-4D0E-AD7C-0E18FE31627A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{154ABA32-3173-4D0E-AD7C-0E18FE31627A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{154ABA32-3173-4D0E-AD7C-0E18FE31627A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{154ABA32-3173-4D0E-AD7C-0E18FE31627A}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {921028FA-3559-4ECF-A918-CD360698FCF9}
EndGlobalSection
EndGlobal
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="GoLive.Generator.JsonPolymorphicGenerator" Version="1.0.4">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>

<PropertyGroup>
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
<CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GX</CompilerGeneratedFilesOutputPath>
</PropertyGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System.Text.Json.Serialization;

namespace JsonPolymorphicGeneratorDemo;

[JsonPolymorphic]
public abstract partial class Person
{

public string? Name { get; set; }
public abstract string Data();
}

public class Teacher : Person
{
public override string Data()
{
return "Class Teacher:" + Name;
}
}
public class Student : Person
{
public override string Data()
{
return "Class Student:" + Name;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json/polymorphism?pivots=dotnet-7-0
using JsonPolymorphicGeneratorDemo;
using System.Text.Json;

Person[] persons = new Person[2];
persons[0] = new Student() { Name="Student Ignat"};

persons[1] = new Teacher() { Name = "Teacher Ignat" };
JsonSerializerOptions opt = new ()
{
WriteIndented = true
};
var ser = JsonSerializer.Serialize(persons, opt);
Console.WriteLine(ser);
var p = JsonSerializer.Deserialize<Person[]>(ser);
if(p != null)
foreach (var item in p)
{
Console.WriteLine(item.Data());
}
31 changes: 31 additions & 0 deletions v2/rscg_examples/JsonPolymorphicGenerator/video.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"step_1_exec":"clipchamp.exe launch",
"step_2_text": "Welcome to Roslyn Examples - the introduction is automatically generated",
"step_3_text":"If you want to see more examples , see [List Of RSCG] https://ignatandrei.github.io/RSCG_Examples/v2/docs/List-of-RSCG",
"step_4_browser":"https://ignatandrei.github.io/RSCG_Examples/v2/docs/List-of-RSCG",
"step_5_text": "My name is Andrei Ignat and I am deeply fond of Roslyn Source Code Generator. ",

"step_6_text": "Today I will present JsonPolymorphicGenerator that is good for Generating JsonDerivedType to be added to the base class .",
"step_7_browser":"https://www.nuget.org/packages/GoLive.Generator.JsonPolymorphicGenerator/",
"step_8_text": "You can download the code from https://ignatandrei.github.io/RSCG_Examples/v2/docs/JsonPolymorphicGenerator)",
"step_9_browser":"https://ignatandrei.github.io/RSCG_Examples/v2/docs/JsonPolymorphicGenerator",
"step_10_text":" Here is the code ",
"step_11_exec":"explorer.exe /select,C:\\gth\\RSCG_Examples\\v2\\rscg_examples\\JsonPolymorphicGenerator\\src\\JsonPolymorphicGeneratorDemo\\JsonPolymorphicGeneratorDemo.csproj",
"step_12_text": "So , let's start the project ",
"step_13_exec": "C:\\Program Files\\Microsoft Visual Studio\\2022\\Community\\Common7\\IDE\\devenv.exe C:\\gth\\RSCG_Examples\\v2\\rscg_examples\\JsonPolymorphicGenerator\\src\\JsonPolymorphicGeneratorDemo\\JsonPolymorphicGeneratorDemo.csproj",

"step_14_text": "You put the [GoLive.Generator.JsonPolymorphicGenerator](https://www.nuget.org/packages/GoLive.Generator.JsonPolymorphicGenerator/) into the csproj ",

"step_15_exec": "C:\\Program Files\\Microsoft Visual Studio\\2022\\Community\\Common7\\IDE\\devenv.exe /edit C:\\gth\\RSCG_Examples\\v2\\rscg_examples\\JsonPolymorphicGenerator\\src\\JsonPolymorphicGeneratorDemo\\JsonPolymorphicGeneratorDemo.csproj",

"step_16_text": "I have used the JsonPolymorphicGenerator in those files",


"step_17_exec":"C:\\Program Files\\Microsoft Visual Studio\\2022\\Community\\Common7\\IDE\\devenv.exe /edit C:\\gth\\RSCG_Examples\\v2\\rscg_examples\\JsonPolymorphicGenerator\\src\\JsonPolymorphicGeneratorDemo\\Person.cs",

"step_18_exec":"C:\\Program Files\\Microsoft Visual Studio\\2022\\Community\\Common7\\IDE\\devenv.exe /edit C:\\gth\\RSCG_Examples\\v2\\rscg_examples\\JsonPolymorphicGenerator\\src\\JsonPolymorphicGeneratorDemo\\Program.cs",

"step_19_hide": "hide"


}
Loading

0 comments on commit 7896639

Please sign in to comment.