Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.

Commit 5a7ae04

Browse files
Siliconrobmythz
authored andcommitted
JsonSerializer/JsvTypeSerializer inconsistently uses the AssumeUtc config setting (#505)
DateTime handling is inconsistent with respect to the AssumcUtc when using the JSConfig setting. If serializing to JSON - The JsonTypeSerializer will use the UTC specification type and convert Unspecified DateTime values to UTC in method "WriteWcfJsonDate" If serializing to CSV- The JsvTypeSerializer will not use the UTC specification type and does not convert Unspecified DateTime values to UTC in method "ToShortestXsdDateTimeString" Adding an internal extension method "UseConfigSpecifiedSetting" to match DateTime conversion between the two different serializers.
1 parent a87c874 commit 5a7ae04

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

src/ServiceStack.Text/Common/DateTimeSerializer.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,7 @@ public static TimeSpan ParseXsdTimeSpan(string dateTimeStr)
442442

443443
public static string ToShortestXsdDateTimeString(DateTime dateTime)
444444
{
445+
dateTime = dateTime.UseConfigSpecifiedSetting();
445446
var timeOfDay = dateTime.TimeOfDay;
446447

447448
var isStartOfDay = timeOfDay.Ticks == 0;
@@ -582,13 +583,18 @@ public static TimeZoneInfo GetLocalTimeZoneInfo()
582583

583584
internal static TimeZoneInfo LocalTimeZone = GetLocalTimeZoneInfo();
584585

585-
public static void WriteWcfJsonDate(TextWriter writer, DateTime dateTime)
586+
private static DateTime UseConfigSpecifiedSetting(this DateTime dateTime)
586587
{
587588
if (JsConfig.AssumeUtc && dateTime.Kind == DateTimeKind.Unspecified)
588589
{
589-
dateTime = DateTime.SpecifyKind(dateTime, DateTimeKind.Utc);
590+
return DateTime.SpecifyKind(dateTime, DateTimeKind.Utc);
590591
}
592+
return dateTime;
593+
}
591594

595+
public static void WriteWcfJsonDate(TextWriter writer, DateTime dateTime)
596+
{
597+
dateTime = dateTime.UseConfigSpecifiedSetting();
592598
switch (JsConfig.DateHandler)
593599
{
594600
case DateHandler.ISO8601:

tests/ServiceStack.Text.Tests/JsonTests/JsonDateTimeTests.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Linq;
33
using NUnit.Framework;
44
using ServiceStack.Text.Common;
5+
using ServiceStack.Text.Jsv;
56

67
namespace ServiceStack.Text.Tests.JsonTests
78
{
@@ -309,6 +310,27 @@ public void When_using_ISO8601_and_serializing_as_Utc_It_should_deserialize_as_U
309310
Assert.AreEqual(initialDate, deserializedDate);
310311
}
311312

313+
[Test]
314+
public void ISO8601_assumeUtc_serialize_datetime_is_the_same()
315+
{
316+
JsConfig.AssumeUtc = true;
317+
JsConfig.DateHandler = DateHandler.ISO8601;
318+
var initialDate = new DateTime(2012, 7, 25, 16, 17, 00, DateTimeKind.Unspecified);
319+
var writers = new
320+
{
321+
jsv = new System.IO.StringWriter(new System.Text.StringBuilder()),
322+
json = new System.IO.StringWriter(new System.Text.StringBuilder())
323+
};
324+
new JsvTypeSerializer().WriteDateTime(writers.jsv, initialDate);
325+
new Json.JsonTypeSerializer().WriteDateTime(writers.json, initialDate);
326+
var results = new
327+
{
328+
jsv = DateTime.SpecifyKind(DateTime.Parse(writers.jsv.ToString()), DateTimeKind.Utc),
329+
json = DateTime.SpecifyKind(DateTime.Parse(writers.json.ToString().Replace("\"", "")), DateTimeKind.Utc)
330+
};
331+
Assert.AreEqual(results.jsv, results.json);
332+
}
333+
312334
[Test]
313335
public void Can_serialize_json_date_iso8601_utc()
314336
{

0 commit comments

Comments
 (0)