public void DateSerializationWithNulls()
        {
            var serializeSettings = new JsonSerializerSettings();
            serializeSettings.ReferenceLoopHandling = ReferenceLoopHandling.Serialize;
            serializeSettings.Formatting = Formatting.Indented;
            serializeSettings.DateFormatHandling = DateFormatHandling.IsoDateFormat;
            serializeSettings.DateTimeZoneHandling = DateTimeZoneHandling.Utc;
            serializeSettings.NullValueHandling = NullValueHandling.Ignore;

            DateTestObject test = new DateTestObject();

            var expectedJson = @"{
              ""d"": ""0001-01-01"",
              ""dt"": ""0001-01-01T00:00:00Z"",
              ""dtoc"": ""0001-01-01"",
              ""dto"": ""0001-01-01T00:00:00+00:00""
            }";
            var json = JsonConvert.SerializeObject(test, serializeSettings);

            DateTestObject testRoundtrip = JsonConvert.DeserializeObject<DateTestObject>(json, serializeSettings);

            Assert.Equal(expectedJson, json);
            Assert.Null(testRoundtrip.DateNullable);
            Assert.Null(testRoundtrip.DateTimeNullable);
        }
        public void DateSerializationWithMaxValue()
        {
            var localDateTime = DateTime.Parse("9999-12-31T22:59:59-01:00", CultureInfo.InvariantCulture, DateTimeStyles.AdjustToUniversal).ToLocalTime();
            var serializeSettings = new JsonSerializerSettings();
            serializeSettings.ReferenceLoopHandling = ReferenceLoopHandling.Serialize;
            serializeSettings.Formatting = Formatting.Indented;
            serializeSettings.DateFormatHandling = DateFormatHandling.IsoDateFormat;
            serializeSettings.DateTimeZoneHandling = DateTimeZoneHandling.Utc;

            DateTestObject test = new DateTestObject();
            test.Date = localDateTime;
            test.DateNullable = localDateTime;
            test.DateTime = localDateTime;
            test.DateTimeNullable = localDateTime;
            test.DateTimeOffsetNullable = localDateTime;
            test.DateTimeOffsetNullableWithConverter = localDateTime;
            test.DateTimeOffsetWithConverter = localDateTime;

            var expectedJson = @"{
              ""d"": ""9999-12-31"",
              ""dt"": ""9999-12-31T23:59:59Z"",
              ""dn"": ""9999-12-31T23:59:59Z"",
              ""dtn"": ""9999-12-31"",
              ""dtoc"": ""9999-12-31"",
              ""dtonc"": ""9999-12-31"",
              ""dto"": ""0001-01-01T00:00:00+00:00"",
              ""dton"": """ + localDateTime.ToString("yyyy-MM-ddTHH:mm:sszzz") + @"""
            }";
            var json = JsonConvert.SerializeObject(test, serializeSettings);

            DateTestObject testRoundtrip = JsonConvert.DeserializeObject<DateTestObject>(json, serializeSettings);
            Assert.Equal(localDateTime, testRoundtrip.DateTime.ToLocalTime());
            Assert.Equal(expectedJson, json);
        }
        public void DateSerializationWithoutNulls()
        {
            var localDateTimeOffset = new DateTimeOffset(2015, 6, 1, 16, 10, 08, 121, new TimeSpan(-7, 0, 0));
            var utcDate = DateTime.Parse("2015-06-01T00:00:00.0", CultureInfo.InvariantCulture);
            var serializeSettings = new JsonSerializerSettings();
            serializeSettings.ReferenceLoopHandling = ReferenceLoopHandling.Serialize;
            serializeSettings.Formatting = Formatting.Indented;
            serializeSettings.DateFormatHandling = DateFormatHandling.IsoDateFormat;
            serializeSettings.DateTimeZoneHandling = DateTimeZoneHandling.Utc;

            DateTestObject test = new DateTestObject();
            test.Date = localDateTimeOffset.LocalDateTime;
            test.DateNullable = localDateTimeOffset.LocalDateTime;
            test.DateTime = localDateTimeOffset.LocalDateTime;
            test.DateTimeNullable = localDateTimeOffset.LocalDateTime;
            test.DateTimeOffset = localDateTimeOffset;
            test.DateTimeOffsetNullable = localDateTimeOffset;
            test.DateTimeOffsetWithConverter = localDateTimeOffset;
            test.DateTimeOffsetNullableWithConverter = localDateTimeOffset;

            var expectedJson = @"{
              ""d"": ""2015-06-01"",
              ""dt"": ""2015-06-01T23:10:08.121Z"",
              ""dn"": ""2015-06-01T23:10:08.121Z"",
              ""dtn"": ""2015-06-01"",
              ""dtoc"": ""2015-06-01"",
              ""dtonc"": ""2015-06-01"",
              ""dto"": ""2015-06-01T16:10:08.121-07:00"",
              ""dton"": ""2015-06-01T16:10:08.121-07:00""
            }";
            var json = JsonConvert.SerializeObject(test, serializeSettings);

            DateTestObject testRoundtrip = JsonConvert.DeserializeObject<DateTestObject>(json, serializeSettings);
            Assert.Equal(expectedJson, json);
            Assert.Equal(utcDate, testRoundtrip.Date);
            Assert.Equal(localDateTimeOffset, testRoundtrip.DateTime.ToLocalTime());
            Assert.Equal(test.DateTimeOffset, testRoundtrip.DateTimeOffset);
        }