Skip to content

Commit 87dc487

Browse files
JSON inheritance
1 parent 7131375 commit 87dc487

File tree

4 files changed

+110
-0
lines changed

4 files changed

+110
-0
lines changed
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>net7.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.6.33417.168
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "JsonInheritance", "JsonInheritance.csproj", "{791864E3-4954-441F-8DFA-DA9E04A7E7F8}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{791864E3-4954-441F-8DFA-DA9E04A7E7F8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{791864E3-4954-441F-8DFA-DA9E04A7E7F8}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{791864E3-4954-441F-8DFA-DA9E04A7E7F8}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{791864E3-4954-441F-8DFA-DA9E04A7E7F8}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {53D3306D-D685-43EA-968A-30D87A60FE3D}
24+
EndGlobalSection
25+
EndGlobal
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System.Text.Json.Serialization;
2+
3+
namespace JsonInheritance;
4+
public record class Game(Guid GameId, string GameType, string PlayerName)
5+
{
6+
// init modifier required for JSON deserialization
7+
[JsonInclude]
8+
public ICollection<Move> Moves { get; private init; } = new List<Move>();
9+
10+
public override string ToString() => $"""
11+
{GameId}: {GameType}, {PlayerName}
12+
{string.Join(':', Moves)}
13+
""";
14+
}
15+
16+
[JsonPolymorphic(TypeDiscriminatorPropertyName = "$discriminator")]
17+
[JsonDerivedType(typeof(Move<ColorField>), typeDiscriminator: "color")]
18+
[JsonDerivedType(typeof(Move<ShapeColorField>), typeDiscriminator: "shape")]
19+
public abstract record class Move(Guid GameId, Guid MoveId, int MoveNumber);
20+
public record class Move<TField>(Guid GameId, Guid MoveId, int MoveNumber)
21+
: Move(GameId, MoveId, MoveNumber)
22+
{
23+
public required ICollection<TField> Fields { get; init; }
24+
25+
public override string ToString() => string.Join(":", Fields);
26+
}
27+
28+
public record class ColorField(string Color)
29+
{
30+
public override string ToString() => $"{Color}";
31+
}
32+
33+
public record class ShapeColorField(string Shape, string Color)
34+
{
35+
public override string ToString() => $"{Shape} {Color}";
36+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using JsonInheritance;
2+
3+
using System.Text.Json;
4+
5+
// create the game instances
6+
7+
Game game1 = new(Guid.NewGuid(), "6x4", "player1");
8+
game1.Moves.Add(new Move<ColorField>(game1.GameId, Guid.NewGuid(), 1)
9+
{
10+
Fields = new[] { new ColorField("red"), new ColorField("blue"), new ColorField("green"), new ColorField("blue") }
11+
});
12+
13+
Game game2 = new(Guid.NewGuid(), "5x5x4", "player2");
14+
game2.Moves.Add(new Move<ShapeColorField>(game2.GameId, Guid.NewGuid(), 1)
15+
{
16+
Fields = new[] { new ShapeColorField("circe", "red"), new ShapeColorField("square", "blue"), new ShapeColorField("circle", "green"), new ShapeColorField("rectangle", "yellow") }
17+
});
18+
19+
// serialize to JSON
20+
21+
JsonSerializerOptions options = new()
22+
{
23+
WriteIndented = true,
24+
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
25+
};
26+
27+
string json1 = JsonSerializer.Serialize(game1, options);
28+
Console.WriteLine(json1);
29+
30+
string json2 = JsonSerializer.Serialize(game2, options);
31+
Console.WriteLine(json2);
32+
33+
// deserialize from JSON
34+
35+
Game? game1b = JsonSerializer.Deserialize<Game>(json1, options);
36+
Console.WriteLine(game1b);
37+
38+
Game? game2b = JsonSerializer.Deserialize<Game>(json2, options);
39+
Console.WriteLine(game2b);

0 commit comments

Comments
 (0)