Statistics
| Branch: | Revision:

root / trunk / Pithos.Interfaces / PithosDateTimeConverter.cs @ 5e31048f

History | View | Annotate | Download (3.1 kB)

1
using System;
2
using System.Globalization;
3
using Newtonsoft.Json;
4
using Newtonsoft.Json.Converters;
5
using Newtonsoft.Json.Utilities;
6

    
7
namespace Pithos.Interfaces
8
{
9
    /// <summary>
10
    /// Converts a <see cref="DateTime"/> to and from a JavaScript date constructor (e.g. new Date(52231943)).
11
    /// </summary>
12
    public class PithosDateTimeConverter : DateTimeConverterBase
13
    {
14
        private DateTime _epoch = new DateTime(1970, 1, 1,0,0,0,DateTimeKind.Utc);
15

    
16
        /// <summary>
17
        /// Writes the JSON representation of the object.
18
        /// </summary>
19
        /// <param name="writer">The <see cref="JsonWriter"/> to write to.</param>
20
        /// <param name="value">The value.</param>
21
        /// <param name="serializer">The calling serializer.</param>
22
        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
23
        {
24
            double seconds;
25

    
26
            if (value is DateTime)
27
            {
28
                var dateTime = (DateTime)value;
29
                var utcDateTime = dateTime.ToUniversalTime();
30
                seconds = (utcDateTime - _epoch).TotalSeconds; ;
31
            }
32
            else if (value is DateTimeOffset)
33
            {
34
                var dateTimeOffset = (DateTimeOffset)value;
35
                var utcDateTimeOffset = dateTimeOffset.ToUniversalTime();
36
                seconds= (utcDateTimeOffset.UtcDateTime - _epoch).TotalSeconds;
37
            }
38
            else
39
            {
40
                throw new Exception("Expected date object value.");
41
            }
42

    
43
            writer.WriteValue(seconds);
44
        }
45

    
46
        /// <summary>
47
        /// Reads the JSON representation of the object.
48
        /// </summary>
49
        /// <param name="reader">The <see cref="JsonReader"/> to read from.</param>
50
        /// <param name="objectType">Type of the object.</param>
51
        /// <param name="existingValue">The existing property value of the JSON that is being converted.</param>
52
        /// <param name="serializer">The calling serializer.</param>
53
        /// <returns>The object value.</returns>
54
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
55
        {
56
            Type t = (IsNullableType(objectType))
57
                         ? Nullable.GetUnderlyingType(objectType)
58
                         : objectType;
59

    
60
            if (reader.TokenType == JsonToken.Null)
61
            {
62
                if (!IsNullableType(objectType))
63
                    throw new Exception(String.Format("Cannot convert null value to {0}.",objectType));
64

    
65
                return null;
66
            }
67

    
68
            var seconds = double.Parse((string)reader.Value,CultureInfo.InvariantCulture);
69

    
70
            DateTime d =  _epoch.AddSeconds(seconds);
71

    
72

    
73
            if (t == typeof(DateTimeOffset))
74
                return new DateTimeOffset(d);
75
            return d;
76
        }
77

    
78
        public static bool IsNullableType(Type t)
79
        {
80
            if (t== null)
81
                throw new ArgumentNullException("t");
82

    
83
            return (t.IsGenericType && t.GetGenericTypeDefinition() == typeof(Nullable<>));
84
        }
85
    }
86
}