/// <summary> /// Receives generating data type and index to be casted into CsvProduct class which is then used to fill up the csvOutput. /// /// Writes summary in the cnosole for each callback. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="data"></param> /// <param name="index"></param> /// <returns></returns> public async override Task <Task> ParseAsync <T>(T data, int index) { CsvProduct product = (CsvProduct)Convert.ChangeType(data, typeof(CsvProduct)); CsvOutput output = new CsvOutput { Filename = GetFileReader().GetFilename(), Id = product.Id, Quantity = product.Quantity, Price = product.Price }; await SemaphoreSlim.WaitAsync(); try { if (AsyncFileWriter) { CsvOutputSerializer csv = new CsvOutputSerializer(output); await GetStreamWriter().WriteLineAsync(csv.ToCsvString()); } else { await AppendOutput(output); } Console.WriteLine("[CSV PARSER ] Processing line: {0} of file: {1}", index + 1, GetFileReader().GetFilename()); } finally { SemaphoreSlim.Release(); } return(Task.CompletedTask); }
public void Setup() { // We'll be using ; as a delimiter to generate a CSV output (single line). Delimiter = ";"; // CsvOutput with values to be used in this test context Output = new CsvOutput() { Filename = "test.csv", Id = "ABCDEF", Quantity = 1000, Price = 7.62 }; CsvSerializer = new CsvOutputSerializer(Output, Delimiter); }
/// <summary> /// Receives genereric data with an index and converts it into a JSON serialized object of class JsonProduct by converting the generic data into a string. /// /// Prints summary in the console and fills the csvOutput later used to generate final output file. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="data"></param> /// <param name="index"></param> /// <returns></returns> public async override Task <Task> ParseAsync <T>(T data, int index) { try { string json = (string)Convert.ChangeType(data, typeof(string)); var product = JsonConvert.DeserializeObject <JsonProduct>(json); CsvOutput output = new CsvOutput { Filename = GetFileReader().GetFilename(), Id = product.Id, Quantity = product.Quantity, Price = product.Price }; await SemaphoreSlim.WaitAsync(); try { if (AsyncFileWriter) { CsvOutputSerializer csv = new CsvOutputSerializer(output); await GetStreamWriter().WriteLineAsync(csv.ToCsvString()); } else { await AppendOutput(output); } Console.WriteLine("[JSON PARSER] Processing line: {0} of file: {1}", index, GetFileReader().GetFilename()); } finally { SemaphoreSlim.Release(); } } catch (Exception) { Console.WriteLine("[JSON PARSER] ERROR: Unable to parse JSON object on line {0} of file: {1}", index, GetFileReader().GetFilename()); } return(Task.CompletedTask); }