Skip to content

Commit 4828d2e

Browse files
author
Ken Hung
committed
Fix DateTimeOffset support.
1 parent b893926 commit 4828d2e

2 files changed

Lines changed: 34 additions & 19 deletions

File tree

src/LitJson/JsonMapper.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -603,6 +603,11 @@ private static void RegisterBaseExporters ()
603603
delegate (object obj, JsonWriter writer) {
604604
writer.Write ((ulong) obj);
605605
};
606+
607+
base_exporters_table[typeof(DateTimeOffset)] =
608+
delegate (object obj, JsonWriter writer) {
609+
writer.Write(((DateTimeOffset)obj).ToString("yyyy-MM-ddTHH:mm:ss.fffffffzzz", datetime_format));
610+
};
606611
}
607612

608613
private static void RegisterBaseImporters ()

test/JsonMapperTest.cs

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -162,15 +162,16 @@ public class UiWindow
162162

163163
public class ValueTypesTest
164164
{
165-
public byte TestByte;
166-
public char TestChar;
167-
public DateTime TestDateTime;
168-
public decimal TestDecimal;
169-
public sbyte TestSByte;
170-
public short TestShort;
171-
public ushort TestUShort;
172-
public uint TestUInt;
173-
public ulong TestULong;
165+
public byte TestByte;
166+
public char TestChar;
167+
public DateTime TestDateTime;
168+
public decimal TestDecimal;
169+
public sbyte TestSByte;
170+
public short TestShort;
171+
public ushort TestUShort;
172+
public uint TestUInt;
173+
public ulong TestULong;
174+
public DateTimeOffset TestDateTimeOffset;
174175
}
175176

176177
public class NullableTypesTest
@@ -399,13 +400,17 @@ public void ExportValueTypesTest ()
399400
test.TestUShort = 30000;
400401
test.TestUInt = 90000000;
401402
test.TestULong = 0xFFFFFFFFFFFFFFFF; // = =18446744073709551615
403+
test.TestDateTimeOffset =
404+
new DateTimeOffset(2019, 9, 18, 16, 47,
405+
50, 123, TimeSpan.FromHours(8)).AddTicks(4567);
402406

403407
string json = JsonMapper.ToJson (test);
404408
string expected =
405409
"{\"TestByte\":200,\"TestChar\":\"P\",\"TestDateTime\":" +
406410
"\"12/22/2012 00:00:00\",\"TestDecimal\":10.333," +
407411
"\"TestSByte\":-5,\"TestShort\":1024,\"TestUShort\":30000" +
408-
",\"TestUInt\":90000000,\"TestULong\":18446744073709551615}";
412+
",\"TestUInt\":90000000,\"TestULong\":18446744073709551615" +
413+
",\"TestDateTimeOffset\":\"2019-09-18T16:47:50.1234567+08:00\"}";
409414

410415
Assert.AreEqual (expected, json);
411416
}
@@ -825,15 +830,16 @@ public void ImportValueTypesTest ()
825830
{
826831
string json = @"
827832
{
828-
""TestByte"": 200,
829-
""TestChar"": 'P',
830-
""TestDateTime"": ""12/22/2012 00:00:00"",
831-
""TestDecimal"": 10.333,
832-
""TestSByte"": -5,
833-
""TestShort"": 1024,
834-
""TestUShort"": 30000,
835-
""TestUInt"": 90000000,
836-
""TestULong"": 18446744073709551615
833+
""TestByte"": 200,
834+
""TestChar"": 'P',
835+
""TestDateTime"": ""12/22/2012 00:00:00"",
836+
""TestDecimal"": 10.333,
837+
""TestSByte"": -5,
838+
""TestShort"": 1024,
839+
""TestUShort"": 30000,
840+
""TestUInt"": 90000000,
841+
""TestULong"": 18446744073709551615,
842+
""TestDateTimeOffset"": ""2019-09-18T16:47:50.1234567+08:00""
837843
}";
838844

839845
ValueTypesTest test = JsonMapper.ToObject<ValueTypesTest> (json);
@@ -848,6 +854,10 @@ public void ImportValueTypesTest ()
848854
Assert.AreEqual (30000, test.TestUShort, "A7");
849855
Assert.AreEqual (90000000, test.TestUInt, "A8");
850856
Assert.AreEqual (18446744073709551615L, test.TestULong, "A9");
857+
Assert.AreEqual(
858+
new DateTimeOffset(2019, 9, 18, 16, 47,
859+
50, 123, TimeSpan.FromHours(8)).AddTicks(4567),
860+
test.TestDateTimeOffset, "A10");
851861
}
852862

853863
[Test]

0 commit comments

Comments
 (0)