Good Afternoon all,
I am writing a program that calls and API, maps the json response to a corresponding data model. then will ultimately write those maps to disk.
The following code snippet is the main entry point for the program.
public static async Task Main(string[] args)
{
try
{
await WriteFile(2010);
}
catch (Exception ex)
{
Console.WriteLine($"There was an exception: {ex.ToString()}");
}
}
public static async Task WriteFile(int year)
{
ApiHelper.InitializeClient();
var data = await DataLoader.LoadData(year);
StreamWriter writer = new StreamWriter(@"Data.txt");
writer.WriteLine(data.Id);
writer.Close();
}
The DataLoader is as follows
public static async Task<BigDataModel> LoadData(int year)
{
using (HttpResponseMessage response = await ApiHelper.ApiClient.GetAsync(GetUri(year)))
{
if (response.IsSuccessStatusCode)
{
DataModel data = await response.Content.ReadAsAsync<DataModel>();
return data.Meta;
}
else
{
throw new Exception(response.ReasonPhrase);
}
}
}
Within the if statement the program returns an exception regarding my json format being incorrect and needing to be deserialized. I have tested this system without attempting to map and array and it works correctly.
My Data Models are as followed
public class DataModel
{
public BigDataModel Meta { get; set; }
}
public class BigDataModel
{
public int Id { get; set; }
}
expected Json response
{
"projected":"value",
"actual":"value",
"meta":["data":"1","id":"data"]
}
What are the best practices when mapping a json object(array) to an object. Is deserialization a must or can I create a work around to write the data to disk.