Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit d2c2ae5

Browse files
committedJun 25, 2024
First commit !
1 parent f54cd72 commit d2c2ae5

16 files changed

+656
-0
lines changed
 

‎.editorconfig

Lines changed: 376 additions & 0 deletions
Large diffs are not rendered by default.

‎.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,3 +396,4 @@ FodyWeavers.xsd
396396

397397
# JetBrains Rider
398398
*.sln.iml
399+
*.idea/

‎Directory.Build.props

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<Project>
2+
<PropertyGroup>
3+
<Deterministic>true</Deterministic>
4+
<RunCodeAnalysis>true</RunCodeAnalysis>
5+
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
6+
<EnforceCodeStyleInBuild>true</EnforceCodeStyleInBuild>
7+
<MSBuildTreatWarningsAsErrors>true</MSBuildTreatWarningsAsErrors>
8+
<CodeAnalysisTreatWarningsAsErrors>true</CodeAnalysisTreatWarningsAsErrors>
9+
</PropertyGroup>
10+
</Project>

‎JsonPolymorphicDemo.sln

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.0.31903.59
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{1B52C506-A8FA-4A1B-9CEA-8CA9CD7D2932}"
7+
EndProject
8+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "App", "src\App\App.csproj", "{B4187B1E-12FD-44EB-9252-8571B525D7C0}"
9+
EndProject
10+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "files", "files", "{703428A9-121A-4DDE-A849-E2C54D824D6D}"
11+
ProjectSection(SolutionItems) = preProject
12+
.editorconfig = .editorconfig
13+
Directory.Build.props = Directory.Build.props
14+
global.json = global.json
15+
README.md = README.md
16+
EndProjectSection
17+
EndProject
18+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{8BCBD2CA-03E8-43EB-9BB1-77C5F1CB0D4F}"
19+
EndProject
20+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tests", "test\Tests\Tests.csproj", "{7ED30432-6A1B-45AF-B383-68DEF8708E62}"
21+
EndProject
22+
Global
23+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
24+
Debug|Any CPU = Debug|Any CPU
25+
Release|Any CPU = Release|Any CPU
26+
EndGlobalSection
27+
GlobalSection(SolutionProperties) = preSolution
28+
HideSolutionNode = FALSE
29+
EndGlobalSection
30+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
31+
{B4187B1E-12FD-44EB-9252-8571B525D7C0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
32+
{B4187B1E-12FD-44EB-9252-8571B525D7C0}.Debug|Any CPU.Build.0 = Debug|Any CPU
33+
{B4187B1E-12FD-44EB-9252-8571B525D7C0}.Release|Any CPU.ActiveCfg = Release|Any CPU
34+
{B4187B1E-12FD-44EB-9252-8571B525D7C0}.Release|Any CPU.Build.0 = Release|Any CPU
35+
{7ED30432-6A1B-45AF-B383-68DEF8708E62}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
36+
{7ED30432-6A1B-45AF-B383-68DEF8708E62}.Debug|Any CPU.Build.0 = Debug|Any CPU
37+
{7ED30432-6A1B-45AF-B383-68DEF8708E62}.Release|Any CPU.ActiveCfg = Release|Any CPU
38+
{7ED30432-6A1B-45AF-B383-68DEF8708E62}.Release|Any CPU.Build.0 = Release|Any CPU
39+
EndGlobalSection
40+
GlobalSection(NestedProjects) = preSolution
41+
{B4187B1E-12FD-44EB-9252-8571B525D7C0} = {1B52C506-A8FA-4A1B-9CEA-8CA9CD7D2932}
42+
{7ED30432-6A1B-45AF-B383-68DEF8708E62} = {8BCBD2CA-03E8-43EB-9BB1-77C5F1CB0D4F}
43+
EndGlobalSection
44+
EndGlobal

‎README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,9 @@
11
# JsonPolymorphicDemo
2+
```
23
Serialization and deserialization of polymorphic types
4+
```
5+
6+
In this demo, i m using [json attributes](https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json/polymorphism?pivots=dotnet-7-0) in order to support serialization and deserialization of polymorphic types.
7+
>
8+
9+
**`Tools`** : net 8.0

‎global.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"sdk": {
3+
"version": "8.0.0",
4+
"rollForward": "latestMajor",
5+
"allowPrerelease": true
6+
}
7+
}

‎src/App/App.csproj

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
</Project>

‎src/App/Models/Circle.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace App.Models;
2+
3+
public record Circle(double Radius) : Shape
4+
{
5+
public override double ComputeArea()
6+
{
7+
return Math.PI * Radius * Radius;
8+
}
9+
}

‎src/App/Models/Rectangle.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace App.Models;
2+
3+
public record Rectangle(double Width, double Height) : Shape
4+
{
5+
public override double ComputeArea()
6+
{
7+
return Width * Height;
8+
}
9+
}

‎src/App/Models/Shape.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using System.Text.Json.Serialization;
2+
3+
namespace App.Models;
4+
5+
[JsonPolymorphic(UnknownDerivedTypeHandling = JsonUnknownDerivedTypeHandling.FailSerialization)]
6+
[JsonDerivedType(typeof(Circle), nameof(Circle))]
7+
[JsonDerivedType(typeof(Square), nameof(Square))]
8+
[JsonDerivedType(typeof(Rectangle), nameof(Rectangle))]
9+
public abstract record Shape
10+
{
11+
public abstract double ComputeArea();
12+
}

‎src/App/Models/Square.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace App.Models;
2+
3+
public record Square(double Side) : Shape
4+
{
5+
public override double ComputeArea()
6+
{
7+
return Side * Side;
8+
}
9+
}

‎src/App/Program.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using System.Text.Json;
2+
using App.Models;
3+
4+
var shapes = new Shape[]
5+
{
6+
new Circle(5),
7+
new Square(6),
8+
new Rectangle(5, 6)
9+
};
10+
11+
foreach (var shape in shapes)
12+
{
13+
var json = JsonSerializer.Serialize(shape);
14+
Console.WriteLine(json);
15+
}

‎test/Tests/CircleTests.cs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using System.Text.Json;
2+
using App.Models;
3+
using AutoFixture;
4+
using FluentAssertions;
5+
6+
namespace Tests;
7+
8+
public class CircleTests
9+
{
10+
[Fact]
11+
public void Should_Serialize_Circles()
12+
{
13+
// arrange
14+
Shape shape = new Fixture().Create<Circle>();
15+
16+
// act
17+
var json = JsonSerializer.Serialize(shape);
18+
19+
// assert
20+
json.Should().NotBeNullOrEmpty();
21+
json.Should().Contain("Circle");
22+
json.Should().Contain("Radius");
23+
}
24+
25+
[Fact]
26+
public void Should_Deserialize_Circles()
27+
{
28+
// arrange
29+
Shape shape = new Fixture().Create<Circle>();
30+
var json = JsonSerializer.Serialize(shape);
31+
32+
// act
33+
var circle = JsonSerializer.Deserialize<Shape>(json);
34+
35+
// assert
36+
circle.Should().NotBeNull();
37+
circle.Should().BeOfType<Circle>();
38+
}
39+
}

‎test/Tests/RectangleTests.cs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using System.Text.Json;
2+
using App.Models;
3+
using AutoFixture;
4+
using FluentAssertions;
5+
6+
namespace Tests;
7+
8+
public class RectangleTests
9+
{
10+
[Fact]
11+
public void Should_Serialize_Rectangles()
12+
{
13+
// arrange
14+
Shape shape = new Fixture().Create<Rectangle>();
15+
16+
// act
17+
var json = JsonSerializer.Serialize(shape);
18+
19+
// assert
20+
json.Should().NotBeNullOrEmpty();
21+
json.Should().Contain("Rectangle");
22+
json.Should().Contain("Width");
23+
json.Should().Contain("Height");
24+
}
25+
26+
[Fact]
27+
public void Should_Deserialize_Rectangles()
28+
{
29+
// arrange
30+
Shape shape = new Fixture().Create<Rectangle>();
31+
var json = JsonSerializer.Serialize(shape);
32+
33+
// act
34+
var square = JsonSerializer.Deserialize<Rectangle>(json);
35+
36+
// assert
37+
square.Should().NotBeNull();
38+
square.Should().BeOfType<Rectangle>();
39+
}
40+
}

‎test/Tests/SquareTests.cs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using System.Text.Json;
2+
using App.Models;
3+
using AutoFixture;
4+
using FluentAssertions;
5+
6+
namespace Tests;
7+
8+
public class SquareTests
9+
{
10+
[Fact]
11+
public void Should_Serialize_Squares()
12+
{
13+
// arrange
14+
Shape shape = new Fixture().Create<Square>();
15+
16+
// act
17+
var json = JsonSerializer.Serialize(shape);
18+
19+
// assert
20+
json.Should().NotBeNullOrEmpty();
21+
json.Should().Contain("Square");
22+
json.Should().Contain("Side");
23+
}
24+
25+
[Fact]
26+
public void Should_Deserialize_Squares()
27+
{
28+
// arrange
29+
Shape shape = new Fixture().Create<Square>();
30+
var json = JsonSerializer.Serialize(shape);
31+
32+
// act
33+
var square = JsonSerializer.Deserialize<Shape>(json);
34+
35+
// assert
36+
square.Should().NotBeNull();
37+
square.Should().BeOfType<Square>();
38+
}
39+
}

‎test/Tests/Tests.csproj

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net8.0</TargetFramework>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
<Nullable>enable</Nullable>
7+
8+
<IsPackable>false</IsPackable>
9+
<IsTestProject>true</IsTestProject>
10+
</PropertyGroup>
11+
12+
<ItemGroup>
13+
<PackageReference Include="AutoFixture" Version="4.18.1" />
14+
<PackageReference Include="coverlet.collector" Version="6.0.0" />
15+
<PackageReference Include="FluentAssertions" Version="6.12.0" />
16+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
17+
<PackageReference Include="xunit" Version="2.5.3" />
18+
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.3" />
19+
</ItemGroup>
20+
21+
<ItemGroup>
22+
<Using Include="Xunit" />
23+
</ItemGroup>
24+
25+
<ItemGroup>
26+
<ProjectReference Include="..\..\src\App\App.csproj" />
27+
</ItemGroup>
28+
29+
</Project>

0 commit comments

Comments
 (0)
Please sign in to comment.