Class LudoBoardFileHandlerGson
- All Implemented Interfaces:
FileHandler<Board>
This class implements the FileHandler
interface for Board
objects,
specifically tailored for LudoGameBoard
instances. It uses the Gson library to serialize
Ludo game boards to JSON format and deserialize them from JSON files.
The JSON structure includes properties for the board's name, description, size, background image, player colors, various tile index arrays (start, track start, finish start, finish), start area size, total track tile count, and a list of tiles with their respective IDs, coordinates, next tile IDs, and types.
Reflection is used internally to set certain fields (like index arrays and integer
properties) on the LudoGameBoard
object during deserialization to avoid overly complex
constructor logic or numerous setter methods for these internal state details.
- See Also:
-
Constructor Summary
Constructors -
Method Summary
-
Constructor Details
-
LudoBoardFileHandlerGson
public LudoBoardFileHandlerGson()
-
-
Method Details
-
readFile
Reads a Ludo game board configuration from a JSON file at the specified path.- Specified by:
readFile
in interfaceFileHandler<Board>
- Parameters:
path
- The path to the JSON file.- Returns:
- A
Board
(specifically aLudoGameBoard
) object deserialized from the file. - Throws:
IOException
- if an error occurs during file reading or parsing.
-
writeFile
Writes a list of Ludo game boards to a JSON file at the specified path.- Specified by:
writeFile
in interfaceFileHandler<Board>
- Parameters:
path
- The path to the JSON file.boards
- A list containing theBoard
(expected to be aLudoGameBoard
) to be written. Only the first board in the list is processed.- Throws:
IOException
- if an error occurs during file writing, such as if a file with the same name already exists or if there's a general I/O issue.IllegalArgumentException
- if the provided list of boards is null or empty.
-