Package edu.ntnu.stud.utils
Class Json
java.lang.Object
edu.ntnu.stud.utils.Json
Utility class for handling JSON files.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic void
addIngredientToJson
(String fileName, String ingName, String quantity) Adds an ingredient to a JSON file.static org.json.JSONArray
getJsonArray
(String fileName) Reads a JSON file and returns a JSONArray.static void
jsonToIngredientList
(List<Ingredient> listName, String fileName) Reads a JSON file and populates a list with the ingredients.static void
listToJson
(List<Ingredient> listName, String fileName) Writes a list of ingredients to a JSON file.
-
Constructor Details
-
Json
public Json()
-
-
Method Details
-
getJsonArray
Reads a JSON file and returns a JSONArray.- Parameters:
fileName
- the name of the JSON file to read.- Returns:
- a JSONArray containing the contents of the JSON file.
- Throws:
FileNotFoundException
- if the file is not found.
-
jsonToIngredientList
Reads a JSON file and populates a list with the ingredients.- Parameters:
listName
- the list to populate with ingredients.fileName
- the name of the JSON file to read.
-
listToJson
Writes a list of ingredients to a JSON file.- Parameters:
listName
- the list of ingredients to write to the JSON file.fileName
- the name of the JSON file to write to.
-
addIngredientToJson
public static void addIngredientToJson(String fileName, String ingName, String quantity) throws IllegalArgumentException Adds an ingredient to a JSON file.- Parameters:
fileName
- the name of the JSON file to add the ingredient to.ingName
- the name of the ingredient to add.quantity
- the quantity of the ingredient to add.- Throws:
IllegalArgumentException
- if the quantity is not a positive number.
-