I am using Newtonsoft.Json for reading a json file. I am trying to make a aysnc call to the json file to read its data but unfortunately it's not returning anything. I tried without async and it works perfectly, following is my code:
public static async Task<T> LoadAsync<T>(string filePath)
{
// filePath: any json file present locally on the disk
string basePath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase).Replace("file:\\", "");
string fullPath = Path.Combine(basePath, filePath);
using (var stream = File.OpenRead(fullPath))
{
var reader = new StreamReader(stream, Encoding.GetEncoding("iso-8859-1"));
var task = Task.Factory.StartNew(() => JsonConvert.DeserializeObject<T>(reader.ReadToEnd()));
var value = await task;
return value;
}
}
I tried to debug but debugger is not coming on "return value" in the above method and I am calling above method by following function:
private void GetDataFromJson()
{
var value = JsonUtilities.LoadAsync<TaxOffice>(Constant.TAXJSONINPUTFILE);
}
What can be the problem ? File is present locally on my computer.
LoadAsyncmethod isasyncthen you should be awaiting it. (Which in turn means thatGetDataFromJsonshould beasync Taskinstead ofvoid.)