json.net: DateTimeStringConverter gets an already DateTime converted object in ReadJson()

后端 未结 1 1545
既然无缘
既然无缘 2020-12-11 22:25

Prerequisites:

  • JSON.Net 11.0.2

I need to store the UTC DateTime round-trip date/time pattern via a JSON based REST-API.



        
相关标签:
1条回答
  • 2020-12-11 22:50

    This is a known behavior of Json.Net. Since JSON does not have a built-in syntax for denoting dates (like it does for booleans), they have to be represented as strings. By default Json.Net tries to be nice and parse date-looking strings for you.

    If you are using your own converter for dates, or otherwise want to handle date parsing yourself, you need to be sure to set the DateParseHandling setting to None in the JsonSerializerSettings, otherwise the internal reader will try to handle it first.

    JsonSerializerSettings settings = new JsonSerializerSettings
    {
        Converters = new List<JsonConverter> { new DateTimeStringConverter() },
        DateParseHandling = DateParseHandling.None
    };
    
    var foo = JsonConvert.DeserializeObject<Foo>(json, settings);
    
    0 讨论(0)
提交回复
热议问题