Skip to content

adds support for datetime (universal format only #43

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/JSONParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
7 changes: 6 additions & 1 deletion src/JSONWriter.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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));
}
Expand Down Expand Up @@ -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('{');
Expand Down
9 changes: 5 additions & 4 deletions test/TestParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ static void Test<T>(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");
Expand Down Expand Up @@ -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();
Expand Down
1 change: 1 addition & 0 deletions test/TestWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down