I have the following models:
public class InputFile
{
public string Path { get; set; } // absolute path of file
public string Content { get; set; } // full content of the json file
}
and
public class InputModel
{
public List<InputFile> Files { get; set; }
}
What I need to do is to read a bunch of JSON files(around 1000) from my hard drive and convert them into the InputModel
format. The Path
is the absolute path of the file and Content
is file content. Note that I am reading only JSON files so the content itself is a JSON file.This is the code:
public void Parse(string collectedInputPath) // Command Line Input
{
List<string> directories = new List<string>();
directories.Add(collectedInputPath);
directories.AddRange(LoadSubDirs(collectedInputPath));
InputModel input = new InputModel();
string body = string.Empty;
foreach(string directory in directories) // Going through all directories
{
foreach (string filePath in Directory.GetFiles(directory)) // Adding every file to the Input Model List
{
string content = System.IO.File.ReadAllText(filePath);
string fp = JsonConvert.SerializeObject(filePath);
string jsonbody = JsonConvert.SerializeObject(content);
body += $"{{\"Path\" : {filePath}, \"Content\": {content}}},";
}
}
body += "]}";
body = "{\"Files\":[" + body;
input = JsonConvert.DeserializeObject<InputModel>(body);
Solve(input);
}
private List<string> LoadSubDirs(string dir)
{
string[] subdirectoryEntries = Directory.GetDirectories(dir);
List<string> subDirectoryList = new List<string>();
foreach (string subDirectory in subdirectoryEntries)
{
subDirectoryList.Add(subDirectory);
subDirectoryList.AddRange(LoadSubDirs(subDirectory));
}
return subDirectoryList;
}
The collectedInputPath
is the path of the root directory provided via user input. I have to check inside every directory under the root directory and read every JSON file.
I'm getting this error:
How should I correct this?