java.lang.Object
edu.ntnu.idatt1002.k01g08.fta.util.FileManager
Contains static methods for reading and writing teams and tournaments to JSON files.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic javax.json.JsonStructure
Reads and returns a JSON structure from a file.static javax.json.JsonArray
loadJsonArray
(File file) Reads and returns a JSON array from a file.static javax.json.JsonObject
loadJsonObject
(File file) Reads and returns a JSON object from a file.static TeamRegister
loadTeamRegister
(File file) Loads a team register from a file.static Tournament
loadTournament
(File file, TeamRegister teamRegister) Loads a tournament from a file, pulling teams from a team register.static boolean
saveIfAbsent
(javax.json.JsonObject json, File file) Saves a JSON structure to a file, if the file does not already exist.static void
Saves a JSON structure to a file.static void
saveTeamRegister
(TeamRegister teamRegister, File file) Saves a team register to a file.static void
saveTournament
(File file, Tournament tournament) Saves a tournament to a file.
-
Constructor Details
-
FileManager
public FileManager()
-
-
Method Details
-
loadJson
Reads and returns a JSON structure from a file.- Parameters:
file
- a file to read from- Returns:
- a JSON structure read from the file
- Throws:
IOException
- if the file for some reason could not be opened for readingjavax.json.JsonException
- if a JSON object or array cannot be created due to incorrect representation
-
loadJsonArray
Reads and returns a JSON array from a file.- Parameters:
file
- a file to read from- Returns:
- a JSON structure read from the file
- Throws:
IOException
- if the file for some reason could not be opened for readingjavax.json.JsonException
- if a JSON array cannot be created due to incorrect representation
-
loadJsonObject
Reads and returns a JSON object from a file.- Parameters:
file
- a file to read from- Returns:
- a JSON structure read from the file
- Throws:
IOException
- if the file for some reason could not be opened for readingjavax.json.JsonException
- if a JSON object cannot be created due to incorrect representation
-
saveJson
Saves a JSON structure to a file.- Parameters:
json
- A JSON structure to save to a filefile
- the file to save the JSON object to- Throws:
IOException
- if file for some reason could not be saved
-
saveIfAbsent
Saves a JSON structure to a file, if the file does not already exist. Returns true if successful.- Parameters:
json
- A JSON structure to save to a filefile
- the file to save the JSON object to- Returns:
- true if the file was created
- Throws:
IOException
- if file for some reason could not be saved
-
loadTeamRegister
Loads a team register from a file. Convenience method for parseTeamRegister(loadJsonArray(file)).- Parameters:
file
- a file to load a team register from- Returns:
- a team register parsed from the contents of the file
- Throws:
IOException
- if the file for some reason could not be opened for reading
-
saveTeamRegister
Saves a team register to a file.- Parameters:
teamRegister
- a team register to savefile
- the file to save to- Throws:
IOException
- if the team register for some reason could not be saved
-
loadTournament
Loads a tournament from a file, pulling teams from a team register. Convenience method for parseTournament(loadJsonObject(file), teamRegister).- Parameters:
file
- a file to load a tournament from- Returns:
- a tournament parsed from the contents of the file
- Throws:
IOException
- if the file for some reason could not be opened for reading
-
saveTournament
Saves a tournament to a file.- Parameters:
tournament
- a tournament to savefile
- the file to save to- Throws:
IOException
- if the tournament for some reason could not be saved
-