This project is read-only.
1

Closed

DateTimeOffset property not handled correctly

description

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:

"Offset":{"$type":"2","MinValue":{"$type":"2","MinValue":{"$i":3},"MaxValue":{"$type":"2","MinValue":{"$i":3},"MaxValue":{"$i":4}}},"MaxValue":{"$i":4}}}

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)
            WriteDateTime(((DateTimeOffset)obj).DateTime);
Closed Jul 15, 2016 at 7:04 PM by MGholam
Fixed in 2.1.17

comments

MGholam wrote Jul 27, 2015 at 3:21 PM

Thanks!

You can add your own types with RegisterCustomType()

skottmckay wrote Jun 20, 2016 at 10: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 11: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 9: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.