Impementing toJson() and fromJson() Method in GameSession and Round class

This commit is contained in:
Felix Kirchner
2025-04-30 14:34:14 +02:00
parent 9f315a5db8
commit 6b6e824860
2 changed files with 41 additions and 0 deletions

View File

@@ -38,6 +38,30 @@ class GameSession {
'winner: $winner]');
}
/// Converts the GameSession object to a JSON map.
Map<String, dynamic> toJson() => {
'gameTitle': gameTitle,
'gameHasPointLimit': gameHasPointLimit,
'players': players,
'playerScores': playerScores,
'roundNumber': roundNumber,
'isGameFinished': isGameFinished,
'winner': winner,
'roundList': roundList.map((e) => e.toJson()).toList()
};
/// Creates a GameSession object from a JSON map.
GameSession.fromJson(Map<String, dynamic> json)
: gameTitle = json['gameTitle'],
gameHasPointLimit = json['gameHasPointLimit'],
players = List<String>.from(json['players']),
playerScores = List<int>.from(json['playerScores']),
roundNumber = json['roundNumber'],
isGameFinished = json['isGameFinished'],
winner = json['winner'],
roundList =
(json['roundList'] as List).map((e) => Round.fromJson(e)).toList();
/// Returns the length of all player names combined.
int getLengthOfPlayerNames() {
int length = 0;