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

Commit 4fc5a15

Browse files
committed
Add example usage of JsConfig<DateTime>.RawSerializeFn
1 parent e5d756e commit 4fc5a15

File tree

1 file changed

+31
-3
lines changed

1 file changed

+31
-3
lines changed

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

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Globalization;
23
using NUnit.Framework;
34

45
namespace ServiceStack.Text.Tests.JsonTests
@@ -11,7 +12,7 @@ public void TestFixtureTearDown()
1112
{
1213
JsConfig.Reset();
1314
}
14-
15+
1516
public class RealType
1617
{
1718
public string Name { get; set; }
@@ -173,7 +174,8 @@ public void Can_call_different_nested_types_custom_serializers()
173174
JsConfig<OuterType>.RawDeserializeFn = str =>
174175
{
175176
var d = str.FromJson<InnerType>();
176-
return new OuterType {
177+
return new OuterType
178+
{
177179
P1 = d
178180
};
179181
};
@@ -191,6 +193,31 @@ public void Can_call_different_nested_types_custom_serializers()
191193
Assert.That(outer.P1.A, Is.EqualTo("Hello"));
192194
Assert.That(outer.P1.B, Is.EqualTo("World"));
193195
}
196+
197+
public class Response
198+
{
199+
public DateTime DateTime { get; set; }
200+
}
201+
202+
[Test]
203+
public void Can_serialize_custom_DateTime()
204+
{
205+
JsConfig<DateTime>.RawSerializeFn = time =>
206+
{
207+
var result = time;
208+
if (time.Kind == DateTimeKind.Unspecified)
209+
{
210+
result = DateTime.SpecifyKind(result, DateTimeKind.Local);
211+
}
212+
return result.ToString(CultureInfo.InvariantCulture);
213+
};
214+
215+
var dto = new Response { DateTime = new DateTime(2001, 1, 1, 1, 1, 1) };
216+
217+
var csv = dto.ToCsv();
218+
219+
Assert.That(csv, Is.EqualTo("DateTime\r\n\"01/01/2001 01:01:01\"\r\n"));
220+
}
194221
}
195222

196223
public class OuterType
@@ -212,7 +239,8 @@ public static string Serialize(InnerType o)
212239
public static InnerType Deserialize(string s)
213240
{
214241
var p = s.Split('-');
215-
return new InnerType {
242+
return new InnerType
243+
{
216244
A = p[0],
217245
B = p[1]
218246
};

0 commit comments

Comments
 (0)