2

I have a strange problem. I am using the JSON Framework from newtonsoft. But it is changing the date formatting of some of my strings.

I transfer some strings within a JSON string like this:

{"MyStrings":["Hello","World"]}

I have this matching DTO class:

public class Test
{
    public List<string> MyStrings { get; set; }
}

Now I want to deserialize it into my DTO class like this:

string content = @"{""MyStrings"":[""2016-10-07T13:37:01.4209934Z"",""2016-10-07T13:37:01.4209934Z""]}";
var obj = JObject.Parse(content);
var test = obj.ToObject<Test>();

You see, in the JSON string there are strings that coincidentally look like a datetime value.

{
  "MyStrings":
    ["2016-10-07T13:37:01.4209934Z",
    "2016-10-07T13:37:01.4209934Z"]
}

If we investigate the string values, we get: "10/07/2016 13:37:01". Something completely different.

What is going on here? I did not change the default behavior. JSON.NET is changing the string values on his own. Why is this framework changing my string values? I want to leave it as it is - a string.

is this a bug in JSON.NET, or can I control this behavior with a special setting?

I tried all enum settings in the JsonSerializerSettings.

-DateParseHandling.None
-DateFormatString
-DateFormatHandling
-DateTimeZoneHandling

Nothing worked.

5
  • It looks like the library is parsing the contents of your JSON list as a list of DateTimes and then calling the .ToString method over them. Quite weird it looks like it's doing some extra unwanted work... Commented Oct 7, 2016 at 15:20
  • Possible duplicate of Json.NET Disable the deserialization on DateTime Commented Oct 7, 2016 at 15:32
  • @BrianRogers - Also related: JToken: Get raw/original JSON value and JSON.NET: Get Specific JSON Date Value. Given that this keeps coming up, I'm leaning towards thinking that the fact that ((JValue)DateTime.Now).ToString() outputs in current culture format rather than invariant ISO 8601 format is a bug. Commented Oct 7, 2016 at 21:25
  • @dbc No argument from me-- it does seem very odd that JValue.ToString() outputs dates in a different format than JObject.ToString() does. Commented Oct 11, 2016 at 6:58
  • 1
    I've been running into this and have found an alternative to JSON.NET that doesn't have these DateTime issues, as it doesn't attempt to represent DateTimes within it's models github.com/gregsdennis/Manatee.Json Commented Jul 12, 2017 at 9:36

2 Answers 2

3

I think that's because you're using the standard JObject.Parse, which 'thinks' that these values are dates (and then call a ToString() on them when you do the .ToObject()).

Try this:

var foo = JsonConvert.DeserializeObject<Test>(content);

Doing this, the deserializer will know which is the target type of every property, I guess.

JsonConvert is a static class inside Newtonsoft.Json .

Sign up to request clarification or add additional context in comments.

2 Comments

I cant directly use DeserializeObject<>. We use JSON.NET in a large engine and first it will be parsed. Later it will be converted into Type 'X'. So I have to split this. But I found a solution for the problem.
Thanks so much for your answer. I found "2021-03-11T00:00:00.0" to be changed into "2021-03-11T00:00:00" because of this (check .0 at the end). As I needed to bounce the received message to the caller, it lead to all kinds of integration issues.
2

I found a solution for my problem. Thanks to qwertoyo for his answer.

I cant directly use DeserializeObject<>. We use JSON.NET in a large engine and first it will be parsed. Later it will be converted into type 'X'. So I have to split this.

This is working for my szenario:

JsonSerializerSettings settings = new JsonSerializerSettings
            {
                DateParseHandling = DateParseHandling.None
            };
object dto = jObject.ToObject(dtoType, JsonSerializer.Create(settings));

This is like the default-implementation for "JObject.ToObject" but you set your own JsonSerializerSettings.

1 Comment

Man this answer was a life-saver, thank you. We have experienced the behaviour when values were parsed into the controller properties and re-formatted by default. I think the DateParseHandling.None should be the default option and not this obscure behavior. I'm fine with formatting when parsing json->DateTime but not json->string.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.