2

I'm using OrmLite and one of my entities has property of type Dictionary of DateTime and int and it fails to deserialize that property. I found out that this is Jsv serializer problem. I have failing test:

[Test]
public void Can_serialize_and_deserialize_Dictionary_with_DateTime_key()
{
    JsConfig.DateHandler = JsonDateHandler.TimestampOffset;
    var expected = new Dictionary<DateTime, int> {{DateTime.Now, 5}};
    var serializedString = TypeSerializer.SerializeToString(expected);
    var actual = TypeSerializer.DeserializeFromString<Dictionary<DateTime, int>>(serializedString);
    Assert.AreEqual(actual.Keys.First().Year, expected.Keys.First().Year);
}

Can anyone suggest how this could be fixed? This seems like Jsv format problem in general and not implementation bug. (but I might be wrong)

Andrej Slivko
  • 1,236
  • 2
  • 12
  • 27
  • The problem is that JSV can't deserialize this {2013-11-19T16:57:33.7901057-05:00:5}. There are too many colons. That's where it gets confused. – kampsj Nov 19 '13 at 22:08
  • @kampsj Yes. That is probably bug in Jsv serializer. It forgets to escape that date with "" – Andrej Slivko Nov 20 '13 at 12:31

1 Answers1

2

As workaround before this issue will be fixed in servicestack.text i assigned my own serialization functions for DateTime and DateTime?

JsConfig<DateTime>.SerializeFn = DateTimeSerializer.ToShortestXsdDateTimeString;
JsConfig<DateTime?>.SerializeFn = time => time.HasValue ? DateTimeSerializer.ToShortestXsdDateTimeString(time.Value) : null;

Those are same functions used inside servicestack.text, but when you assign your own functions library will check if your values needs escaping or not.

Andrej Slivko
  • 1,236
  • 2
  • 12
  • 27