diff --git a/src/JSONParser.cs b/src/JSONParser.cs index 9db9821..dbc40b9 100644 --- a/src/JSONParser.cs +++ b/src/JSONParser.cs @@ -243,6 +243,12 @@ internal static object ParseValue(Type type, string json) } return dictionary; } + if (type == typeof(DateTime)) + { + DateTime result; + DateTime.TryParse(json, System.Globalization.DateTimeFormatInfo.InvariantInfo, System.Globalization.DateTimeStyles.AssumeUniversal, out result); + return result; + } if (type == typeof(object)) { return ParseAnonymousValue(json); diff --git a/src/JSONWriter.cs b/src/JSONWriter.cs index bf57a0f..74d614a 100644 --- a/src/JSONWriter.cs +++ b/src/JSONWriter.cs @@ -1,6 +1,7 @@ using System; using System.Collections; using System.Collections.Generic; +using System.Globalization; using System.Reflection; using System.Runtime.Serialization; using System.Text; @@ -71,7 +72,7 @@ static void AppendValue(StringBuilder stringBuilder, object item) { stringBuilder.Append(((double)item).ToString(System.Globalization.CultureInfo.InvariantCulture)); } - else if (type == typeof(decimal)) + else if (type == typeof(decimal)) { stringBuilder.Append(((decimal)item).ToString(System.Globalization.CultureInfo.InvariantCulture)); } @@ -127,6 +128,10 @@ static void AppendValue(StringBuilder stringBuilder, object item) } stringBuilder.Append('}'); } + else if (item is DateTime time) + { + stringBuilder.Append(time.ToString(CultureInfo.InvariantCulture)); + } else { stringBuilder.Append('{'); diff --git a/test/TestParser.cs b/test/TestParser.cs index 14b7319..01e875f 100644 --- a/test/TestParser.cs +++ b/test/TestParser.cs @@ -38,6 +38,7 @@ static void Test(T expected, string json) [TestMethod] public void TestValues() { + Test(new DateTime(), "01/01/0001 00:00:00"); Test(12345, "12345"); Test(12345L, "12345"); Test(12345UL, "12345"); @@ -296,15 +297,15 @@ public void TestEscaping() } [TestMethod] - public void TestMultithread() + public void TestMultithread() { // Lots of threads - for (int i = 0; i < 100; i++) + for (int i = 0; i < 100; i++) { - new Thread(() => + new Thread(() => { // Each threads has enough work to potentially hit a race condition - for (int j = 0; j < 10000; j++) + for (int j = 0; j < 10000; j++) { TestValues(); TestArrayOfValues(); diff --git a/test/TestWriter.cs b/test/TestWriter.cs index d77beab..78082e0 100644 --- a/test/TestWriter.cs +++ b/test/TestWriter.cs @@ -30,6 +30,7 @@ public enum Style [TestMethod] public void TestValues() { + Assert.AreEqual(new DateTime().ToJson(), "01/01/0001 00:00:00"); Assert.AreEqual("\"\u94b1\u4e0d\u591f!\"", "\u94b1\u4e0d\u591f!".ToJson()); Assert.AreEqual("123", 123.ToJson()); Assert.AreEqual("true", true.ToJson());