0

I have a question connected with json deserilizing (Newtonsoft.Json). I don't understand the process deserialization DateTime types and date time formatted string, in other words type string, where it is written date like this "01.01.2000" and in JObject this field is string.

For example:

[
    {
        "DateOfBirth": "10.02.1990",
        "SomeString": "01.12.1888"
    }
]

I use the next deserialization setting:

    new JsonSerializerSettings
    {
       ContractResolver = new CamelCasePropertyNamesContractResolver(),
       MissingMemberHandling = MissingMemberHandling.Ignore,
       DateParseHandling = DateParseHandling.None,
       DateFormatHandling = DateFormatHandling.IsoDateFormat,
       DateFormatString = "dd.MM.yyyy",
       Converters = new List<JsonConverter>()
       {
          new StringEnumConverter(),
          new IsoDateTimeConverter
          {
             DateTimeFormat = "dd.MM.yyyy"
          }
       },
       Error = (sender, args) =>
       {
          args.ErrorContext.Handled = true;
       },
    }

By default I expected that the first field is deserialize to DateTime 10/02/1990 00:00:00 and the second field deserialized to simple string in some object. But this is not so. Both field convert to DateTime format. If I set DateParseHandling.None without IsoDateTimeConverter the result will be next: DateTime deserialize to null value and string will have "01.12.1888". But if I set both of this setting, everything OK.

Can someone explain this situation? And what should I do in order to get correct result without set random settings and hope for luck?

Guillaume CR
  • 3,006
  • 1
  • 19
  • 31
Mats
  • 1
  • 2
    why would you expect "SomeString" to be deserialized to a string? It looks like a valid date to me. – Sam I am says Reinstate Monica Sep 21 '17 at 20:39
  • Can please [edit] your question to include a full [mcve] that reproduces the problem? You show the settings but not how you are using them. Your problem might be related to [JToken: Get raw/original JSON value](https://stackoverflow.com/a/35141787/3744182), [Json.NET Disable the deserialization on DateTime](https://stackoverflow.com/q/11856694/3744182), or [JSON.NET: Get Specific JSON Date Value](https://stackoverflow.com/q/35166060/3744182), but I can't say for sure without seeing your complete code. – dbc Sep 21 '17 at 21:35
  • Also take a look at https://www.newtonsoft.com/json/help/html/datesinjson.htm. What you are seeing might not be a bug. – dbc Sep 21 '17 at 21:36

0 Answers0