neuecc / Utf8Json

Definitely Fastest and Zero Allocation JSON Serializer for C#(NET, .NET Core, Unity, Xamarin).
MIT License
2.36k stars 267 forks source link

Ability to deserialize from a strange DateTime format #152

Open dylanvdmerwe opened 5 years ago

dylanvdmerwe commented 5 years ago

I have JSON which looks like this:

{"Date":"\/Date(1552660288417+0200)\/"}
        public class TestDto
        {
            public DateTime Date { get; set; }
        }

  string response = Utf8Json.JsonSerializer.ToJsonString(new TestDto { Date = DateTime.Now });
  var dto = Utf8Json.JsonSerializer.Deserialize<TestDto>(response);
System.InvalidOperationException: 'invalid datetime format. value:/Date(1552660688703+0200)/'

I want to know how I can add in a resolver to handle this type of DateTime format so that the deserialization does not fail. I don't want to serialize into this format, just not fail to create a DateTime when this format is experienced.

As a side note, this is caused by ServiceStack.Text JsonSerializer which I am swapping to Utf8Json.

neuecc commented 5 years ago

It is legacy Microsoft DateTime format. You can create custom DateTimeFormatter(parse yourself) and add it to resolver.