Example #1
0
        public void EmptyColumnsAreDeserializedIntoNull()
        {
            typeof(ModelWithNullableValues).IsPublic.Should().BeFalse();

            string csv = ",,,,,,,,,,,,,";

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

            item.Bool.Should().BeNull();
            item.Byte.Should().BeNull();
            item.SByte.Should().BeNull();
            item.Short.Should().BeNull();
            item.UShort.Should().BeNull();
            item.Int.Should().BeNull();
            item.UInt.Should().BeNull();
            item.Long.Should().BeNull();
            item.ULong.Should().BeNull();
            item.Float.Should().BeNull();
            item.Double.Should().BeNull();
            item.Decimal.Should().BeNull();
            item.String.Should().BeEmpty();
            item.DateTime.Should().BeNull();

            csv   = "\"Bool\",\"Byte\",\"SByte\",\"Short\",\"UShort\",\"Int\",\"UInt\",\"Long\",\"ULong\",\"Float\",\"Double\",\"Decimal\",\"String\",\"DateTime\"\r\nTrue,102,-100,-200,200,-3000,3000,-40000,40000,1E+14,1.7837193718273812E+19,989898989898,\"CSV Serializer\",\"08/23/2019 00:00:00\"";
            items = CsvSerializer.Deserialize <ModelWithNullableValues>(csv, hasHeaders: true);
            items.Length.Should().Be(1);
            item = items.Single();
            item.Bool.Should().BeTrue();
            item.Byte.Should().Be(0x66);
            item.SByte.Should().Be(-100);
            item.Short.Should().Be(-200);
            item.UShort.Should().Be(200);
            item.Int.Should().Be(-3000);
            item.UInt.Should().Be(3000);
            item.Long.Should().Be(-40000L);
            item.ULong.Should().Be(40000UL);
            item.Float.Should().Be(100000000000000.0f);
            item.Double.Should().Be(17837193718273812973.0);
            item.Decimal.Should().Be(989898989898m);
            item.String.Should().Be("CSV Serializer");
            item.DateTime.Should().Be(new DateTime(2019, 8, 23));
        }
Example #2
0
        public void NullValuesAreSerializedIntoEmptyColumn()
        {
            typeof(ModelWithNullableValues).IsPublic.Should().BeFalse();

            ModelWithNullableValues obj = new ModelWithNullableValues {
                Bool     = null,
                Byte     = null,
                SByte    = null,
                Short    = null,
                UShort   = null,
                Int      = null,
                UInt     = null,
                Long     = null,
                ULong    = null,
                Float    = null,
                Double   = null,
                Decimal  = null,
                String   = null,
                DateTime = null
            };
            string csv = CsvSerializer.Serialize(new[] { obj }, withHeaders: true);

            csv.Should().Be("\"Bool\",\"Byte\",\"SByte\",\"Short\",\"UShort\",\"Int\",\"UInt\",\"Long\",\"ULong\",\"Float\",\"Double\",\"Decimal\",\"String\",\"DateTime\"\r\n,,,,,,,,,,,,,");

            obj = new ModelWithNullableValues {
                Bool     = true,
                Byte     = 0x66,
                SByte    = -100,
                Short    = -200,
                UShort   = 200,
                Int      = -3000,
                UInt     = 3000,
                Long     = -40000L,
                ULong    = 40000UL,
                Float    = 100000000000000.0f,
                Double   = 17837193718273812973.0,
                Decimal  = 989898989898m,
                String   = "CSV Serializer",
                DateTime = new DateTime(2019, 8, 23)
            };
            csv = CsvSerializer.Serialize(new[] { obj }, withHeaders: true);
            csv.Should().Be("\"Bool\",\"Byte\",\"SByte\",\"Short\",\"UShort\",\"Int\",\"UInt\",\"Long\",\"ULong\",\"Float\",\"Double\",\"Decimal\",\"String\",\"DateTime\"\r\nTrue,102,-100,-200,200,-3000,3000,-40000,40000,1E+14,1.7837193718273812E+19,989898989898,\"CSV Serializer\",\"8/23/2019 12:00:00 AM\"");
        }