Created
September 25, 2013 12:12
-
-
Save tugberkugurlu/6698790 to your computer and use it in GitHub Desktop.
JSON.NET TimeSpan serialization/deserialization
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Newtonsoft.Json; | |
using System; | |
using Xunit; | |
namespace JsonNetTimeSpanTests | |
{ | |
public class Model | |
{ | |
public string Name { get; set; } | |
public TimeSpan Interval { get; set; } | |
} | |
public class Class1 | |
{ | |
[Fact] | |
public void SerializeTimeSpan() | |
{ | |
Model model = new Model | |
{ | |
Name = "Tugberk", | |
Interval = new TimeSpan(1, 2, 3) | |
}; | |
string val = JsonConvert.SerializeObject(model); | |
Console.WriteLine(val); | |
} | |
[Fact] | |
public void DeserializeTimeSpan() | |
{ | |
string json = "{ \"Name\":\"Tugberk\",\"Interval\":\"01:02:03\" }"; | |
Model model = JsonConvert.DeserializeObject<Model>(json); | |
Console.WriteLine(model.Interval); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment