Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Quote timespans in JsonSerializer

  • Loading branch information...
commit 52cb0b432e57897f65943f59adad1abbb5d9c26a 1 parent 91e7e3b
@mythz mythz authored
View
8 src/ServiceStack.Text/Json/JsonTypeSerializer.cs
@@ -127,14 +127,14 @@ public void WriteNullableDateTimeOffset(TextWriter writer, object dateTimeOffset
}
public void WriteTimeSpan(TextWriter writer, object oTimeSpan)
- {
- writer.Write(DateTimeSerializer.ToXsdTimeSpanString((TimeSpan)oTimeSpan));
+ {
+ WriteRawString(writer, DateTimeSerializer.ToXsdTimeSpanString((TimeSpan)oTimeSpan));
}
public void WriteNullableTimeSpan(TextWriter writer, object oTimeSpan)
{
- if (oTimeSpan == null) return;
- writer.Write(DateTimeSerializer.ToXsdTimeSpanString((TimeSpan?)oTimeSpan));
+ if (oTimeSpan == null) return;
+ WriteRawString(writer, DateTimeSerializer.ToXsdTimeSpanString((TimeSpan)oTimeSpan));
}
public void WriteGuid(TextWriter writer, object oValue)
View
68 tests/ServiceStack.Text.Tests/DateTimeOffsetAndTimeSpanTests.cs
@@ -0,0 +1,68 @@
+using System;
+using NUnit.Framework;
+using ServiceStack.ServiceModel.Serialization;
+
+namespace ServiceStack.Text.Tests
+{
+ [TestFixture]
+ public class DateTimeOffsetAndTimeSpanTests : TestBase
+ {
+ [TestFixtureSetUp]
+ public void TestFixtureSetUp()
+ {
+ JsonDataContractSerializer.Instance.UseBcl = true;
+ }
+
+ [TestFixtureTearDown]
+ public void TestFixtureTearDown()
+ {
+ JsonDataContractSerializer.Instance.UseBcl = false;
+ }
+
+ [Test]
+ public void Can_Serializable_DateTimeOffset_Field()
+ {
+ var model = new SampleModel { Id = 1, Date = new DateTimeOffset(2012, 6, 27, 11, 26, 04, 524, TimeSpan.FromHours(7)) };
+
+ //Behaviour of .NET's BCL classes
+ //JsonDataContractSerializer.Instance.SerializeToString(model).Print();
+ //DataContractSerializer.Instance.Parse(model).Print();
+
+ var json = JsonSerializer.SerializeToString(model);
+ Assert.That(json, Is.StringContaining("\"TimeSpan\":\"PT0S\""));
+
+ var fromJson = json.FromJson<SampleModel>();
+
+ Assert.That(model.Date, Is.EqualTo(fromJson.Date));
+ Assert.That(model.TimeSpan, Is.EqualTo(fromJson.TimeSpan));
+
+ Serialize(fromJson);
+ }
+
+ [Test]
+ public void Can_serialize_TimeSpan_field()
+ {
+ var fromDate = new DateTime(2069, 01, 02);
+ var toDate = new DateTime(2079, 01, 02);
+ var period = toDate - fromDate;
+
+ var model = new SampleModel { Id = 1, TimeSpan = period };
+ var json = JsonSerializer.SerializeToString(model);
+ Assert.That(json, Is.StringContaining("\"TimeSpan\":\"P3652D\""));
+
+ //Behaviour of .NET's BCL classes
+ //JsonDataContractSerializer.Instance.SerializeToString(model).Print();
+ //DataContractSerializer.Instance.Parse(model).Print();
+
+ Serialize(model);
+ }
+
+ public class SampleModel
+ {
+ public int Id { get; set; }
+
+ public DateTimeOffset Date { get; set; }
+ public TimeSpan TimeSpan { get; set; }
+ }
+ }
+}
View
27 tests/ServiceStack.Text.Tests/EntityWithDateTimeOffsetTests.cs
@@ -1,27 +0,0 @@
-namespace ServiceStack.Text.Tests
-{
- using System;
- using NUnit.Framework;
-
- public class EntityWithDateTimeOffsetTests
- {
- [Test]
- public void CanSerializableDateTimeOffsetField()
- {
- var model = new SampleModel { Id = 1, Date = new DateTimeOffset(2012, 6, 27, 11, 26, 04, 524, TimeSpan.FromHours(7)) };
-
- var s = JsonSerializer.SerializeToString(model);
-
- var afterModel = JsonSerializer.DeserializeFromString<SampleModel>("{\"Id\":1,\"Date\":\"\\/Date(1340771164524+0700)\\/\"}");
-
- Assert.AreEqual(model.Date, afterModel.Date);
- }
-
- public class SampleModel
- {
- public int Id { get; set; }
-
- public DateTimeOffset Date { get; set; }
- }
- }
-}
View
2  tests/ServiceStack.Text.Tests/ServiceStack.Text.Tests.csproj
@@ -168,7 +168,7 @@
<Compile Include="BclStructTests.cs" />
<Compile Include="CyclicalDependencyTests.cs" />
<Compile Include="DynamicModels\GoogleMapsApiTests.cs" />
- <Compile Include="EntityWithDateTimeOffsetTests.cs" />
+ <Compile Include="DateTimeOffsetAndTimeSpanTests.cs" />
<Compile Include="JsonObjectTests.cs" />
<Compile Include="JsonTests\AnonymousDeserializationTests.cs" />
<Compile Include="JsonTests\CamelCaseTests.cs" />
Please sign in to comment.
Something went wrong with that request. Please try again.