0

I am talking to a web service from Xamarin. It is sending dates down over the wire in JSON, in ISO-8601 format, like this:

"2016-05-13T16:02:47.480-07:00"

If I try to deserialize just a single date, it works fine. But when the DateTime is a member of class, it does not work. No error is given. Here is some example code:

using ServiceStack.Text;

class TestDate {
   public DateTime  testDate;
}

void Test() {
    JsConfig.DateHandler = JsonDateHandler.ISO8601; 

    // this works just fine:
    String dateString = "2016-05-13T16:02:47.480-07:00";
    DateTime theDate = dateString.FromJson<DateTime> ();

    // this does not deserialize
    String testStr = "{\"testDate\":\"2016-05-13T16:02:51.923-07:00\"}";
    TestDate tester = testStr.FromJson<TestDate> ();
}

Any ideas? I don't see any error, just get a new TestDate object without the date being set.

Dave Vronay
  • 625
  • 7
  • 22

1 Answers1

0

ServiceStack Text Serializer does serialize as expected:

JsConfig.DateHandler = ServiceStack.Text.DateHandler.ISO8601;

var dto = new TestDate { testDate = new DateTime(2016, 05, 13, 16, 02, 47, 480) };
var json = dto.ToJson();
json.Print(); //= {"testDate":"2016-05-13T16:02:47.4800000"}

and deserializes correctly:

var fromJson = json.FromJson<TestDate>();
Assert.That(fromJson.testDate, Is.EqualTo(dto.testDate)); //= true
mythz
  • 141,670
  • 29
  • 246
  • 390
  • Ok i don't have control over the Web service, any ideas for the best way to parse it? Also, how come it works with a single date? – Dave Vronay May 14 '16 at 01:21
  • @DaveVronay It parses by default without changing the DateHandler also it looks like you're using a very old version of ServiceStack.Text where `JsonDateHandler` type no longer exists. – mythz May 14 '16 at 01:31
  • Also I just checked the page you linked and it seems that the ISO 8601 does support time zones... https://en.wikipedia.org/wiki/ISO_8601#Time_zone_designators – Dave Vronay May 14 '16 at 22:55
  • @DaveVronay Yeah apologies quick misread of docs, BTW this also works in the latest version of ServiceStack.Text. But you have an issue with your DTO, only **public properties** are serialized by default, otherwise you need to add `JsConfig.IncludePublicFields = true` – mythz May 15 '16 at 03:00