2. DataHandler.JSONDataHandler
Types
PariahJSON
Represents a JSON file on disk along with its parsed data.
-
Fields:
- string FileName (read-only): The name of the JSON file (without extension).
- string FilePath (internal set): The directory path where the file resides.
- JsonObject Data (read-only): The parsed JSON content.
-
Constructor:
PariahJSON(string fileName, string filePath, JsonObject data)
Initializes a new instance with the given file name, file path, and JSON data.
Methods
CreateJsonFile(string Filename, string FileLocation, JsonObject defaultData)
Creates a new .json file containing the provided default data.
- Parameters:
- Filename: Name of the file (without “.json”).
- FileLocation: Directory in which to create the file.
- defaultData: The JsonObject to write as the file’s initial contents.
- Filename: Name of the file (without “.json”).
- Returns: Task
- Exceptions:
- Exception if the directory doesn’t exist or a file with the same name already exists.
- Wraps any write errors in a generic Exception with an explanatory message.
- Exception if the directory doesn’t exist or a file with the same name already exists.
LoadJsonFile(string Filename, string FileLocation)
Loads and parses an existing .json file into a PariahJSON struct.
- Parameters:
- Filename: Name of the file (without “.json”).
- FileLocation: Directory where the file resides.
- Filename: Name of the file (without “.json”).
- Returns: Task containing the file’s name, path, and parsed JsonObject.
- Exceptions:
- Exception if the directory or file doesn’t exist, or on read/parse errors.
- Exception if the directory or file doesn’t exist, or on read/parse errors.
AddToJson(PariahJSON JsonData, string dataName, object data, SecureData Key)
Encrypts and adds a NEW data field to the in-memory JSON. If a field with that name already exists it throws — use UpdateJson to change (or add-or-update) an existing field. (Your IDE will remind you of this on hover.)
- Parameters:
- JsonData: Existing PariahJSON instance.
- dataName: JSON property name to add.
- data: The object to serialize & encrypt.
- Key: The SecureData key used for encryption. Required — there are no default keys anymore.
- JsonData: Existing PariahJSON instance.
- Returns: Task with the updated JSON (does not write to disk).
DeletefromJson(PariahJSON JsonData, string dataName)
Removes a property from the in-memory JSON.
- Parameters:
- JsonData: Existing PariahJSON instance.
- dataName: JSON property name to remove.
- JsonData: Existing PariahJSON instance.
- Returns: PariahJSON with the property removed (does not write to disk).
UpdateJson(PariahJSON JsonData, string dataName, object data, SecureData Key)
Add-or-update: replaces an existing JSON property with new encrypted data, or adds it if it isn't there yet. (Use AddToJson when you specifically want it to fail if the field already exists.)
- Parameters:
- JsonData: Existing PariahJSON instance.
- dataName: JSON property name to update.
- data: The new object to serialize & encrypt.
- Key: The SecureData key used for encryption. Required — there are no default keys anymore.
- JsonData: Existing PariahJSON instance.
- Returns: Task with the updated JSON (does not write to disk).
GetVariable(PariahJSON JsonData, string dataName, SecureData Key)
Retrieves and decrypts a previously added JSON property.
- Parameters:
- JsonData: Existing PariahJSON instance.
- dataName: JSON property name to retrieve.
- Key: The SecureData key used for decryption. Required — there are no default keys anymore.
- JsonData: Existing PariahJSON instance.
- Returns: Task — The decrypted object.
- Exceptions:
- Exception if the property is missing or not a string, or on decryption errors.
- Exception if the property is missing or not a string, or on decryption errors.
CheckIfVariableExists(PariahJSON JsonData, string dataName)
Checks whether a given property exists and is a string.
- Parameters:
- JsonData: Existing PariahJSON instance.
- dataName: JSON property name to check.
- JsonData: Existing PariahJSON instance.
- Returns: Task
SaveJson(PariahJSON JsonData)
Serializes and writes the in-memory JSON back to its original file location.
- Parameters:
- JsonData: The PariahJSON holding the file name, path, and updated data.
- JsonData: The PariahJSON holding the file name, path, and updated data.
- Returns: Task
- Exceptions:
- Exception if the directory or file doesn’t exist or on write errors.
- Exception if the directory or file doesn’t exist or on write errors.