Class LadderGameBoardFileHandlerGson

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

public class LadderGameBoardFileHandlerGson extends Object implements FileHandler<Board>
FileHandler implementation for Board objects.

This class provides methods for reading and writing Board objects to and from JSON files. It uses the Gson library for serialization and deserialization.

  • Constructor Details

    • LadderGameBoardFileHandlerGson

      public LadderGameBoardFileHandlerGson()
  • Method Details

    • readFile

      public Board readFile(String path) throws IOException
      Reads a file at the given path and returns a list of Board objects. If there is only one board in the file, it will be returned as a list with one element. If the file does not exist or cannot be read, an empty list will be returned.
      Specified by:
      readFile in interface FileHandler<Board>
      Parameters:
      path - The path to the file.
      Returns:
      A list of Board objects or null if the file does not exist or cannot be read.
      Throws:
      IOException - if an error occurs while reading the file.
    • writeFile

      public void writeFile(String path, List<Board> boards) throws IOException
      Writes a list of Board objects to a JSON file at the given path.
      Specified by:
      writeFile in interface FileHandler<Board>
      Parameters:
      path - The path to the file.
      boards - The list of Board objects to write.
      Throws:
      IOException - If an error occurs while writing to the file.