Esempio n. 1
0
        /// <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 CsvSerializer_A_Must_Strip_Double_Quotes_And_Delimiter()
        {
            // Here we'll create a string array and then join it with currently used delimiter to create a string which the serializer should assert.
            string[] expectedValueArray = new string[]
            {
                "\"" + Output.Filename + "\"",
                "\"" + Output.Id + "\"",
                Output.Quantity.ToString(),
                Output.Price.ToString("0.00", System.Globalization.CultureInfo.InvariantCulture)
            };
            string assertedValue = string.Join(Delimiter, expectedValueArray);

            // Here we'll screw up Filename and Id of CsvOutput to add some double quotes and delimiters
            Output.Filename = ";\"" + Output.Filename + ";\"";
            Output.Id       = "\";;\"" + Output.Id + "\"\";;;\"";

            Assert.AreEqual(CsvSerializer.ToCsvString(), assertedValue);
        }
Esempio n. 3
0
        /// <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);
        }