Skip to content
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
16 changes: 15 additions & 1 deletion src/LitJson/JsonMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,19 @@ public IDictionary<string, PropertyMetadata> Properties {
public delegate IJsonWrapper WrapperFactory ();


[Flags]
public enum JsonMapperOptions
{
None = 0x00,
DateTimesAlwaysUniversal = 0x01,
}


public class JsonMapper
{
#region Fields
public static JsonMapperOptions Options;

private static int max_nesting_depth;

private static IFormatProvider datetime_format;
Expand Down Expand Up @@ -667,7 +677,11 @@ private static void RegisterBaseImporters ()
typeof (char), importer);

importer = delegate (object input) {
return Convert.ToDateTime ((string) input, datetime_format);
DateTime result = Convert.ToDateTime((string) input, datetime_format);
if ((JsonMapper.Options & JsonMapperOptions.DateTimesAlwaysUniversal) != 0) {
result = result.ToUniversalTime();
}
return result;
};
RegisterImporter (base_importers_table, typeof (string),
typeof (DateTime), importer);
Expand Down
27 changes: 27 additions & 0 deletions test/JsonMapperTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,11 @@ public enum NullableEnum
TestVal2 = 2
}

public class DateTimeTest
{
public DateTime dateTimeValue;
}

public class NullableEnumTest
{
public NullableEnum? TestEnum;
Expand Down Expand Up @@ -1024,5 +1029,27 @@ public void NullableEnumExportTest()
expectedJson = "{\"TestEnum\":null}";
Assert.AreEqual(expectedJson, JsonMapper.ToJson(value));
}

[Test]
public void DateTimeShouldBeUniversalTest()
{
string json = @"{
""dateTimeValue"": ""2014-05-02T05:52:10.569000+00:00""
}";

JsonMapper.Options = JsonMapperOptions.DateTimesAlwaysUniversal;

DateTimeTest dateTimeTest = JsonMapper.ToObject<DateTimeTest>(json);
Assert.AreEqual(DateTimeKind.Utc, dateTimeTest.dateTimeValue.Kind);

json = @"{
""dateTimeValue"": ""2014-05-02T05:52:10.569000""
}";

dateTimeTest = JsonMapper.ToObject<DateTimeTest>(json);
Assert.AreEqual(DateTimeKind.Utc, dateTimeTest.dateTimeValue.Kind);

JsonMapper.Options = JsonMapperOptions.None;
}
}
}