2 using System.Globalization;
3 using Newtonsoft.Json.Utilities;
5 namespace Newtonsoft.Json.Converters
8 /// Converts a <see cref="DateTime"/> to and from the ISO 8601 date format (e.g. 2008-04-12T12:53Z).
10 public class IsoDateTimeConverter : DateTimeConverterBase
12 private const string DefaultDateTimeFormat = "yyyy'-'MM'-'dd'T'HH':'mm':'ss.FFFFFFFK";
14 private DateTimeStyles _dateTimeStyles = DateTimeStyles.RoundtripKind;
15 private string _dateTimeFormat;
16 private CultureInfo _culture;
19 /// Gets or sets the date time styles used when converting a date to and from JSON.
21 /// <value>The date time styles used when converting a date to and from JSON.</value>
22 public DateTimeStyles DateTimeStyles
24 get { return _dateTimeStyles; }
25 set { _dateTimeStyles = value; }
29 /// Gets or sets the date time format used when converting a date to and from JSON.
31 /// <value>The date time format used when converting a date to and from JSON.</value>
32 public string DateTimeFormat
34 get { return _dateTimeFormat ?? string.Empty; }
35 set { _dateTimeFormat = StringUtils.NullEmptyString(value); }
39 /// Gets or sets the culture used when converting a date to and from JSON.
41 /// <value>The culture used when converting a date to and from JSON.</value>
42 public CultureInfo Culture
44 get { return _culture ?? CultureInfo.CurrentCulture; }
45 set { _culture = value; }
49 /// Writes the JSON representation of the object.
51 /// <param name="writer">The <see cref="JsonWriter"/> to write to.</param>
52 /// <param name="value">The value.</param>
53 /// <param name="serializer">The calling serializer.</param>
54 public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
58 if (value is DateTime)
60 DateTime dateTime = (DateTime)value;
62 if ((_dateTimeStyles & DateTimeStyles.AdjustToUniversal) == DateTimeStyles.AdjustToUniversal
63 || (_dateTimeStyles & DateTimeStyles.AssumeUniversal) == DateTimeStyles.AssumeUniversal)
64 dateTime = dateTime.ToUniversalTime();
66 text = dateTime.ToString(_dateTimeFormat ?? DefaultDateTimeFormat, Culture);
68 #if !PocketPC && !NET20
69 else if (value is DateTimeOffset)
71 DateTimeOffset dateTimeOffset = (DateTimeOffset)value;
72 if ((_dateTimeStyles & DateTimeStyles.AdjustToUniversal) == DateTimeStyles.AdjustToUniversal
73 || (_dateTimeStyles & DateTimeStyles.AssumeUniversal) == DateTimeStyles.AssumeUniversal)
74 dateTimeOffset = dateTimeOffset.ToUniversalTime();
76 text = dateTimeOffset.ToString(_dateTimeFormat ?? DefaultDateTimeFormat, Culture);
81 throw new Exception("Unexpected value when converting date. Expected DateTime or DateTimeOffset, got {0}.".FormatWith(CultureInfo.InvariantCulture, ReflectionUtils.GetObjectType(value)));
84 writer.WriteValue(text);
88 /// Reads the JSON representation of the object.
90 /// <param name="reader">The <see cref="JsonReader"/> to read from.</param>
91 /// <param name="objectType">Type of the object.</param>
92 /// <param name="existingValue">The existing value of object being read.</param>
93 /// <param name="serializer">The calling serializer.</param>
94 /// <returns>The object value.</returns>
95 public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
97 bool nullable = ReflectionUtils.IsNullableType(objectType);
99 ? Nullable.GetUnderlyingType(objectType)
102 if (reader.TokenType == JsonToken.Null)
104 if (!ReflectionUtils.IsNullableType(objectType))
105 throw new Exception("Cannot convert null value to {0}.".FormatWith(CultureInfo.InvariantCulture, objectType));
110 if (reader.TokenType != JsonToken.String)
111 throw new Exception("Unexpected token parsing date. Expected String, got {0}.".FormatWith(CultureInfo.InvariantCulture, reader.TokenType));
113 string dateText = reader.Value.ToString();
115 if (string.IsNullOrEmpty(dateText) && nullable)
118 #if !PocketPC && !NET20
119 if (t == typeof(DateTimeOffset))
121 if (!string.IsNullOrEmpty(_dateTimeFormat))
122 return DateTimeOffset.ParseExact(dateText, _dateTimeFormat, Culture, _dateTimeStyles);
124 return DateTimeOffset.Parse(dateText, Culture, _dateTimeStyles);
128 if (!string.IsNullOrEmpty(_dateTimeFormat))
129 return DateTime.ParseExact(dateText, _dateTimeFormat, Culture, _dateTimeStyles);
131 return DateTime.Parse(dateText, Culture, _dateTimeStyles);