Class LudoBoardFileHandlerGson

java.lang.Object
edu.ntnu.idi.idatt.filehandler.LudoBoardFileHandlerGson
All Implemented Interfaces:
FileHandler<Board>

public class LudoBoardFileHandlerGson extends Object implements FileHandler<Board>
LudoBoardFileHandlerGson.

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 Details

    • LudoBoardFileHandlerGson

      public LudoBoardFileHandlerGson()
  • Method Details

    • readFile

      public Board readFile(String path) throws IOException
      Reads a Ludo game board configuration from a JSON file at the specified path.
      Specified by:
      readFile in interface FileHandler<Board>
      Parameters:
      path - The path to the JSON file.
      Returns:
      A Board (specifically a LudoGameBoard) object deserialized from the file.
      Throws:
      IOException - if an error occurs during file reading or parsing.
    • writeFile

      public void writeFile(String path, List<Board> boards) throws IOException
      Writes a list of Ludo game boards to a JSON file at the specified path.
      Specified by:
      writeFile in interface FileHandler<Board>
      Parameters:
      path - The path to the JSON file.
      boards - A list containing the Board (expected to be a LudoGameBoard) 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.