Exemplo n.º 1
0
        public void SerializeUTC()
        {
            DateTimeTestClass c = new DateTimeTestClass();

            c.DateTimeField       = new DateTime(2008, 12, 12, 12, 12, 12, 0, DateTimeKind.Utc).ToLocalTime();
            c.DateTimeOffsetField = new DateTime(2008, 12, 12, 12, 12, 12, 0, DateTimeKind.Utc).ToLocalTime();
            c.PreField            = "Pre";
            c.PostField           = "Post";
            string json = JsonSerializer.Serialize(c);

            NullableDateTimeTestClass newOne = JsonSerializer.Deserialize <NullableDateTimeTestClass>(json);

            Assert.Equal(newOne.DateTimeField, c.DateTimeField);
            Assert.Equal(newOne.DateTimeOffsetField, c.DateTimeOffsetField);
            Assert.Equal(newOne.PostField, c.PostField);
            Assert.Equal(newOne.PreField, c.PreField);

            //test the other edge case too (start of a year)
            c.DateTimeField       = new DateTime(2008, 1, 1, 1, 1, 1, 0, DateTimeKind.Utc).ToLocalTime();
            c.DateTimeOffsetField = new DateTime(2008, 1, 1, 1, 1, 1, 0, DateTimeKind.Utc).ToLocalTime();
            c.PreField            = "Pre";
            c.PostField           = "Post";
            json = JsonSerializer.Serialize(c);

            newOne = JsonSerializer.Deserialize <NullableDateTimeTestClass>(json);
            Assert.Equal(newOne.DateTimeField, c.DateTimeField);
            Assert.Equal(newOne.DateTimeOffsetField, c.DateTimeOffsetField);
            Assert.Equal(newOne.PostField, c.PostField);
            Assert.Equal(newOne.PreField, c.PreField);
        }
Exemplo n.º 2
0
        public void NullableSerializeUTC()
        {
            NullableDateTimeTestClass c = new NullableDateTimeTestClass();

            c.DateTimeField       = new DateTime(2008, 12, 12, 12, 12, 12, 0, DateTimeKind.Utc).ToLocalTime();
            c.DateTimeOffsetField = new DateTime(2008, 12, 12, 12, 12, 12, 0, DateTimeKind.Utc).ToLocalTime();
            c.PreField            = "Pre";
            c.PostField           = "Post";
            string json = JsonSerializer.Serialize(c);

            NullableDateTimeTestClass newOne = JsonSerializer.Deserialize <NullableDateTimeTestClass>(json);

            Assert.Equal(newOne.DateTimeField, c.DateTimeField);
            Assert.Equal(newOne.DateTimeOffsetField, c.DateTimeOffsetField);
            Assert.Equal(newOne.PostField, c.PostField);
            Assert.Equal(newOne.PreField, c.PreField);

            c.DateTimeField       = null;
            c.DateTimeOffsetField = null;
            c.PreField            = "Pre";
            c.PostField           = "Post";
            json = JsonSerializer.Serialize(c);
            Assert.Equal(@"{""PreField"":""Pre"",""DateTimeField"":null,""DateTimeOffsetField"":null,""PostField"":""Post""}", json);
        }