2 // Copyright (c) 2007 James Newton-King
4 // Permission is hereby granted, free of charge, to any person
5 // obtaining a copy of this software and associated documentation
6 // files (the "Software"), to deal in the Software without
7 // restriction, including without limitation the rights to use,
8 // copy, modify, merge, publish, distribute, sublicense, and/or sell
9 // copies of the Software, and to permit persons to whom the
10 // Software is furnished to do so, subject to the following
13 // The above copyright notice and this permission notice shall be
14 // included in all copies or substantial portions of the Software.
16 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
18 // OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20 // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21 // WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 // OTHER DEALINGS IN THE SOFTWARE.
27 using System.Collections.Generic;
30 using Newtonsoft.Json.Tests.TestObjects;
31 using NUnit.Framework;
32 using Newtonsoft.Json.Converters;
34 namespace Newtonsoft.Json.Tests.Converters
36 public class JavaScriptDateTimeConverterTests : TestFixtureBase
39 public void SerializeDateTime()
41 JavaScriptDateTimeConverter converter = new JavaScriptDateTimeConverter();
43 DateTime d = new DateTime(2000, 12, 15, 22, 11, 3, 55, DateTimeKind.Utc);
46 result = JsonConvert.SerializeObject(d, converter);
47 Assert.AreEqual("new Date(976918263055)", result);
50 #if !PocketPC && !NET20
52 public void SerializeDateTimeOffset()
54 JavaScriptDateTimeConverter converter = new JavaScriptDateTimeConverter();
56 DateTimeOffset now = new DateTimeOffset(2000, 12, 15, 22, 11, 3, 55, TimeSpan.Zero);
59 result = JsonConvert.SerializeObject(now, converter);
60 Assert.AreEqual("new Date(976918263055)", result);
66 int i = Convert.ToInt32("+1");
71 public void SerializeNullableDateTimeClass()
73 NullableDateTimeTestClass t = new NullableDateTimeTestClass()
76 DateTimeOffsetField = null
79 JavaScriptDateTimeConverter converter = new JavaScriptDateTimeConverter();
83 result = JsonConvert.SerializeObject(t, converter);
84 Assert.AreEqual(@"{""PreField"":null,""DateTimeField"":null,""DateTimeOffsetField"":null,""PostField"":null}", result);
86 t = new NullableDateTimeTestClass()
88 DateTimeField = new DateTime(2000, 12, 15, 22, 11, 3, 55, DateTimeKind.Utc),
89 DateTimeOffsetField = new DateTimeOffset(2000, 12, 15, 22, 11, 3, 55, TimeSpan.Zero)
92 result = JsonConvert.SerializeObject(t, converter);
93 Assert.AreEqual(@"{""PreField"":null,""DateTimeField"":new Date(976918263055),""DateTimeOffsetField"":new Date(976918263055),""PostField"":null}", result);
97 [ExpectedException(typeof(Exception), ExpectedMessage = "Cannot convert null value to System.DateTime.")]
98 public void DeserializeNullToNonNullable()
100 DateTimeTestClass c2 =
101 JsonConvert.DeserializeObject<DateTimeTestClass>(@"{""PreField"":""Pre"",""DateTimeField"":null,""DateTimeOffsetField"":null,""PostField"":""Post""}", new JavaScriptDateTimeConverter());
105 public void DeserializeDateTimeOffset()
107 JavaScriptDateTimeConverter converter = new JavaScriptDateTimeConverter();
108 DateTimeOffset start = new DateTimeOffset(2000, 12, 15, 22, 11, 3, 55, TimeSpan.Zero);
110 string json = JsonConvert.SerializeObject(start, converter);
112 DateTimeOffset result = JsonConvert.DeserializeObject<DateTimeOffset>(json, converter);
113 Assert.AreEqual(new DateTimeOffset(2000, 12, 15, 22, 11, 3, 55, TimeSpan.Zero), result);
118 public void DeserializeDateTime()
120 JavaScriptDateTimeConverter converter = new JavaScriptDateTimeConverter();
122 DateTime result = JsonConvert.DeserializeObject<DateTime>("new Date(976918263055)", converter);
123 Assert.AreEqual(new DateTime(2000, 12, 15, 22, 11, 3, 55, DateTimeKind.Utc), result);