|
static bool | ReadJsonFile (string path, Action< JsonObject > successCallback, Action failCallback=null, Action fileNotFoundCallback=null, Action< string, Exception > overrideExceptionHandling=null) |
| Offers two error callbacks for when a file is not found and for when a file failed to parse correctly, and one for when the file has been succesfully parsed into a json object. With the latter, you can use the parsed object to build out whatever you need. More...
|
|
static JsonValue | ReadJsonStream (TextReader jsonReader, Action< Exception > exceptionHandler=null) |
| Tries reading Json from a TextReader and returns the parsed JsonValue. Includes an optional exceptionHandler argument More...
|
|
static JsonValue | SafeReadJsonString (string jsonText, Action< Exception > exceptionHandler=null) |
| Tries reading a Json string and returns the parsed JsonValue. Includes an optional exceptionHandler argument. More...
|
|
Definition at line 9 of file FileReader.cs.
◆ HandleException()
static void BGC.IO.FileReader.HandleException |
( |
string |
path, |
|
|
Exception |
excp |
|
) |
| |
|
inlinestaticprivate |
Definition at line 125 of file FileReader.cs.
127 string[] errorMessage;
129 if (excp is ParsingException parsingExcp)
131 errorMessage =
new string[]
133 "Internal parsing error.",
137 "Suggested corrective action:",
138 parsingExcp.correctiveAction
143 errorMessage =
new string[]
145 "JSON parsing error",
150 $
"Line {jsonExcp.Position.line + 1}, Column {jsonExcp.Position.column + 1}" 155 errorMessage =
new string[]
157 "Exception Encountered",
static void HandleExceptionFiles(string path, string[] errorMessage)
The exception that is thrown when a JSON message cannot be parsed.
◆ HandleExceptionFiles()
static void BGC.IO.FileReader.HandleExceptionFiles |
( |
string |
path, |
|
|
string [] |
errorMessage |
|
) |
| |
|
inlinestaticprivate |
Definition at line 166 of file FileReader.cs.
168 string fileName = Path.GetFileName(path);
170 string directoryPath = Path.GetDirectoryName(path);
171 string directoryName = Path.GetFileName(directoryPath);
172 string errorDirectory = $
"Error{directoryName}";
173 string errorDirectoryPath = Path.Combine(Path.GetDirectoryName(directoryPath), errorDirectory);
174 string errorFilePath = Path.Combine(errorDirectoryPath, fileName);
175 string errorLogPath = $
"{errorFilePath}_error.txt";
177 if (!Directory.Exists(errorDirectoryPath))
179 Directory.CreateDirectory(errorDirectoryPath);
182 if (File.Exists(errorFilePath))
184 File.Delete(errorFilePath);
186 File.Move(path, errorFilePath);
188 if (File.Exists(errorLogPath))
190 File.Delete(errorLogPath);
193 File.WriteAllLines(errorLogPath, errorMessage);
◆ ReadJsonFile()
static bool BGC.IO.FileReader.ReadJsonFile |
( |
string |
path, |
|
|
Action< JsonObject > |
successCallback, |
|
|
Action |
failCallback = null , |
|
|
Action |
fileNotFoundCallback = null , |
|
|
Action< string, Exception > |
overrideExceptionHandling = null |
|
) |
| |
|
inlinestatic |
Offers two error callbacks for when a file is not found and for when a file failed to parse correctly, and one for when the file has been succesfully parsed into a json object. With the latter, you can use the parsed object to build out whatever you need.
There is also an optional override for exception handling.
- Parameters
-
path | File Path to load |
successCallback | Executed only on successful read |
failCallback | Notifies caller of failure |
fileNotFoundCallback | Notifies caller of failure to locate file |
overrideExceptionHandling | Optionally replaces default exception handling |
- Returns
- Whether the json file was successfully read
Definition at line 24 of file FileReader.cs.
References LightJson.Serialization.JsonReader.ParseFile().
Referenced by BGC.Users.ProfileData.Deserialize(), BGC.Audio.Calibration.Initialize(), and BGC.Study.ProtocolManager.LoadProtocolSet().
31 bool jsonFileRead =
false;
35 if (!File.Exists(path))
37 Debug.LogError($
"Unable to find Json file at path \"{path}\"");
38 fileNotFoundCallback?.Invoke();
50 Debug.LogError($
"Error parsing file at {path}: {excp.Message}");
52 failCallback?.Invoke();
54 if (overrideExceptionHandling != null)
56 overrideExceptionHandling.Invoke(path, excp);
static void HandleException(string path, Exception excp)
static JsonValue ParseFile(string path)
Creates a JsonValue by reading the given file.
Represents a reader that can read JsonValues.
◆ ReadJsonStream()
static JsonValue BGC.IO.FileReader.ReadJsonStream |
( |
TextReader |
jsonReader, |
|
|
Action< Exception > |
exceptionHandler = null |
|
) |
| |
|
inlinestatic |
Tries reading Json from a TextReader and returns the parsed JsonValue. Includes an optional exceptionHandler argument
- Parameters
-
jsonReader | As reader containing serialized Json |
exceptionHandler | An optional exception handler |
- Returns
- Whether the json was successfully read
Definition at line 74 of file FileReader.cs.
References LightJson.JsonValue.Null, and LightJson.Serialization.JsonReader.Parse().
78 Debug.Assert(jsonReader != null);
88 Debug.LogError($
"Error parsing Json by text: {excp.Message}");
90 exceptionHandler?.Invoke(excp);
static readonly JsonValue Null
Represents a null JsonValue.
A wrapper object that contains a valid JSON value.
Represents a reader that can read JsonValues.
◆ SafeReadJsonString()
static JsonValue BGC.IO.FileReader.SafeReadJsonString |
( |
string |
jsonText, |
|
|
Action< Exception > |
exceptionHandler = null |
|
) |
| |
|
inlinestatic |
Tries reading a Json string and returns the parsed JsonValue. Includes an optional exceptionHandler argument.
- Parameters
-
jsonText | As string containing deserialized Json |
exceptionHandler | An optional exception handler |
- Returns
- Whether the json was successfully read
Definition at line 103 of file FileReader.cs.
References LightJson.JsonValue.Null, and LightJson.Serialization.JsonReader.Parse().
107 Debug.Assert(
string.IsNullOrEmpty(jsonText) ==
false);
117 Debug.LogError($
"Error parsing Json by text: {excp.Message}");
119 exceptionHandler?.Invoke(excp);
static readonly JsonValue Null
Represents a null JsonValue.
A wrapper object that contains a valid JSON value.
Represents a reader that can read JsonValues.
The documentation for this class was generated from the following file: