Added fromJson, toJson

This commit is contained in:
2025-11-18 23:16:57 +01:00
parent 07d623d963
commit d86de09042
3 changed files with 40 additions and 0 deletions

View File

@@ -21,4 +21,21 @@ class Game {
String toString() {
return 'Game{\n\tid: $id,\n\tname: $name,\n\tplayers: $players,\n\tgroup: $group,\n\twinner: $winner\n}';
}
/// Creates a Game instance from a JSON object.
Game.fromJson(Map<String, dynamic> json)
: id = json['id'],
name = json['name'],
players = json['players'] != null
? (json['players'] as List)
.map((playerJson) => Player.fromJson(playerJson))
.toList()
: null,
group = json['group'] != null ? Group.fromJson(json['group']) : null,
winner = json['winner'] ?? '';
/// Converts the Game instance to a JSON object.
String toJson() {
return 'Game{id: $id,name: $name,players: $players,group: $group,winner: $winner}';
}
}

View File

@@ -13,4 +13,17 @@ class Group {
String toString() {
return 'Group{id: $id, name: $name,members: $members}';
}
/// Creates a Group instance from a JSON object.
Group.fromJson(Map<String, dynamic> json)
: id = json['id'],
name = json['name'],
members = (json['members'] as List)
.map((memberJson) => Player.fromJson(memberJson))
.toList();
/// Converts the Group instance to a JSON object.
String toJson() {
return 'Group{id: $id, name: $name,members: $members}';
}
}

View File

@@ -10,4 +10,14 @@ class Player {
String toString() {
return 'Player{id: $id,name: $name}';
}
/// Creates a Player instance from a JSON object.
Player.fromJson(Map<String, dynamic> json)
: id = json['id'],
name = json['name'];
/// Converts the Player instance to a JSON object.
String toJson() {
return 'Player{id: $id,name: $name}';
}
}