Skip to content
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

Change StripeDateTimeConverter to write epoch not MS /Date(.)/ format #1144

Merged
merged 1 commit into from
Mar 23, 2018
Merged
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,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; }
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ internal class StripeDateTimeConverter : DateTimeConverterBase
{
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
writer.WriteRawValue(@"""\/Date(" + EpochTime.ConvertDateTimeToEpoch((DateTime)value).ToString() + @")\/""");
if (value == null)
writer.WriteNull();
else
writer.WriteRawValue(((DateTime)value).ConvertDateTimeToEpoch().ToString());
}

public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
Expand Down