public void ToJson_DateAndTimeNoOffset_CorrectJson()
        {
            //arrange
            var dateTimeObject = new NullableDateTimeClass();

            dateTimeObject.Property = new DateTime(2016, 1, 2, 23, 59, 58, 555);

            //act
            var json = _convert.ToJson(dateTimeObject);

            //assert
            Assert.That(json.ToString(), Is.EqualTo("{\"Property\":\"2016-01-02T23:59:58.555\"}"));
        }
示例#2
0
        public void ToJson_Utc_CorrectJson()
        {
            //arrange
            var dateTimeObject = new NullableDateTimeClass();

            dateTimeObject.Property = new DateTime(2016, 1, 2, 23, 59, 58, 555, DateTimeKind.Utc);

            //act
            string json = _convert.ToJson(dateTimeObject);

            //assert
            Assert.That(json, Is.EqualTo("{\"Property\":\"2016-01-02T23:59:58.555Z\"}"));
        }
        public void ToJson_DateOnly_CorrectJson()
        {
            //arrange
            var dateTimeObject = new NullableDateTimeClass();

            dateTimeObject.Property = new DateTime(2017, 3, 7);

            //act
            var json = _convert.ToJson(dateTimeObject);

            //assert
            Assert.That(json.ToString(), Is.EqualTo("{\"Property\":\"2017-03-07T00:00:00\"}"));
        }
        public void DateTimeProperty_CorrectlyDeserialized(string value, DateTime?expectedDateTime, DateTimeKind expectedKind)
        {
            //arrange
            var jsonClass = new NullableDateTimeClass();

            //act
            _convert.FromJson(jsonClass, $"{{\"Property\":{value}}}");

            //assert
            Assert.That(jsonClass.Property, Is.EqualTo(expectedDateTime));
            if (expectedDateTime != null)
            {
                Assert.That(jsonClass.Property.Value.Kind, Is.EqualTo(expectedKind));
            }
        }
        public void ToJson_Local_CorrectJson()
        {
            //arrange
            var dateTimeObject = new NullableDateTimeClass();

            dateTimeObject.Property = new DateTime(2016, 1, 2, 23, 59, 58, 555, DateTimeKind.Local);

            //act
            var json = _convert.ToJson(dateTimeObject);

            //assert
            var offset  = TimeZoneInfo.Local.GetUtcOffset(DateTime.UtcNow);
            var sign    = offset.Duration().TotalMinutes > 0 ? "+" : "-";
            var hours   = Math.Abs(offset.Hours).ToString("00");
            var minutes = offset.Minutes.ToString("00");

            Assert.That(json.ToString(), Is.EqualTo($"{{\"Property\":\"2016-01-02T23:59:58.555{sign}{hours}:{minutes}\"}}"));
        }
示例#6
0
 protected abstract string ToJson(NullableDateTimeClass jsonClass);
示例#7
0
        protected override string ToJson(NullableDateTimeClass jsonClass)
        {
            var jsonUtf8 = _convert.ToJsonUtf8(jsonClass);

            return(Encoding.UTF8.GetString(jsonUtf8));
        }
示例#8
0
 protected override string ToJson(NullableDateTimeClass jsonClass)
 {
     return(_convert.ToJson(jsonClass).ToString());
 }