Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Based on what I've seen while using JSON.NET for a while now, you will never, with the default settings, parse a string and retrieve a token with type <code>JTokenType.TimeSpan</code> (same for some other types as well, such as Guid or Uri). I have a pretty good guess of why this is the case (based on my experience working a few years ago with the <code>DataContractJsonSerializer</code>).</p> <p>Basically, it's a matter of how much information the parser can retrieve out of the input. JSON is a very simple syntax which only knows about <em>numbers</em>, <em>boolean</em> and <em>strings</em> (in addition to arrays and objects). Many CLR types don't have a native JSON type (Uri, DateTime, DateTimeOffset, TimeSpan, and so on), so when any JSON parser is reading the data, it will try to use the best match.</p> <p>If you're deserializing the JSON string into a CLR data type, then the serializer has some additional information that it can use to disambiguate what a JSON string maps to - the type of the field / property that value is being deserialized to. However, when you're deserializing a JSON data to a <code>JToken</code> object graph, there's no additional information, and JSON.NET has to choose one type. The most natural type to deserialize a JSON string is, well, a CLR string.</p> <p>But why do dates are deserialized correctly as <code>JTokenType.Date</code>? IIRC, the JSON.NET reader has a special code for dates (controlled by the <code>DateParseHandling</code> enumeration), which tries to match the parsed strings to some predefined formats (either ISO 8601 or the old Microsoft ASP.NET AJAX format), and if it finds a string which match it, it will read it as a <code>DateTime</code> (or <code>DateTimeOffset</code>) instead of a string. I don't know whether it's possible to extend that behavior to also support <code>TimeSpan</code> or other types, but I wouldn't be surprised, since the extensibility in JSON.NET is quite good.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload