I have multiple JSON files with data and multiple classes with properties mapped to fields in the JSON files. I have a generic method for loading the JSON like this:
public static TResult LoadFromJSON<TResult>(string pathToJSON)
{
using (var reader = new StreamReader(pathToJSON))
{
var json = reader.ReadToEnd();
var records = JsonConvert.DeserializeObject<TResult>(json);
return records;
}
}
and then another method that passes in the Class object I am targeting into the above LoadFromJSON
public SortingData GetJSONTestData()
{
var pathJSON = @"..\..\TestData\sortData.json";
var sortCriteriaJSON = TestDataLoader.LoadFromJSON<SortingData>(pathJSON);
return sortCriteriaJSON;
}
what I want to do is make that second method generic, so instead of having SortingData class (in 2 places) and sortData.json file, I can pass in the Class (in both places) and the JSON file dynamically..... so I don't have to duplicate the method over and over just for a different class. So something like this:
public <dynamicObject> GetJSONTestData()
{
var pathJSON = @"..\..\TestData\<dynamic json file>";
var sortCriteriaJSON = TestDataLoader.LoadFromJSON<dynamicObject>(pathJSON);
return sortCriteriaJSON;
}
Not sure how to go about this. Any ideas?