private void SaveToFile(ScoreContainer container) { using (var sw = new StreamWriter(FilePath)) { var obj = JsonConvert.SerializeObject(container, Formatting.Indented); sw.WriteLine(obj); } }
public void Store(string playerId, Score score) { if (File.Exists(FilePath)) { ScoreContainer scoreContainer = null; using (var sr = new StreamReader(FilePath)) { var text = sr.ReadToEnd(); try { scoreContainer = JsonConvert.DeserializeObject <ScoreContainer>(text); } catch (Exception e) { scoreContainer = new ScoreContainer(); } } if (scoreContainer == null) { scoreContainer = new ScoreContainer(); } scoreContainer.Add(playerId, score); SaveToFile(scoreContainer); } else { var scoreContainer = new ScoreContainer(); scoreContainer.Add(playerId, score); SaveToFile(scoreContainer); } }