Merge pull request #315 from sp-tarkov/async-deserialize
Small async changes
This commit is contained in:
@@ -97,31 +97,28 @@ public class ImporterUtil
|
||||
{
|
||||
try
|
||||
{
|
||||
using (var fs = new FileStream(file, FileMode.Open, FileAccess.Read))
|
||||
onReadCallback?.Invoke(file);
|
||||
|
||||
// Get the set method to update the object
|
||||
var setMethod = GetSetMethod(
|
||||
_fileUtil.StripExtension(file).ToLower(),
|
||||
loadedType,
|
||||
out var propertyType,
|
||||
out var isDictionary
|
||||
);
|
||||
|
||||
var fileDeserialized = await DeserializeFileAsync(file, propertyType);
|
||||
|
||||
onObjectDeserialized?.Invoke(file, fileDeserialized);
|
||||
|
||||
lock (dictionaryLock)
|
||||
{
|
||||
onReadCallback?.Invoke(file);
|
||||
|
||||
// Get the set method to update the object
|
||||
var setMethod = GetSetMethod(
|
||||
_fileUtil.StripExtension(file).ToLower(),
|
||||
loadedType,
|
||||
out var propertyType,
|
||||
out var isDictionary
|
||||
setMethod.Invoke(
|
||||
result,
|
||||
isDictionary
|
||||
? [_fileUtil.StripExtension(file), fileDeserialized]
|
||||
: new[] { fileDeserialized }
|
||||
);
|
||||
|
||||
var fileDeserialized = await DeserializeFileAsync(fs, file, propertyType);
|
||||
|
||||
onObjectDeserialized?.Invoke(file, fileDeserialized);
|
||||
|
||||
lock (dictionaryLock)
|
||||
{
|
||||
setMethod.Invoke(
|
||||
result,
|
||||
isDictionary
|
||||
? [_fileUtil.StripExtension(file), fileDeserialized]
|
||||
: new[] { fileDeserialized }
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
@@ -159,14 +156,14 @@ public class ImporterUtil
|
||||
}
|
||||
}
|
||||
|
||||
private async Task<object> DeserializeFileAsync(FileStream fs, string file, Type propertyType)
|
||||
private async Task<object> DeserializeFileAsync(string file, Type propertyType)
|
||||
{
|
||||
if (propertyType.IsGenericType && propertyType.GetGenericTypeDefinition() == typeof(LazyLoad<>))
|
||||
{
|
||||
return CreateLazyLoadDeserialization(file, propertyType);
|
||||
}
|
||||
|
||||
return await Task.Run(() => _jsonUtil.DeserializeFromFileStream(fs, propertyType));
|
||||
return await _jsonUtil.DeserializeFromFileAsync(file, propertyType);
|
||||
}
|
||||
|
||||
private object CreateLazyLoadDeserialization(string file, Type propertyType)
|
||||
|
||||
@@ -78,6 +78,23 @@ public class JsonUtil
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Convert JSON into an object from a file asynchronously
|
||||
/// </summary>
|
||||
/// <param name="file">The JSON File to read</param>
|
||||
/// <returns>T</returns>
|
||||
public async Task<T?> DeserializeFromFileAsync<T>(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<T>(fs, jsonSerializerOptionsNoIndent);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Convert JSON into an object from a file
|
||||
/// </summary>
|
||||
@@ -97,6 +114,24 @@ public class JsonUtil
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Convert JSON into an object from a file asynchronously
|
||||
/// </summary>
|
||||
/// <param name="file">The JSON File to read</param>
|
||||
/// <param name="type">The type of the object to deserialize to</param>
|
||||
/// <returns>object</returns>
|
||||
public async Task<object?> 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);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Convert JSON into an object from a FileStream
|
||||
/// </summary>
|
||||
@@ -108,6 +143,17 @@ public class JsonUtil
|
||||
return JsonSerializer.Deserialize(fs, type, jsonSerializerOptionsNoIndent);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Convert JSON into an object from a FileStream asynchronously
|
||||
/// </summary>
|
||||
/// <param name="fs">The file stream to deserialize</param>
|
||||
/// <param name="type">The type of the object to deserialize to</param>
|
||||
/// <returns></returns>
|
||||
public async Task<object?> DeserializeFromFileStreamAsync(FileStream fs, Type type)
|
||||
{
|
||||
return await JsonSerializer.DeserializeAsync(fs, type, jsonSerializerOptionsNoIndent);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Convert an object into JSON
|
||||
/// </summary>
|
||||
|
||||
Reference in New Issue
Block a user