Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Quote timespans in JsonSerializer

  • Loading branch information...
commit 52cb0b432e57897f65943f59adad1abbb5d9c26a 1 parent 91e7e3b
Demis Bellot authored
8  src/ServiceStack.Text/Json/JsonTypeSerializer.cs
@@ -127,14 +127,14 @@ public void WriteNullableDateTimeOffset(TextWriter writer, object dateTimeOffset
127 127
         }
128 128
 
129 129
         public void WriteTimeSpan(TextWriter writer, object oTimeSpan)
130  
-        {
131  
-            writer.Write(DateTimeSerializer.ToXsdTimeSpanString((TimeSpan)oTimeSpan));
  130
+        {
  131
+            WriteRawString(writer, DateTimeSerializer.ToXsdTimeSpanString((TimeSpan)oTimeSpan));            
132 132
         }
133 133
 
134 134
         public void WriteNullableTimeSpan(TextWriter writer, object oTimeSpan)
135 135
         {
136  
-            if (oTimeSpan == null) return;
137  
-            writer.Write(DateTimeSerializer.ToXsdTimeSpanString((TimeSpan?)oTimeSpan));
  136
+            if (oTimeSpan == null) return;
  137
+            WriteRawString(writer, DateTimeSerializer.ToXsdTimeSpanString((TimeSpan)oTimeSpan));
138 138
         }
139 139
 
140 140
         public void WriteGuid(TextWriter writer, object oValue)
68  tests/ServiceStack.Text.Tests/DateTimeOffsetAndTimeSpanTests.cs
... ...
@@ -0,0 +1,68 @@
  1
+using System;
  2
+using NUnit.Framework;
  3
+using ServiceStack.ServiceModel.Serialization;
  4
+
  5
+namespace ServiceStack.Text.Tests
  6
+{
  7
+    [TestFixture]
  8
+    public class DateTimeOffsetAndTimeSpanTests : TestBase
  9
+    {
  10
+        [TestFixtureSetUp]
  11
+        public void TestFixtureSetUp()
  12
+        {
  13
+            JsonDataContractSerializer.Instance.UseBcl = true;
  14
+        }
  15
+
  16
+        [TestFixtureTearDown]
  17
+        public void TestFixtureTearDown()
  18
+        {
  19
+            JsonDataContractSerializer.Instance.UseBcl = false;            
  20
+        }
  21
+
  22
+        [Test]
  23
+        public void Can_Serializable_DateTimeOffset_Field()
  24
+        {
  25
+            var model = new SampleModel { Id = 1, Date = new DateTimeOffset(2012, 6, 27, 11, 26, 04, 524, TimeSpan.FromHours(7)) };
  26
+
  27
+            //Behaviour of .NET's BCL classes
  28
+            //JsonDataContractSerializer.Instance.SerializeToString(model).Print();
  29
+            //DataContractSerializer.Instance.Parse(model).Print();
  30
+
  31
+            var json = JsonSerializer.SerializeToString(model);
  32
+            Assert.That(json, Is.StringContaining("\"TimeSpan\":\"PT0S\""));
  33
+
  34
+            var fromJson = json.FromJson<SampleModel>();
  35
+
  36
+            Assert.That(model.Date, Is.EqualTo(fromJson.Date));
  37
+            Assert.That(model.TimeSpan, Is.EqualTo(fromJson.TimeSpan));
  38
+
  39
+            Serialize(fromJson);
  40
+        }
  41
+
  42
+        [Test]
  43
+        public void Can_serialize_TimeSpan_field()
  44
+        {
  45
+            var fromDate = new DateTime(2069, 01, 02);
  46
+            var toDate = new DateTime(2079, 01, 02);
  47
+            var period = toDate - fromDate;
  48
+
  49
+            var model = new SampleModel { Id = 1, TimeSpan = period };
  50
+            var json = JsonSerializer.SerializeToString(model);
  51
+            Assert.That(json, Is.StringContaining("\"TimeSpan\":\"P3652D\""));
  52
+
  53
+            //Behaviour of .NET's BCL classes
  54
+            //JsonDataContractSerializer.Instance.SerializeToString(model).Print();
  55
+            //DataContractSerializer.Instance.Parse(model).Print();
  56
+
  57
+            Serialize(model);
  58
+        }
  59
+
  60
+        public class SampleModel
  61
+        {
  62
+            public int Id { get; set; }
  63
+
  64
+            public DateTimeOffset Date { get; set; }
  65
+            public TimeSpan TimeSpan { get; set; }
  66
+        }
  67
+    }
  68
+}
27  tests/ServiceStack.Text.Tests/EntityWithDateTimeOffsetTests.cs
... ...
@@ -1,27 +0,0 @@
1  
-namespace ServiceStack.Text.Tests
2  
-{
3  
-    using System;
4  
-    using NUnit.Framework;
5  
-
6  
-    public class EntityWithDateTimeOffsetTests
7  
-    {
8  
-        [Test]
9  
-        public void CanSerializableDateTimeOffsetField()
10  
-        {
11  
-            var model = new SampleModel { Id = 1, Date = new DateTimeOffset(2012, 6, 27, 11, 26, 04, 524, TimeSpan.FromHours(7)) };
12  
-
13  
-            var s = JsonSerializer.SerializeToString(model);
14  
-
15  
-            var afterModel = JsonSerializer.DeserializeFromString<SampleModel>("{\"Id\":1,\"Date\":\"\\/Date(1340771164524+0700)\\/\"}");
16  
-
17  
-            Assert.AreEqual(model.Date, afterModel.Date);
18  
-        }
19  
-
20  
-        public class SampleModel
21  
-        {
22  
-            public int Id { get; set; }
23  
-
24  
-            public DateTimeOffset Date { get; set; }
25  
-        }
26  
-    }
27  
-}
2  tests/ServiceStack.Text.Tests/ServiceStack.Text.Tests.csproj
@@ -168,7 +168,7 @@
168 168
     <Compile Include="BclStructTests.cs" />
169 169
     <Compile Include="CyclicalDependencyTests.cs" />
170 170
     <Compile Include="DynamicModels\GoogleMapsApiTests.cs" />
171  
-    <Compile Include="EntityWithDateTimeOffsetTests.cs" />
  171
+    <Compile Include="DateTimeOffsetAndTimeSpanTests.cs" />
172 172
     <Compile Include="JsonObjectTests.cs" />
173 173
     <Compile Include="JsonTests\AnonymousDeserializationTests.cs" />
174 174
     <Compile Include="JsonTests\CamelCaseTests.cs" />

0 notes on commit 52cb0b4

Please sign in to comment.
Something went wrong with that request. Please try again.