示例#1
0
        public async Task TestJsonValues()
        {
            var csvData =
                "A;B" + Environment.NewLine +
                "42;\"{\"\"IntValue\"\":42,\"\"StrValue\"\":\"\"Forty-Two\"\"}\"" + Environment.NewLine +
                "5;\"{\"\"IntValue\"\":5,\"\"StrValue\"\":\"\"Five\"\"}\"";

            using var csvReader = new CsvReader(csvData);

            var csvTableReader = new CsvTableReader(csvReader);

            Assert.NotNull(csvTableReader);

            csvTableReader.AddConverter <CustomType>(new CustomTypeConverter());

            await csvTableReader.ReadHeadersAsync();

            Assert.Equal(2, csvTableReader.Headers.Count);
            Assert.Equal("A", csvTableReader.Headers[0]);
            Assert.Equal("B", csvTableReader.Headers[1]);

            await csvTableReader.ReadAsync();

            Assert.Equal(42, csvTableReader.GetValue <int>("A"));
            var o1 = csvTableReader.GetValue <CustomType>("B");

            Assert.Equal(42, o1.IntValue);
            Assert.Equal("Forty-Two", o1.StrValue);

            await csvTableReader.ReadAsync();

            Assert.Equal(5, csvTableReader.GetValue <int>("A"));
            var o2 = csvTableReader.GetValue <CustomType>("B");

            Assert.Equal(5, o2.IntValue);
            Assert.Equal("Five", o2.StrValue);
        }