2
// Copyright (c) 2007 James Newton-King
4
// Permission is hereby granted, free of charge, to any person
5
// obtaining a copy of this software and associated documentation
6
// files (the "Software"), to deal in the Software without
7
// restriction, including without limitation the rights to use,
8
// copy, modify, merge, publish, distribute, sublicense, and/or sell
9
// copies of the Software, and to permit persons to whom the
10
// Software is furnished to do so, subject to the following
13
// The above copyright notice and this permission notice shall be
14
// included in all copies or substantial portions of the Software.
16
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
18
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23
// OTHER DEALINGS IN THE SOFTWARE.
27
using System.Globalization;
28
using Newtonsoft.Json.Utilities;
30
namespace Newtonsoft.Json.Converters
33
/// Converts a <see cref="DateTime"/> to and from the ISO 8601 date format (e.g. 2008-04-12T12:53Z).
35
public class IsoDateTimeConverter : DateTimeConverterBase
37
private const string DefaultDateTimeFormat = "yyyy'-'MM'-'dd'T'HH':'mm':'ss.FFFFFFFK";
39
private DateTimeStyles _dateTimeStyles = DateTimeStyles.RoundtripKind;
40
private string _dateTimeFormat;
41
private CultureInfo _culture;
44
/// Gets or sets the date time styles used when converting a date to and from JSON.
46
/// <value>The date time styles used when converting a date to and from JSON.</value>
47
public DateTimeStyles DateTimeStyles
49
get { return _dateTimeStyles; }
50
set { _dateTimeStyles = value; }
54
/// Gets or sets the date time format used when converting a date to and from JSON.
56
/// <value>The date time format used when converting a date to and from JSON.</value>
57
public string DateTimeFormat
59
get { return _dateTimeFormat ?? string.Empty; }
60
set { _dateTimeFormat = StringUtils.NullEmptyString(value); }
64
/// Gets or sets the culture used when converting a date to and from JSON.
66
/// <value>The culture used when converting a date to and from JSON.</value>
67
public CultureInfo Culture
69
get { return _culture ?? CultureInfo.CurrentCulture; }
70
set { _culture = value; }
74
/// Writes the JSON representation of the object.
76
/// <param name="writer">The <see cref="JsonWriter"/> to write to.</param>
77
/// <param name="value">The value.</param>
78
/// <param name="serializer">The calling serializer.</param>
79
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
83
if (value is DateTime)
85
DateTime dateTime = (DateTime)value;
87
if ((_dateTimeStyles & DateTimeStyles.AdjustToUniversal) == DateTimeStyles.AdjustToUniversal
88
|| (_dateTimeStyles & DateTimeStyles.AssumeUniversal) == DateTimeStyles.AssumeUniversal)
89
dateTime = dateTime.ToUniversalTime();
91
text = dateTime.ToString(_dateTimeFormat ?? DefaultDateTimeFormat, Culture);
93
#if !PocketPC && !NET20
94
else if (value is DateTimeOffset)
96
DateTimeOffset dateTimeOffset = (DateTimeOffset)value;
97
if ((_dateTimeStyles & DateTimeStyles.AdjustToUniversal) == DateTimeStyles.AdjustToUniversal
98
|| (_dateTimeStyles & DateTimeStyles.AssumeUniversal) == DateTimeStyles.AssumeUniversal)
99
dateTimeOffset = dateTimeOffset.ToUniversalTime();
101
text = dateTimeOffset.ToString(_dateTimeFormat ?? DefaultDateTimeFormat, Culture);
106
throw new JsonSerializationException("Unexpected value when converting date. Expected DateTime or DateTimeOffset, got {0}.".FormatWith(CultureInfo.InvariantCulture, ReflectionUtils.GetObjectType(value)));
109
writer.WriteValue(text);
113
/// Reads the JSON representation of the object.
115
/// <param name="reader">The <see cref="JsonReader"/> to read from.</param>
116
/// <param name="objectType">Type of the object.</param>
117
/// <param name="existingValue">The existing value of object being read.</param>
118
/// <param name="serializer">The calling serializer.</param>
119
/// <returns>The object value.</returns>
120
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
122
bool nullable = ReflectionUtils.IsNullableType(objectType);
124
? Nullable.GetUnderlyingType(objectType)
127
if (reader.TokenType == JsonToken.Null)
129
if (!ReflectionUtils.IsNullableType(objectType))
130
throw JsonSerializationException.Create(reader, "Cannot convert null value to {0}.".FormatWith(CultureInfo.InvariantCulture, objectType));
135
if (reader.TokenType == JsonToken.Date)
137
#if !PocketPC && !NET20
138
if (t == typeof(DateTimeOffset))
139
return new DateTimeOffset((DateTime)reader.Value);
145
if (reader.TokenType != JsonToken.String)
146
throw JsonSerializationException.Create(reader, "Unexpected token parsing date. Expected String, got {0}.".FormatWith(CultureInfo.InvariantCulture, reader.TokenType));
148
string dateText = reader.Value.ToString();
150
if (string.IsNullOrEmpty(dateText) && nullable)
153
#if !PocketPC && !NET20
154
if (t == typeof(DateTimeOffset))
156
if (!string.IsNullOrEmpty(_dateTimeFormat))
157
return DateTimeOffset.ParseExact(dateText, _dateTimeFormat, Culture, _dateTimeStyles);
159
return DateTimeOffset.Parse(dateText, Culture, _dateTimeStyles);
163
if (!string.IsNullOrEmpty(_dateTimeFormat))
164
return DateTime.ParseExact(dateText, _dateTimeFormat, Culture, _dateTimeStyles);
166
return DateTime.Parse(dateText, Culture, _dateTimeStyles);
b'\\ No newline at end of file'