Class Json

java.lang.Object
edu.ntnu.stud.utils.Json

public class Json extends Object
Utility class for handling JSON files.
  • Constructor Details

    • Json

      public Json()
  • Method Details

    • getJsonArray

      public static org.json.JSONArray getJsonArray(String fileName) throws FileNotFoundException
      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

      public static void jsonToIngredientList(List<Ingredient> listName, String fileName)
      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

      public static void listToJson(List<Ingredient> listName, String fileName)
      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.