Add project files.
This commit is contained in:
16
Model/Player.cs
Normal file
16
Model/Player.cs
Normal file
@@ -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; }
|
||||
|
||||
}
|
||||
}
|
||||
52
Program.cs
Normal file
52
Program.cs
Normal file
@@ -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<Player> players = new List<Player>();
|
||||
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<Player>
|
||||
{
|
||||
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<Player> players, string filePath)
|
||||
{
|
||||
string json = JsonSerializer.Serialize(players);
|
||||
await File.WriteAllTextAsync(filePath, json); // <-- async versie
|
||||
}
|
||||
|
||||
private static async Task<List<Player>> DeserializePlayersAsync(string filePath)
|
||||
{
|
||||
string json = await File.ReadAllTextAsync(filePath); // <-- async versie
|
||||
List<Player> players = JsonSerializer.Deserialize<List<Player>>(json);
|
||||
return players ?? new List<Player>(); // kleine extra check
|
||||
}
|
||||
}
|
||||
}
|
||||
10
Serialization_Demo.csproj
Normal file
10
Serialization_Demo.csproj
Normal file
@@ -0,0 +1,10 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
25
Serialization_Demo.sln
Normal file
25
Serialization_Demo.sln
Normal file
@@ -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
|
||||
Reference in New Issue
Block a user