-
Notifications
You must be signed in to change notification settings - Fork 573
/
Copy pathdatetime_json_converter.cs
48 lines (41 loc) · 1.17 KB
/
datetime_json_converter.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
using System;
using FluentAssertions;
using Newtonsoft.Json;
using Stripe.Infrastructure;
using Xunit;
namespace Stripe.Tests.Xunit
{
public class datetime_json_converter
{
[Fact]
public void should_roundtrip_datetimes()
{
var obj = new TestObject
{
Date = RoundedDate()
};
var reloaded = JsonConvert.DeserializeObject<TestObject>(JsonConvert.SerializeObject(obj));
reloaded.Date.Should().Be(obj.Date);
}
[Fact]
public void should_handle_null()
{
var obj = new TestObject
{
Date = null
};
var reloaded = JsonConvert.DeserializeObject<TestObject>(JsonConvert.SerializeObject(obj));
reloaded.Date.Should().BeNull();
}
private static DateTime RoundedDate()
{
var date = DateTime.UtcNow;
return date.AddTicks( -1 * (date.Ticks % TimeSpan.TicksPerSecond));
}
public class TestObject
{
[JsonConverter(typeof(StripeDateTimeConverter))]
public DateTime? Date { get; set; }
}
}
}