ln.types/json/converter/JSONTimeSpanConverter.cs

49 lines
1.2 KiB
C#

// /**
// * File: JSONTimeSpanConverter.cs
// * Author: haraldwolff
// *
// * This file and it's content is copyrighted by the Author and / or copyright holder.
// * Any use wihtout proper permission is illegal and may lead to legal actions.
// *
// *
// **/
using System;
using Newtonsoft.Json.Linq;
namespace ln.types.json.converter
{
public class JSONTimeSpanConverter :IJsonConvert
{
public JSONTimeSpanConverter()
{
}
public string GetSkyType(Type nativeType)
{
if (typeof(TimeSpan).Equals(nativeType))
return "timespan";
return null;
}
public bool JSON2Value(Type targetType, JToken jToken, ref object value)
{
if (typeof(TimeSpan).Equals(targetType))
{
value = TimeSpan.FromSeconds(jToken.Value<double>());
return true;
}
return false;
}
public bool Value2JSON(object value, ref JToken jToken)
{
if (value is TimeSpan)
{
jToken = JToken.FromObject(((TimeSpan)value).TotalSeconds);
return true;
}
return false;
}
}
}