public void NullableSerializeUTC() { NullableDateTimeTestClass c = new NullableDateTimeTestClass(); c.DateTimeField = new DateTime(2008, 12, 12, 12, 12, 12, 0, DateTimeKind.Utc).ToLocalTime(); c.DateTimeOffsetField = new DateTime(2008, 12, 12, 12, 12, 12, 0, DateTimeKind.Utc).ToLocalTime(); c.PreField = "Pre"; c.PostField = "Post"; string json = JsonConvert.SerializeObject(c, new IsoDateTimeConverter() { DateTimeStyles = DateTimeStyles.AssumeUniversal }); Assert.AreEqual(@"{""PreField"":""Pre"",""DateTimeField"":""2008-12-12T12:12:12Z"",""DateTimeOffsetField"":""2008-12-12T12:12:12+00:00"",""PostField"":""Post""}", json); //test the other edge case too c.DateTimeField = null; c.DateTimeOffsetField = null; c.PreField = "Pre"; c.PostField = "Post"; json = JsonConvert.SerializeObject(c, new IsoDateTimeConverter() { DateTimeStyles = DateTimeStyles.AssumeUniversal }); Assert.AreEqual(@"{""PreField"":""Pre"",""DateTimeField"":null,""DateTimeOffsetField"":null,""PostField"":""Post""}", json); }
public void SerializeNullableDateTimeClass() { NullableDateTimeTestClass t = new NullableDateTimeTestClass { DateTimeField = null, DateTimeOffsetField = null }; UnixDateTimeConverter converter = new UnixDateTimeConverter(); string result = JsonConvert.SerializeObject(t, converter); Assert.AreEqual( @"{""PreField"":null,""DateTimeField"":null,""DateTimeOffsetField"":null,""PostField"":null}", result ); t = new NullableDateTimeTestClass { DateTimeField = new DateTime(2018, 1, 1, 21, 1, 16, DateTimeKind.Utc), DateTimeOffsetField = new DateTimeOffset(1970, 2, 1, 20, 6, 18, TimeSpan.Zero) }; result = JsonConvert.SerializeObject(t, converter); Assert.AreEqual( @"{""PreField"":null,""DateTimeField"":1514840476,""DateTimeOffsetField"":2750778,""PostField"":null}", result ); }
public void SerializeNullableDateTimeClass() { NullableDateTimeTestClass t = new NullableDateTimeTestClass() { DateTimeField = null, DateTimeOffsetField = null }; JavaScriptDateTimeConverter converter = new JavaScriptDateTimeConverter(); string result; result = JsonConvert.SerializeObject(t, converter); Assert.AreEqual( @"{""PreField"":null,""DateTimeField"":null,""DateTimeOffsetField"":null,""PostField"":null}", result ); t = new NullableDateTimeTestClass() { DateTimeField = new DateTime(2000, 12, 15, 22, 11, 3, 55, DateTimeKind.Utc), DateTimeOffsetField = new DateTimeOffset(2000, 12, 15, 22, 11, 3, 55, TimeSpan.Zero) }; result = JsonConvert.SerializeObject(t, converter); Assert.AreEqual( @"{""PreField"":null,""DateTimeField"":new Date(976918263055),""DateTimeOffsetField"":new Date(976918263055),""PostField"":null}", result ); }
public void NullableDeserializeEmptyString() { string json = @"{""DateTimeField"":""""}"; NullableDateTimeTestClass c = JsonConvert.DeserializeObject <NullableDateTimeTestClass>(json, new JsonSerializerSettings { Converters = new [] { new IsoDateTimeConverter() } }); Assert.AreEqual(null, c.DateTimeField); }
public void NullableDeserializeUTC() { NullableDateTimeTestClass c = JsonConvert.DeserializeObject <NullableDateTimeTestClass>(@"{""PreField"":""Pre"",""DateTimeField"":""2008-12-12T12:12:12Z"",""DateTimeOffsetField"":""2008-12-12T12:12:12Z"",""PostField"":""Post""}", new IsoDateTimeConverter() { DateTimeStyles = DateTimeStyles.AssumeUniversal }); Assert.AreEqual(new DateTime(2008, 12, 12, 12, 12, 12, 0, DateTimeKind.Utc).ToLocalTime(), c.DateTimeField); Assert.AreEqual(new DateTimeOffset(2008, 12, 12, 12, 12, 12, 0, TimeSpan.Zero), c.DateTimeOffsetField); Assert.AreEqual("Pre", c.PreField); Assert.AreEqual("Post", c.PostField); NullableDateTimeTestClass c2 = JsonConvert.DeserializeObject <NullableDateTimeTestClass>(@"{""PreField"":""Pre"",""DateTimeField"":null,""DateTimeOffsetField"":null,""PostField"":""Post""}", new IsoDateTimeConverter() { DateTimeStyles = DateTimeStyles.AssumeUniversal }); Assert.AreEqual(null, c2.DateTimeField); Assert.AreEqual(null, c2.DateTimeOffsetField); Assert.AreEqual("Pre", c2.PreField); Assert.AreEqual("Post", c2.PostField); }
public void NullableSerializeUTC() { NullableDateTimeTestClass c = new NullableDateTimeTestClass(); c.DateTimeField = new DateTime(2008, 12, 12, 12, 12, 12, 0, DateTimeKind.Utc).ToLocalTime(); c.DateTimeOffsetField = new DateTime(2008, 12, 12, 12, 12, 12, 0, DateTimeKind.Utc).ToLocalTime(); c.PreField = "Pre"; c.PostField = "Post"; string json = JavaScriptConvert.SerializeObject(c, new IsoDateTimeConverter() { DateTimeStyles = DateTimeStyles.AssumeUniversal }); Assert.AreEqual(@"{""PreField"":""Pre"",""DateTimeField"":""2008-12-12T12:12:12.0000000Z"",""DateTimeOffsetField"":""2008-12-12T12:12:12.0000000+00:00"",""PostField"":""Post""}", json); //test the other edge case too c.DateTimeField = null; c.DateTimeOffsetField = null; c.PreField = "Pre"; c.PostField = "Post"; json = JavaScriptConvert.SerializeObject(c, new IsoDateTimeConverter() { DateTimeStyles = DateTimeStyles.AssumeUniversal }); Assert.AreEqual(@"{""PreField"":""Pre"",""DateTimeField"":null,""DateTimeOffsetField"":null,""PostField"":""Post""}", json); }
public void SerializeNullableDateTimeClass() { NullableDateTimeTestClass t = new NullableDateTimeTestClass() { DateTimeField = null, DateTimeOffsetField = null }; JavaScriptDateTimeConverter converter = new JavaScriptDateTimeConverter(); string result; result = JavaScriptConvert.SerializeObject(t, converter); Assert.AreEqual(@"{""PreField"":null,""DateTimeField"":null,""DateTimeOffsetField"":null,""PostField"":null}", result); t = new NullableDateTimeTestClass() { DateTimeField = new DateTime(2000, 12, 15, 22, 11, 3, 55, DateTimeKind.Utc), DateTimeOffsetField = new DateTimeOffset(2000, 12, 15, 22, 11, 3, 55, TimeSpan.Zero) }; result = JavaScriptConvert.SerializeObject(t, converter); Assert.AreEqual(@"{""PreField"":null,""DateTimeField"":new Date(976918263055),""DateTimeOffsetField"":new Date(976918263055),""PostField"":null}", result); }