-
Notifications
You must be signed in to change notification settings - Fork 14
Serializing & Deserializing DateTime objects #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
cae76f6
b18721a
2aef06a
f75b7ba
0db1602
c36eeb0
0a606d0
993370e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -150,8 +150,8 @@ public void DateTimeWithoutMilliseconds_should_always_be_deserialized_correctly_ | |
| Assert.AreEqual(dateWithoutMillisecondsUnspecified, deserialized); | ||
| } | ||
|
|
||
| /// <summary>UTC date time is deserialized as kind UTC.</summary> | ||
| [Test, Ignore("Don't pre-serialize into Utc")] | ||
| /// <summary>UTC date time is deserialized as kind UTC.</summary> | ||
| [Test] | ||
| public void UtcDateTime_Is_Deserialized_As_Kind_Utc() | ||
| { | ||
| //Serializing UTC | ||
|
|
@@ -160,7 +160,7 @@ public void UtcDateTime_Is_Deserialized_As_Kind_Utc() | |
| var serialized = JsonSerializer.SerializeToString(utcNow); | ||
| //Deserializing UTC? | ||
| var deserialized = JsonSerializer.DeserializeFromString<DateTime>(serialized); | ||
| Assert.That(deserialized.Kind, Is.EqualTo(DateTimeKind.Utc)); //fails -> is DateTimeKind.Local | ||
| Assert.That(deserialized.Kind, Is.EqualTo(DateTimeKind.Utc)); | ||
| } | ||
|
|
||
| /// <summary> | ||
|
|
@@ -371,6 +371,86 @@ public void DateTime_Is_Serialized_As_Utc_and_Deserialized_as_local() | |
| { | ||
| Assert.AreEqual(DateTimeKind.Utc, TypeSerializer.DeserializeFromString<TestObject>(TypeSerializer.SerializeToString<TestObject>(testObject)).Date.Kind); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// DateTime with DateTimeKind.Unspecified should be treated as UTC (rather than Local) when AssumeUtc is true | ||
| /// </summary> | ||
| [Test] | ||
| public void DateTime_Unspecified_Is_Serialized_And_Deserialized_As_Utc_When_AssumeUtc_Flag_Is_True() | ||
| { | ||
| var dateTimeUnspecified = new DateTime(2013, 1, 1, 0, 0, 1, DateTimeKind.Unspecified); | ||
|
|
||
| // Using JsonSerializer becayse TypeSerializer doesn't call DateTimeSerializer.WriteWcfJsonDate | ||
| var deserialized = JsonSerializer.DeserializeFromString<DateTime>(JsonSerializer.SerializeToString<DateTime>(dateTimeUnspecified)); | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Had to use JsonSerializer here because it seems TypeSerializer is only calling the DateTimeSerializer methods on deserialize... not on serialize. JsonSerializer used it for both. |
||
| Assert.AreEqual(DateTimeKind.Local, deserialized.Date.Kind); | ||
| Assert.AreEqual(dateTimeUnspecified.Hour, deserialized.Hour); | ||
|
|
||
|
|
||
| // Change the behavior with config | ||
| using (JsConfig.With(assumeUtc: true)) | ||
| { | ||
| // Using JsonSerializer becayse TypeSerializer doesn't call DateTimeSerializer.WriteWcfJsonDate | ||
| var serializedUtc = JsonSerializer.SerializeToString<DateTime>(dateTimeUnspecified); | ||
| var deserializedUtc = JsonSerializer.DeserializeFromString<DateTime>(serializedUtc); | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as note above at line 385. |
||
| Assert.AreEqual(DateTimeKind.Utc, deserializedUtc.Date.Kind); | ||
| Assert.AreEqual(dateTimeUnspecified.Hour, deserializedUtc.Hour); | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Dates strings with no time component should deserialize to Midnight (00:00:00) Local by default, or Midnight Utc when AssumeUtc is set to true | ||
| /// </summary> | ||
| [Test] | ||
| public void DateTime_Should_Deserialize_Strings_With_No_Time_As_Midnight() | ||
| { | ||
| const string dateTimeStr = "2015-03-23"; | ||
|
|
||
| var deserialized = (TypeSerializer.DeserializeFromString<DateTime>(dateTimeStr)); | ||
| Assert.AreEqual(DateTimeKind.Local, deserialized.Date.Kind); | ||
| Assert.AreEqual(0, deserialized.Hour); | ||
|
|
||
| // Change the behavior with config | ||
| using (JsConfig.With(assumeUtc: true)) | ||
| { | ||
| var deserializedUtc = (TypeSerializer.DeserializeFromString<DateTime>(dateTimeStr)); | ||
| Assert.AreEqual(DateTimeKind.Utc, deserializedUtc.Date.Kind); | ||
| Assert.AreEqual(0, deserializedUtc.Hour); | ||
| } | ||
| } | ||
|
|
||
| [Test] | ||
| public void DateTime_Should_Deserialize_Correctly_If_It_Doesnt_Recognize_The_Offset_and_falls_back_to_DateTimeParse() | ||
| { | ||
| const string dateTimeStr = "2015-03-31T16:02:42-04:00"; | ||
| var currentTimeZone = TimeZone.CurrentTimeZone; | ||
|
|
||
| // TEST 1: No JsConfig options set should deserialize as local | ||
| var deserialized = (TypeSerializer.DeserializeFromString<DateTime>(dateTimeStr)); | ||
| Assert.AreEqual(DateTimeKind.Local, deserialized.Date.Kind); | ||
|
|
||
| var deserializedJson = (JsonSerializer.DeserializeFromString<DateTime>(dateTimeStr)); | ||
| Assert.AreEqual(DateTimeKind.Local, deserializedJson.Date.Kind); | ||
|
|
||
| // TEST 2: AssumeUtc shouldn't change anything because the time zone is specified | ||
| using (JsConfig.With(assumeUtc: true)) | ||
| { | ||
| deserialized = (TypeSerializer.DeserializeFromString<DateTime>(dateTimeStr)); | ||
| Assert.AreEqual(DateTimeKind.Local, deserialized.Date.Kind); | ||
|
|
||
| deserializedJson = (JsonSerializer.DeserializeFromString<DateTime>(dateTimeStr)); | ||
| Assert.AreEqual(DateTimeKind.Local, deserializedJson.Date.Kind); | ||
| } | ||
|
|
||
| // TEST 3: AlwaysUseUtc should convert the time to UTC | ||
| using (JsConfig.With(alwaysUseUtc: true)) | ||
| { | ||
| deserialized = (TypeSerializer.DeserializeFromString<DateTime>(dateTimeStr)); | ||
| Assert.AreEqual(DateTimeKind.Utc, deserialized.Date.Kind); | ||
|
|
||
| deserializedJson = (JsonSerializer.DeserializeFromString<DateTime>(dateTimeStr)); | ||
| Assert.AreEqual(DateTimeKind.Utc, deserializedJson.Date.Kind); | ||
| } | ||
| } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The comment said this test was failing but I checked it again and it passed fine, so I removed the Ignore tag.