You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
sae201_qwirkle/Qwirkle/QwirkleClassLibrary/Persistences/LeaderboardPersistenceJson.cs

27 lines
813 B

using System.Runtime.Serialization.Json;
using QwirkleClassLibrary.Players;
namespace QwirkleClassLibrary.Persistences;
public class LeaderboardPersistenceJson : ILeaderboardPersistence
{
public void SaveLeaderboard(Leaderboard leaderboard)
{
var serializer = new DataContractJsonSerializer(typeof(Leaderboard));
using (Stream writer = File.Create("Leaderboard.json"))
{
serializer.WriteObject(writer, leaderboard);
}
}
public Leaderboard LoadLeaderboard()
{
var serializer = new DataContractJsonSerializer(typeof(Leaderboard));
using (Stream reader = File.OpenRead("Leaderboard.json"))
{
return serializer.ReadObject(reader) as Leaderboard ?? throw new InvalidOperationException();
}
}
}