Note that there are some explanatory texts on larger screens.

plurals
  1. POSerializing Timespan in Dictionary<string, object> with Json.NET
    primarykey
    data
    text
    <p>I have a property that is actually a Dictionary. And I keep many types in this dictionary like TimeSpans, DateTimes, etc. But serializing and deserializing TimeSpans are wrong and it deserializes as string.</p> <pre><code>var dict = new Dictionary&lt;string, object&gt;(); dict.Add("int", 15); dict.Add("string", "foo"); dict.Add("timeSpan", new TimeSpan(1,1,1)); dict.Add("dateTime", DateTime.Now); var settings = new JsonSerializerSettings{ TypeNameHandling = TypeNameHandling.All, TypeNameAssemblyFormat = FormatterAssemblyStyle.Simple }; var serializedObj = JsonConvert.SerializeObject(dict, Newtonsoft.Json.Formatting.Indented, settings); var deserializedObj = (Dictionary&lt;string, object&gt;)JsonConvert.DeserializeObject(serializedObj, settings); //Displaying the types with LinqPad: deserializedObj["int"].GetType().Dump(); deserializedObj["string"].GetType().Dump(); deserializedObj["timeSpan"].GetType().Dump(); deserializedObj["dateTime"].GetType().Dump(); </code></pre> <p>RESULTS: <img src="https://i.stack.imgur.com/vaopM.png" alt="Types of the objects"></p> <p>So TimeSpan in an object can't deserialize to a timespan. I've tried with latest version of Json.Net too. But the result is same.</p> <p>How can I specify type name for TimeSpan? Or should I write a custom converter and how?</p> <p><strong>Edit:</strong></p> <p>I did some tests and I changed serialized timeSpan property like this:</p> <pre><code>""timeSpan"": { ""$type"": ""System.TimeSpan"", ""$value"": ""01:01:01""} </code></pre> <p>and this time json.net could desrialize it as TimeSpan. But How can I specify $type and $value of TimeSpan at Serialization level like this?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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