DateTimeOffset property not handled correctly


If you have a DateTimeOffset property ToJSON recurses due to WriteValue not having a check for DateTimeOffset. WriteValue calls WriteObject for DateTimeOffset.MinValue and DateTimeOffset.MaxValue.

e.g. output from serialization of an instance of
    class ClassWithDateTimeOffset
        public DateTimeOffset Offset { get; set; }
has this for the Offset property:


Depending on the scenario it can hit the MAX_DEPTH limit.

The simplest fix is to add the below to the WriteValue if/else if, although I'm not sure if that's the most technically correct way to output a DateTimeOffset.
        else if (obj is DateTimeOffset)
Closed Jul 15, 2016 at 6:04 PM by MGholam
Fixed in 2.1.17


MGholam wrote Jul 27, 2015 at 2:21 PM


You can add your own types with RegisterCustomType()

skottmckay wrote Jun 20, 2016 at 9:40 AM

It has been almost a year and it's a 2 line change to support a built in .NET type. Should I submit a pull request on git or is this project dead?

MGholam wrote Jun 20, 2016 at 10:31 AM

Like I said you can use RegisterCustomType(), also casting it to a DateTime is not valid since you loose the Offset timezone property.

skottmckay wrote Jun 21, 2016 at 8:37 AM

I understand RegisterCustomType can be used, it's just that I've hit this multiple times and wonder whether it should be directly supported given it's a standard C# type. Using the roundtrip format would be most correct wouldn't it, or is this something that you don't want to add given the many different date time formats that a DateTimeOffset could be written out in? Even detecting a DateTimeOffset in read or write paths with no custom handler and providing a help message about using RegisterCustomType would be be beneficial.