Example #1
0
        public void TestLocalized()
        {
            string NLNLTest = "Double;Float;Date\n1,1;2,2;23-12-2018 00:00:00";
            string ENUSTest = "Double;Float;Date\n1.1;2.2;12/23/2018 12:00:00 AM";


            var DeserializationOptions = new CsvDeserializationOptions
            {
                Culture   = CultureInfo.GetCultureInfo("en-us"),
                Separator = ';'
            };

            var Input = StringToStreamReader(ENUSTest);

            var Data = CsvSerializer.Deserialize <HasLocalizable>(Input, DeserializationOptions);

            Assert.Single(Data);
            Assert.Equal(1, Math.Floor(Data.First().Double));
            Assert.Equal(2, Math.Floor(Data.First().Float));
            Assert.Equal(23, Data.First().Date.Day);
            Assert.Equal(12, Data.First().Date.Month);
            Assert.Equal(2018, Data.First().Date.Year);

            DeserializationOptions.Culture = CultureInfo.GetCultureInfo("nl-nl");
            Input = StringToStreamReader(NLNLTest);
            Data  = CsvSerializer.Deserialize <HasLocalizable>(Input, DeserializationOptions);
            Assert.Single(Data);
            Assert.Equal(1, Math.Floor(Data.First().Double));
            Assert.Equal(2, Math.Floor(Data.First().Float));
            Assert.Equal(23, Data.First().Date.Day);
            Assert.Equal(12, Data.First().Date.Month);
            Assert.Equal(2018, Data.First().Date.Year);
        }
Example #2
0
        public void CsvSeedFiles_Deserialization_ShouldWork()
        {
            var appSettingsReader      = new AppSettingsReader();
            var dataFilesDirectoryPath = appSettingsReader.GetValue("SeedCsvDataFiles", typeof(string)).ToString();

            var modelTypes = ProcessingTools.Bio.Biorepositories.Data.Seed.Assembly.Assembly
                             .GetType()
                             .Assembly
                             .GetTypes()
                             .Where(t => !t.IsAbstract && t.IsClass && Attribute.IsDefined(t, typeof(FileNameAttribute)) && Attribute.IsDefined(t, typeof(CsvObjectAttribute)));

            foreach (var modelType in modelTypes)
            {
                Console.WriteLine(modelType.FullName);

                var fileNameAttribute = modelType.GetCustomAttributes(typeof(FileNameAttribute), false).FirstOrDefault() as FileNameAttribute;

                string fileName = string.Format("{0}/{1}", dataFilesDirectoryPath, fileNameAttribute.Name);
                Assert.IsTrue(File.Exists(fileName), $"FileName ‘{fileName}’ should be valid.");

                string csvText = File.ReadAllText(fileName);
                Assert.IsFalse(string.IsNullOrWhiteSpace(csvText), $"Text of ‘{fileName}’ should not be null or whitespace.");

                var serializer = new CsvSerializer();
                var items      = serializer.Deserialize(modelType, csvText)?.ToList();

                Assert.IsNotNull(items, "Deserialized items should not be null.");
                Assert.IsTrue(items.Count > 0, "Number of deserialized items should be greater than 0.");
            }
        }
Example #3
0
        public string Action(List <string> args)
        {
            var path = args[0];

            if (!path.EndsWith(".csv", StringComparison.OrdinalIgnoreCase))
            {
                path = path + ".csv";
            }

            if (!File.Exists(path))
            {
                throw new FileNotFoundException("File does not exist");
            }

            var items = File.ReadLines(path).ToList();

            if (items.Any() && items.ElementAt(0) == CsvSerializer.Headers(typeof(Item)))
            {
                items.RemoveAt(0);
            }

            var final = items.Select(item => CsvSerializer.Deserialize <Item>(item)).ToList();

            Storage.GetInstance().SetItems(final);

            return($"Loaded storage from `{path}`");
        }
Example #4
0
        public CsvTable GetTable(Uri location, CsvSettings settings)
        {
            if (location == null || string.IsNullOrEmpty(location.AbsolutePath))
            {
                throw new ArgumentNullException("location");
            }

            if (settings == null)
            {
                throw new ArgumentNullException("setting");
            }

            /*
             * if (!ValidateLocation(location))
             *  throw new Exception("file seems does not exists");
             */

            using (var reader = File.OpenText(location.LocalPath))
            {
                var csvFile = _serializer.Deserialize(reader, settings);

                csvFile.Location = location;

                return(csvFile);
            }
        }
        public void CsvSerializer_DeserializeType_AppliedOnInvalidObject_ShouldThrow()
        {
            const string CsvText = "Name,Year,Description\nJohn Smith,2015,No desription here\nJane Smith,2016,Desription here!";

            var serializer = new CsvSerializer();
            var result     = serializer.Deserialize(typeof(NameYearDescriptionSampleObject), CsvText);
        }
Example #6
0
        public void CsvSerializer_Deserialize_Enumerable_Test()
        {
            // Arrange
            var expectedList = new List <SimpleObject>
            {
                new SimpleObject {
                    Id = 27, Name = "Simple Object 27"
                },
                new SimpleObject {
                    Id = 28, Name = "Simple Object 28"
                }
            };
            var csvSerializer = new CsvSerializer();
            var csv           = "Id,Name" + Environment.NewLine
                                + "27,Simple Object 27" + Environment.NewLine
                                + "28,Simple Object 28" + Environment.NewLine;

            // Act
            var actualList = csvSerializer.Deserialize <List <SimpleObject> >(csv);

            // Assert
            for (int i = 0; i < actualList.Count; i++)
            {
                var actual   = actualList[i];
                var expected = expectedList[i];
                Assert.AreEqual(expected.Id, actual.Id, nameof(expected.Id) + "property should match");
                Assert.AreEqual(expected.Name, actual.Name, nameof(expected.Name) + "property should match");
            }
        }
Example #7
0
        /// <summary>
        /// read line and create object T
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="fileName"></param>
        /// <returns>Liste of T, one by line</returns>
        public ListSerializable <T> read <Y>(string filePath) where Y : ListSerializable <T>
        {
            ListSerializable <T> elements = (Y)Activator.CreateInstance(typeof(Y));

            using (var reader = new StreamReader(filePath))
            {
                int count = 0;
                while (!reader.EndOfStream)
                {
                    var line = reader.ReadLine();
                    if (count == 0 && HasHeader)
                    {
                        CheckCorrectHeader(line);
                    }
                    else
                    {
                        IStandardSerializer <T> serializer = new CsvSerializer <T>(Separator);
                        Serializable            element    = serializer.Deserialize(line);
                        elements.Add((T)element);
                    }
                    count++;
                }
            }

            return(elements);
        }
        private static void TestWriteReadMultiCsv()
        {
            var path = Path.ChangeExtension(Assembly.GetExecutingAssembly().Location, ".csv");

            using (var fs = new FileStream(path, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite | FileShare.Delete))
            {
                var dms1 = new DataModel[][]
                {
                    DataModel.GenerateData(1000, true).ToArray(),
                    DataModel.GenerateData(500, true).ToArray()
                };

                var csv = new CsvSerializer();
                csv.Serialize(new StreamWriter(fs), dms1);
                fs.Flush(true);
                fs.Position = 0;

                var dms2  = csv.Deserialize(new StreamReader(fs), new Type[] { typeof(DataModel), typeof(DataModel) });
                var dms2a = new DataModel[][]
                {
                    dms2[0].Cast <DataModel>().ToArray(),
                    dms2[1].Cast <DataModel>().ToArray()
                };

                Console.WriteLine($"Serialized data {(dms1[0].SequenceEqual(dms2[0].Cast<DataModel>()) ? "==" : "!=")} Deserialized data");
                Console.WriteLine($"Serialized data {(dms1[1].SequenceEqual(dms2[1].Cast<DataModel>()) ? "==" : "!=")} Deserialized data");

                Console.WriteLine($"Serialized data {(dms1[0].SequenceEqual(dms2a[0]) ? "==" : "!=")} Deserialized data");
                Console.WriteLine($"Serialized data {(dms1[1].SequenceEqual(dms2a[1]) ? "==" : "!=")} Deserialized data");
            }
        }
Example #9
0
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            if (File.Exists("Ustawienia.csv"))
            {
                // Deserialization
                IList<Ustawienia> deserializedData = null;
                using (var stream = new FileStream("Ustawienia.csv", FileMode.Open, FileAccess.Read))
                {
                    var cs = new CsvSerializer<Ustawienia>()
                    {
                        UseTextQualifier = true,
                    };

                    deserializedData = cs.Deserialize(stream);
                }
                Singleton.ustawienia = deserializedData.ElementAt(0);

                // Deserialization
                IList<Kombinacja> deserializedData3 = null;
                using (var stream = new FileStream("Kombinacje.csv", FileMode.Open, FileAccess.Read))
                {
                    var cs = new CsvSerializer<Kombinacja>()
                    {
                        UseTextQualifier = true,
                    };

                    deserializedData3 = cs.Deserialize(stream);
                }

                Singleton.merged = (List<Kombinacja>)deserializedData3;

                if (File.Exists("Dane.csv"))
                {
                    // Deserialization
                    IList<Dane> deserializedData2 = null;
                    using (var stream = new FileStream("Dane.csv", FileMode.Open, FileAccess.Read))
                    {
                        var cs = new CsvSerializer<Dane>()
                        {
                            UseTextQualifier = true,
                        };

                        deserializedData2 = cs.Deserialize(stream);
                    }

                    Singleton.dane = deserializedData2.ElementAt(0);
                    Application.Run(new Eksperyment());
                }
                else
                {
                    Application.Run(new Powitanie());
                }
            }
            else
            {
                Application.Run(new Panel());
            }
        }
        public static bool LoadCustomers()
        {
            string path = System.String.Format(@"{0}App_Data\us-500.csv", System.AppDomain.CurrentDomain.BaseDirectory);
            using (var stream = new FileStream(path, FileMode.Open, FileAccess.Read))
            {
                try
                {
                    var cs = new CsvSerializer<Customer>()
                    {
                        UseTextQualifier = false,
                        Separator = ',',
                        RowSeparator = "\n",
                        UseLineNumbers = false,
                        ColumnsContainedInHeader = true
                    };

                    Customers = cs.Deserialize(stream);
                    _loaded = true;
                }
                catch
                { }
            }

            return _loaded;
        }
Example #11
0
        public List <Song> ReadSongs()
        {
            string fileContent = File.ReadAllText(filePath);

            fileContent = SongsFileCleaner.CleanText(fileContent);

            return(CsvSerializer <Song> .Deserialize(delimiter, fileContent).ToList());
        }
Example #12
0
        public T Deserialize <T>(string file) where T : new()
        {
            CsvSerializer serializer = new CsvSerializer();

            using (FileStream fs = new FileStream(file, FileMode.OpenOrCreate))
            {
                return(serializer.Deserialize <T>(fs));
            }
        }
            public FilenameParsingFixtureData()
            {
                using var streamReader = RealFileSystem.File.OpenText(ResolvePath(FixtureFile));
                var serializer = new CsvSerializer();

                this.filenameParsingFixtureModels = serializer
                                                    .Deserialize <FilenameParsingFixtureModel>(streamReader)
                                                    .ToArray();
            }
Example #14
0
        public void CsvSerializer_MapObjectArrayToSerializeClass()
        {
            var values      = new[] { "field" };
            var testSubject = new CsvSerializer <TestData.Simple>();
            var row         = testSubject.Deserialize(TestHelpers.StringArrayToStream(values)).First();

            Assert.Equal(default(int), row.ID);
            Assert.Equal("field", row.Field);
        }
        public void CanDeserializeCsvFromExcel()
        {
            string csv = @"No;Name;Price;Weight;CreatedDate;IsSuspended
10;Deflector, Dust (For Rear Differential);200000;20,5;13/12/2019;FALSE
13;""Deflector; Tire; Filter"";150000;15,5;20/11/2019;TRUE";

            ExcelModel[] models = CsvSerializer.Deserialize <ExcelModel>(csv, hasHeaders: true, delimiter: ';', provider: CultureInfo.GetCultureInfo("id-ID"));
            models.Count().Should().Be(2);
        }
Example #16
0
        public void TestTooManyFieldsOnLine()
        {
            var Input = StringToStreamReader(
                "Prop1,Prop1"
                + Environment.NewLine
                + "A,B,C,D,E");

            Assert.ThrowsAny <CsvFormatException>(() => CsvSerializer.Deserialize <HasLocalizable>(Input));
        }
        public void MatchedAndUnmatchedTransactions()
        {
            string path     = @"C:\Users\Nick\Documents\Visual Studio 2013\Projects\LeanITrend\Engine\bin\Debug\";
            string pathname = path + "transactions.csv";
            // This part of the test is just to look at the JsonConvert
            //string txt;
            //using (StreamReader sr = new StreamReader(pathname))
            //{
            //    txt = sr.ReadToEnd();
            //    sr.Close();
            //}
            //int index = txt.IndexOf("\r\n", System.StringComparison.Ordinal);
            //string titlesremoved = txt.Substring(index + 2);

            int counter = 0;
            List <OrderTransaction> list = new List <OrderTransaction>();

            OrderTransactionProcessor processor = new OrderTransactionProcessor();

            using (StreamReader sr = new StreamReader(pathname))
            {
                string line = sr.ReadLine();    // read the header but do not count it.

                while (!sr.EndOfStream)
                {
                    line = sr.ReadLine();
                    if (line != null && line.Contains("Symbol"))
                    {
                        continue;
                    }
                    Assert.IsNotNull(line);
                    counter++;
                    OrderTransaction t = new OrderTransaction();
                    CsvSerializer.Deserialize(",", line, ref t, false);
                    list.Add(t);
                    processor.ProcessTransaction(t);
                }
                sr.Close();
            }
            var csv = CsvSerializer.Serialize(",", processor.Trades, true);

            using (StreamWriter sw = new StreamWriter(path + "Trades.csv"))
            {
                foreach (var s in csv)
                {
                    sw.WriteLine(s);
                }
                sw.Flush();
                sw.Close();
            }
            var x = JsonConvert.SerializeObject(list);

            Assert.IsTrue(counter == list.Count);
            Assert.IsTrue(processor.TotalProfit == 52.75m);
            Assert.IsTrue(processor.TotalCommission == -26m);
        }
        public void CsvCanBeDeserializedToPrivateType()
        {
            string csv = "\"Name\"\r\n\"CSV Serializer\"";

            PrivateModel[] items = CsvSerializer.Deserialize <PrivateModel>(csv, hasHeaders: true);
            items.Length.Should().Be(1);
            PrivateModel item = items.Single();

            item.Name.Should().Be("CSV Serializer");
        }
Example #19
0
        static void Main()
        {
            var cs = new CsvSerializer <Example>()
            {
                HasHeader = true
            };
            var csv = cs.Serialize(CreateExamples(100000).ToArray());

            cs.Deserialize(csv);
        }
Example #20
0
        public void DoubleQuotesAreUnescapedOnDeserializing()
        {
            typeof(EscapeTest).IsPublic.Should().BeFalse();
            string csv = "\"Tony \"\"Iron Man\"\" Stark\"";

            EscapeTest[] items = CsvSerializer.Deserialize <EscapeTest>(csv);
            items.Length.Should().Be(1);
            EscapeTest item = items[0];

            item.Name.Should().Be("Tony \"Iron Man\" Stark");
        }
Example #21
0
        public void DeserializeTest()
        {
            var cs = new CsvSerializer <Example>
            {
                HasHeader = true
            };

            var examples = cs.Deserialize(CreateExamplesCsv());

            CollectionAssert.AreEqual(CreateExamples(), examples);
        }
Example #22
0
        public void TestSeparatorsNonDefault()
        {
            var Reader = StringToStreamReader("Field1;Field2\n\"Hello\";\"World\"");
            var Result = CsvSerializer.Deserialize <HasFields>(Reader, new CsvDeserializationOptions {
                Separator = ';', UseTextQualifier = true
            });

            Assert.Single(Result);
            Assert.Equal("Hello", Result.First().Field1);
            Assert.Equal("World", Result.First().Field2);
        }
        public void TestDeserialize()
        {
            var csv = new CsvSerializer(CultureInfo.InvariantCulture);

            var sr = new StringReader(TestData.Csv);

            var result = csv.Deserialize(sr, new Type[] { typeof(DataModel), typeof(DataModel2) });

            Assert.IsTrue(TestData.ArrayOfModels[0].OfType <DataModel>().SequenceEqual(result[0].OfType <DataModel>()));
            Assert.IsTrue(TestData.ArrayOfModels[1].OfType <DataModel2>().SequenceEqual(result[1].OfType <DataModel2>()));
        }
Example #24
0
        public void TestCreateWithCell()
        {
            var table = Table.Create(3, 3);

            table.Set(new Cell(0, 0, "index"), new Cell(1, 0, "name"), new Cell(2, 0, "address"),
                      new Cell(0, 1, "1"), new Cell(1, 1, "Stas"), new Cell(2, 1, "127.0.0.1"),
                      new Cell(0, 2, "2"), new Cell(1, 2, "Ury"), new Cell(2, 2, "192.168.0.1"));

            var textTable = CsvSerializer.Serialize(table, "test_table.csv");

            var oldTable = CsvSerializer.Deserialize("test_table.csv");
        }
Example #25
0
        public void CommasInStringDontSplitString()
        {
            typeof(CommaTest).IsPublic.Should().BeFalse();
            string csv = "\"Stark, Tony\",\"Stark\"\r\n\"Banner, Bruce\",\"Banner\"";

            CommaTest[] items = CsvSerializer.Deserialize <CommaTest>(csv);
            items.Length.Should().Be(2);
            items[0].Name.Should().Be("Stark, Tony");
            items[0].LastName.Should().Be("Stark");
            items[1].Name.Should().Be("Banner, Bruce");
            items[1].LastName.Should().Be("Banner");
        }
Example #26
0
        public void TestDeserializeGeneric()
        {
            var csvSerializer = new CsvSerializer();

            var content = CreateContent();

            var expected = CreateRecords();

            var actual = csvSerializer.Deserialize <List <Customer> >(content);

            AssertUtilities.Equal(expected, actual);
        }
Example #27
0
        public void TestSepartorConflicting()
        {
            var Reader  = StringToStreamReader("sep=,\nField1,Field2\n\"Hello\",\"World\"");
            var Options = new CsvDeserializationOptions {
                Separator = ';', UseTextQualifier = true
            };
            var Result = CsvSerializer.Deserialize <HasFields>(Reader, Options);

            Assert.Single(Result);
            Assert.Equal("Hello", Result.First().Field1);
            Assert.Equal("World", Result.First().Field2);
            Assert.Equal(','.ToString(), Options.Separator.ToString());
        }
Example #28
0
        public void DeserializationTests(string filename)
        {
            using (var tempDirectory = new TempDirectory())
            {
                var resourceFilename = Path.Join("Resources", filename + ".csv");
                var inputBomString   = File.ReadAllText(resourceFilename);

                var bom = CsvSerializer.Deserialize(inputBomString);

                var bomXml = XmlBomSerializer.Serialize(bom);

                Snapshot.Match(bomXml, SnapshotNameExtension.Create(filename));
            }
        }
Example #29
0
        public void TestIncompleteLine()
        {
            var Input = StringToStreamReader("Prop1,Prop2" + Environment.NewLine
                                             + "A" + Environment.NewLine
                                             + "C,D");

            var Data = CsvSerializer.Deserialize <HasProperties>(Input);

            Assert.True(Data.Count() == 2);
            Assert.Equal("A", Data.First().Prop1);
            Assert.Null(Data.First().Prop2);
            Assert.Equal("C", Data.Last().Prop1);
            Assert.Equal("D", Data.Last().Prop2);
        }
Example #30
0
        public void TestDeserializeTyped()
        {
            var csvSerializer = new CsvSerializer();

            var content = CreateContent();

            var expected = CreateRecords();

            var actual = csvSerializer.Deserialize(content, typeof(List <Customer>));

            AssertUtilities.Equal(expected, actual);

            Assert.IsType <List <Customer> >(actual);
        }
        public void CanSerializeAndDeserializeInEN_INLocale()
        {
            IFormatProvider  provider = CultureInfo.GetCultureInfo("en-IN");
            DecimalAndDouble model    = new DecimalAndDouble {
                Decimal = 123_456.789m,
                Double  = 123_456.789
            };
            string csv = CsvSerializer.Serialize(new[] { model }, provider: provider);

            DecimalAndDouble[] deserialized = CsvSerializer.Deserialize <DecimalAndDouble>(csv, provider: provider);
            deserialized.Length.Should().Be(1);
            model = deserialized[0];
            model.Decimal.Should().Be(123_456.789m);
            model.Double.Should().Be(123_456.789);
        }
Example #32
0
        public void CsvSerializer_ParseDocument()
        {
            var sb = new StringBuilder();

            sb.AppendLine("a,b,c");
            sb.AppendLine("a,b,c");
            sb.AppendLine("a,b,c");

            var testSubject = new CsvSerializer <TestData.ImplicitlyOrdered>();
            var rows        = testSubject.Deserialize(sb.ToStream())
                              .ToArray();

            Assert.True(rows.All(x => x.Field1 == "a"));
            Assert.True(rows.All(x => x.Field2 == "b"));
            Assert.True(rows.All(x => x.Field3 == "c"));
            Assert.Equal(3, rows.Length);
        }