Skip to content

Instantly share code, notes, and snippets.

@tugberkugurlu
Created September 25, 2013 12:12
Show Gist options
  • Save tugberkugurlu/6698790 to your computer and use it in GitHub Desktop.
Save tugberkugurlu/6698790 to your computer and use it in GitHub Desktop.
JSON.NET TimeSpan serialization/deserialization
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