diff --git a/Libraries/SPTarkov.Server.Core/Utils/JsonUtil.cs b/Libraries/SPTarkov.Server.Core/Utils/JsonUtil.cs index 940c1e00..4ec01814 100644 --- a/Libraries/SPTarkov.Server.Core/Utils/JsonUtil.cs +++ b/Libraries/SPTarkov.Server.Core/Utils/JsonUtil.cs @@ -112,6 +112,23 @@ public class JsonUtil } } + /// + /// Convert JSON into an object from a file asynchronously + /// + /// The JSON File to read + /// T + public async Task DeserializeFromFileAsync(string file) + { + if (!File.Exists(file)) + { + return default; + } + + await using FileStream fs = new(file, FileMode.Open, FileAccess.Read, FileShare.Read, bufferSize: 4096, useAsync: true); + + return await JsonSerializer.DeserializeAsync(fs, jsonSerializerOptionsNoIndent); + } + /// /// Convert JSON into an object from a file /// @@ -131,6 +148,24 @@ public class JsonUtil } } + /// + /// Convert JSON into an object from a file asynchronously + /// + /// The JSON File to read + /// The type of the object to deserialize to + /// object + public async Task DeserializeFromFileAsync(string file, Type type) + { + if (!File.Exists(file)) + { + return default; + } + + await using FileStream fs = new(file, FileMode.Open, FileAccess.Read, FileShare.Read, bufferSize: 4096, useAsync: true); + + return await JsonSerializer.DeserializeAsync(fs, type, jsonSerializerOptionsNoIndent); + } + /// /// Convert JSON into an object from a FileStream /// @@ -142,6 +177,17 @@ public class JsonUtil return JsonSerializer.Deserialize(fs, type, jsonSerializerOptionsNoIndent); } + /// + /// Convert JSON into an object from a FileStream asynchronously + /// + /// The file stream to deserialize + /// The type of the object to deserialize to + /// + public async Task DeserializeFromFileStreamAsync(FileStream fs, Type type) + { + return await JsonSerializer.DeserializeAsync(fs, type, jsonSerializerOptionsNoIndent); + } + /// /// Convert an object into JSON ///