Json.NET Disable the deserialization on DateTime
When parsing from an object
to JObject
you can specify a JsonSerializer
which instructs how to handle dates.
JObject.FromObject(new { time = s },
new JsonSerializer {
DateParseHandling = DateParseHandling.None
});
Unfortunately Parse
doesn't have this option, although it would make sense to have it. Looking at the source for Parse
we can see that all it does is instantiate a JsonReader
and then passes that to Load
. JsonReader
does have parsing options.
You can achieve your desired result like this:
using(JsonReader reader = new JsonTextReader(new StringReader(j1.ToString()))) {
reader.DateParseHandling = DateParseHandling.None;
JObject o = JObject.Load(reader);
}
For background, see Json.NET interprets and modifies ISO dates when deserializing to JObject #862, specifically this comment from JamesNK: I have no plans to change it, and I would do it again if give the chance.
You can accomplish this using JsonConvert.DeserializeObject
as well, by using JsonSerializerSettings
:
string s = "2012-08-08T01:54:45.3042880+00:00";
string jsonStr = $@"{{""time"":""{s}""}}";
JObject j1 = JsonConvert.DeserializeObject<JObject>(jsonStr, new JsonSerializerSettings {DateParseHandling = DateParseHandling.None});