-Fixed test when tests run in UTC timezone
This commit is contained in:
@@ -262,14 +262,21 @@ namespace Newtonsoft.Json.Tests.Converters
|
|||||||
[Test]
|
[Test]
|
||||||
public void SerializeShouldChangeNonUTCDates()
|
public void SerializeShouldChangeNonUTCDates()
|
||||||
{
|
{
|
||||||
|
DateTime localDateTime = new DateTime(2008, 1, 1, 1, 1, 1, 0, DateTimeKind.Local);
|
||||||
|
|
||||||
DateTimeTestClass c = new DateTimeTestClass();
|
DateTimeTestClass c = new DateTimeTestClass();
|
||||||
c.DateTimeField = new DateTime(2008, 1, 1, 1, 1, 1, 0, DateTimeKind.Local);
|
c.DateTimeField = localDateTime;
|
||||||
c.PreField = "Pre";
|
c.PreField = "Pre";
|
||||||
c.PostField = "Post";
|
c.PostField = "Post";
|
||||||
string json = JsonConvert.SerializeObject(c, new IsoDateTimeConverter() { DateTimeStyles = DateTimeStyles.AssumeUniversal }); //note that this fails without the Utc converter...
|
string json = JsonConvert.SerializeObject(c, new IsoDateTimeConverter() { DateTimeStyles = DateTimeStyles.AssumeUniversal }); //note that this fails without the Utc converter...
|
||||||
c.DateTimeField = new DateTime(2008, 1, 1, 1, 1, 1, 0, DateTimeKind.Utc);
|
c.DateTimeField = new DateTime(2008, 1, 1, 1, 1, 1, 0, DateTimeKind.Utc);
|
||||||
string json2 = JsonConvert.SerializeObject(c, new IsoDateTimeConverter() { DateTimeStyles = DateTimeStyles.AssumeUniversal });
|
string json2 = JsonConvert.SerializeObject(c, new IsoDateTimeConverter() { DateTimeStyles = DateTimeStyles.AssumeUniversal });
|
||||||
Assert.AreNotEqual(json, json2);
|
|
||||||
|
// if the current timezone is utc then local already equals utc
|
||||||
|
if (TimeZone.CurrentTimeZone.GetUtcOffset(localDateTime) == TimeSpan.Zero)
|
||||||
|
Assert.AreEqual(json, json2);
|
||||||
|
else
|
||||||
|
Assert.AreNotEqual(json, json2);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|||||||
@@ -579,12 +579,18 @@ namespace Newtonsoft.Json.Tests.Serialization
|
|||||||
Dictionary<string, object> collection = new Dictionary<string, object>()
|
Dictionary<string, object> collection = new Dictionary<string, object>()
|
||||||
{
|
{
|
||||||
{"First", new UrlStatus{ Status = 404, Url = @"http://www.bing.com"}},
|
{"First", new UrlStatus{ Status = 404, Url = @"http://www.bing.com"}},
|
||||||
{"Second", new UrlStatus{Status = 400, Url = @"http://www.google.com"}}
|
{"Second", new UrlStatus{Status = 400, Url = @"http://www.google.com"}},
|
||||||
|
{"List", new List<UrlStatus>
|
||||||
|
{
|
||||||
|
new UrlStatus {Status = 300, Url = @"http://www.yahoo.com"},
|
||||||
|
new UrlStatus {Status = 200, Url = @"http://www.askjeeves.com"}
|
||||||
|
}
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
string json = JsonConvert.SerializeObject(collection, Formatting.Indented, new JsonSerializerSettings
|
string json = JsonConvert.SerializeObject(collection, Formatting.Indented, new JsonSerializerSettings
|
||||||
{
|
{
|
||||||
TypeNameHandling = TypeNameHandling.Objects,
|
TypeNameHandling = TypeNameHandling.All,
|
||||||
TypeNameAssemblyFormat = FormatterAssemblyStyle.Simple
|
TypeNameAssemblyFormat = FormatterAssemblyStyle.Simple
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -599,20 +605,38 @@ namespace Newtonsoft.Json.Tests.Serialization
|
|||||||
""$type"": ""Newtonsoft.Json.Tests.Serialization.TypeNameHandlingTests+UrlStatus, Newtonsoft.Json.Tests"",
|
""$type"": ""Newtonsoft.Json.Tests.Serialization.TypeNameHandlingTests+UrlStatus, Newtonsoft.Json.Tests"",
|
||||||
""Status"": 400,
|
""Status"": 400,
|
||||||
""Url"": ""http://www.google.com""
|
""Url"": ""http://www.google.com""
|
||||||
|
},
|
||||||
|
""List"": {
|
||||||
|
""$type"": ""System.Collections.Generic.List`1[[Newtonsoft.Json.Tests.Serialization.TypeNameHandlingTests+UrlStatus, Newtonsoft.Json.Tests]], mscorlib"",
|
||||||
|
""$values"": [
|
||||||
|
{
|
||||||
|
""$type"": ""Newtonsoft.Json.Tests.Serialization.TypeNameHandlingTests+UrlStatus, Newtonsoft.Json.Tests"",
|
||||||
|
""Status"": 300,
|
||||||
|
""Url"": ""http://www.yahoo.com""
|
||||||
|
},
|
||||||
|
{
|
||||||
|
""$type"": ""Newtonsoft.Json.Tests.Serialization.TypeNameHandlingTests+UrlStatus, Newtonsoft.Json.Tests"",
|
||||||
|
""Status"": 200,
|
||||||
|
""Url"": ""http://www.askjeeves.com""
|
||||||
|
}
|
||||||
|
]
|
||||||
}
|
}
|
||||||
}", json);
|
}", json);
|
||||||
|
|
||||||
object c = JsonConvert.DeserializeObject(json, new JsonSerializerSettings
|
object c = JsonConvert.DeserializeObject(json, new JsonSerializerSettings
|
||||||
{
|
{
|
||||||
TypeNameHandling = TypeNameHandling.Objects,
|
TypeNameHandling = TypeNameHandling.All,
|
||||||
TypeNameAssemblyFormat = FormatterAssemblyStyle.Simple
|
TypeNameAssemblyFormat = FormatterAssemblyStyle.Simple
|
||||||
});
|
});
|
||||||
|
|
||||||
Assert.IsInstanceOfType(typeof(Dictionary<string, object>), c);
|
Assert.IsInstanceOfType(typeof(Dictionary<string, object>), c);
|
||||||
|
|
||||||
Dictionary<string, object> newCollection = (Dictionary<string, object>)c;
|
Dictionary<string, object> newCollection = (Dictionary<string, object>)c;
|
||||||
Assert.AreEqual(2, newCollection.Count);
|
Assert.AreEqual(3, newCollection.Count);
|
||||||
Assert.AreEqual(@"http://www.bing.com", ((UrlStatus)newCollection["First"]).Url);
|
Assert.AreEqual(@"http://www.bing.com", ((UrlStatus)newCollection["First"]).Url);
|
||||||
|
|
||||||
|
List<UrlStatus> statues = (List<UrlStatus>) newCollection["List"];
|
||||||
|
Assert.AreEqual(2, statues.Count);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user