// /** // * File: JSONDateTimeConverter.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 JSONDateTimeConverter : IJsonConvert { public JSONDateTimeConverter() { } public string GetSkyType(Type nativeType) { if (typeof(DateTime).Equals(nativeType) || typeof(DateTimeOffset).Equals(nativeType)) return "datetime"; return null; } public bool JSON2Value(Type targetType, JToken jToken, ref object value) { if (typeof(DateTime).Equals(targetType)) { value = DateTimeOffset.FromUnixTimeSeconds(jToken.Value()).DateTime; return true; } else if (typeof(DateTimeOffset).Equals(targetType)) { value = DateTimeOffset.FromUnixTimeSeconds(jToken.Value()); return true; } return false; } public bool Value2JSON(object value, ref JToken jToken) { if (value is DateTime) { jToken = JToken.FromObject(new DateTimeOffset((DateTime)value).ToUnixTimeSeconds()); return true; } else if (value is DateTimeOffset) { jToken = JToken.FromObject(((DateTimeOffset)value).ToUnixTimeSeconds()); return true; } return false; } } }