public void DateTimeProperty_CorrectlyDeserialized(string value, DateTime expectedDateTime, DateTimeKind expectedKind) { //arrange var jsonClass = new DateTimeClass(); //act _convert.FromJson(jsonClass, $"{{\"Property\":{value}}}"); //assert Assert.That(jsonClass.Property, Is.EqualTo(expectedDateTime)); Assert.That(jsonClass.Property.Kind, Is.EqualTo(expectedKind)); }
public void ToJson_DateAndTimeNoOffset_CorrectJson() { //arrange var dateTimeObject = new DateTimeClass(); 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\"}")); }
public void ToJson_DateOnly_CorrectJson() { //arrange var dateTimeObject = new DateTimeClass(); 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 ToJson_Utc_CorrectJson() { //arrange var dateTimeObject = new DateTimeClass(); 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_Local_CorrectJson() { //arrange var dateTimeObject = new DateTimeClass(); 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}\"}}")); }
protected abstract string ToJson(DateTimeClass jsonClass);
protected override string ToJson(DateTimeClass jsonClass) { var jsonUtf8 = _convert.ToJsonUtf8(jsonClass); return(Encoding.UTF8.GetString(jsonUtf8)); }
protected override string ToJson(DateTimeClass jsonClass) { return(_convert.ToJson(jsonClass).ToString()); }