question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

JObject.Parse vs DateTime

See original GitHub issue

Given the following code snippet

String js = "{ \"DateTime\": \"2017-03-07T17:26:40.352Z\" }";
var jo = JObject.Parse(js);
String ds1 = (String)jo["DateTime"];

results "ds1 = “03/07/2017 17:26:40"” which is not quite correct, because it misses fractions of second and timezone information. DateTime.Parse/TryParse will not be correct then. This is a problem in my app, because I want to use DateTime.TryParse to check for correctly formatted input data. I cannot use (DateTime)jo[“DateTime”] because it might throw an exception. (Yes, this exception could be caught, but this is what TryParse is for, isn’t it).

Actually I would have expected ds1 to be “2017-03-07T17:26:40.352Z”, but this may be another story…

Issue Analytics

  • State:open
  • Created 7 years ago
  • Comments:6

github_iconTop GitHub Comments

8reactions
k-yoshimotocommented, Feb 7, 2019

Found better one.

JObject.Load method can take a JsonReader as a parameter so we can change its settings without modifying the source code.

JObject jobject;
using(var reader = new JsonTextReader(new StringReader(json)) { DateParseHandling = DateParseHandling.None })
    jobject = JObject.Load(reader);

myObject = jobject["flagment"].ToObject<MyObject>();
7reactions
k-yoshimotocommented, Jan 30, 2019

Same here.

I wanted to parse a part of object in a JSON string and sadly I wrote something like this:

var jobject = JObject.Parse(json);
var flagment = jobject["Flagment"].ToString(Formatting.None);
JsonConvert.DeserializeObject(flagment);

Made up #1969 to work this around. Hope it will be accepted.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to avoid JObject.Parse parsing a DateTime, and ...
Basically he wants Json.net to not interpret it as a DateTime and instead just return the original string directly.
Read more >
DateParseHandling Enumeration
DateParseHandling Enumeration ; None, 0, Date formatted strings are not parsed to a date type and are read as strings. ; DateTime, 1,...
Read more >
Json.NET unexpected defaults for parsing date - Vincent Costel
Because, by default, if the parser recognizes an ISO8601 date string, it will automatically convert it to a DateTime object, therefore losing ...
Read more >
DateTime.Parse Method (System)
The Parse method tries to convert the string representation of a date and time value to its DateTime equivalent. It tries to parse...
Read more >
String To DateTime Conversion In C#
DateTime.Parse() · Value. It is a string representation of date and time. · Provider. It is an object which provides culture-specific info.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found