-
Notifications
You must be signed in to change notification settings - Fork 0
/
IntToDateConverter.cs
63 lines (59 loc) · 1.82 KB
/
IntToDateConverter.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace Proxy
{
/// <summary>
/// Converts from a number (a date returned from ArcGIS REST API) to a <see cref="DateTime"/>.
/// </summary>
public class IntToDateConverter: JsonConverter
{
/// <summary>
/// Returns false.
/// </summary>
public override bool CanWrite { get { return false; } }
/// <summary>
/// Returns true if <paramref name="objectType"/> is <see cref="DateTime"/> type.
/// </summary>
/// <param name="objectType">Type of object.</param>
/// <returns></returns>
public override bool CanConvert(Type objectType)
{
return objectType == typeof(DateTime);
}
/// <summary>
/// Reads a date integer from JSON into a date object.
/// </summary>
/// <param name="reader"></param>
/// <param name="objectType"></param>
/// <param name="existingValue"></param>
/// <param name="serializer"></param>
/// <returns></returns>
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
if (reader.TokenType == JsonToken.Integer)
{
double val = Convert.ToDouble(reader.Value);
DateTime output = new DateTime(1970, 1, 1).AddMilliseconds(val);
return output;
}
else
{
throw new NotSupportedException("Only a token type of JsonToken.Integer is supported.");
}
}
/// <summary>
/// This method is not implemented for this class.
/// </summary>
/// <param name="writer"></param>
/// <param name="value"></param>
/// <param name="serializer"></param>
/// <exception cref="NotImplementedException">Thrown if this method is called.</exception>
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
throw new NotImplementedException();
}
}
}