diff --git a/Model/Player.cs b/Model/Player.cs new file mode 100644 index 0000000..6183897 --- /dev/null +++ b/Model/Player.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Serialization_Demo.Model +{ + public class Player + { + + public string Name { get; set; } + public int Score { get; set; } + + } +} diff --git a/Program.cs b/Program.cs new file mode 100644 index 0000000..2361706 --- /dev/null +++ b/Program.cs @@ -0,0 +1,52 @@ +// See https://aka.ms/new-console-template for more information + +using Serialization_Demo.Model; +using System.Runtime.CompilerServices; +using System.Text.Json; + +namespace Serialization_Demo +{ + class Program + { + private static List players = new List(); + private static string filePath = "players.json"; + static async Task Main(string[] args) // <-- HIER async Task Main + { + if (File.Exists(filePath)) + { + players = await DeserializePlayersAsync(filePath); // <-- await + Console.WriteLine("Players loaded from file:"); + foreach (var player in players) + { + Console.WriteLine($"Name: {player.Name}, Score: {player.Score}"); + } + } + else + { + players = new List + { + new Player { Name = "Player 1", Score = 100 }, + new Player { Name = "Player 2", Score = 200 }, + new Player { Name = "Player 3", Score = 300 } + }; + + await SerializePlayersAsync(players, filePath); // <-- await + Console.WriteLine($"Players ({players.Count}) written to file"); + } + } + + + private static async Task SerializePlayersAsync(List players, string filePath) + { + string json = JsonSerializer.Serialize(players); + await File.WriteAllTextAsync(filePath, json); // <-- async versie + } + + private static async Task> DeserializePlayersAsync(string filePath) + { + string json = await File.ReadAllTextAsync(filePath); // <-- async versie + List players = JsonSerializer.Deserialize>(json); + return players ?? new List(); // kleine extra check + } + } +} \ No newline at end of file diff --git a/Serialization_Demo.csproj b/Serialization_Demo.csproj new file mode 100644 index 0000000..2150e37 --- /dev/null +++ b/Serialization_Demo.csproj @@ -0,0 +1,10 @@ + + + + Exe + net8.0 + enable + enable + + + diff --git a/Serialization_Demo.sln b/Serialization_Demo.sln new file mode 100644 index 0000000..c6d1d13 --- /dev/null +++ b/Serialization_Demo.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.13.35919.96 d17.13 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Serialization_Demo", "Serialization_Demo.csproj", "{78E0108A-B3F3-4CD3-BCA7-4B5289859AB9}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {78E0108A-B3F3-4CD3-BCA7-4B5289859AB9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {78E0108A-B3F3-4CD3-BCA7-4B5289859AB9}.Debug|Any CPU.Build.0 = Debug|Any CPU + {78E0108A-B3F3-4CD3-BCA7-4B5289859AB9}.Release|Any CPU.ActiveCfg = Release|Any CPU + {78E0108A-B3F3-4CD3-BCA7-4B5289859AB9}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {4DB8CC8B-4B64-4EBE-B876-2291FE7F5E07} + EndGlobalSection +EndGlobal