~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to external/Newtonsoft.Json/Src/Newtonsoft.Json/Converters/IsoDateTimeConverter.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
Import upstream version 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#region License
 
2
// Copyright (c) 2007 James Newton-King
 
3
//
 
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
 
11
// conditions:
 
12
//
 
13
// The above copyright notice and this permission notice shall be
 
14
// included in all copies or substantial portions of the Software.
 
15
//
 
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.
 
24
#endregion
 
25
 
 
26
using System;
 
27
using System.Globalization;
 
28
using Newtonsoft.Json.Utilities;
 
29
 
 
30
namespace Newtonsoft.Json.Converters
 
31
{
 
32
  /// <summary>
 
33
  /// Converts a <see cref="DateTime"/> to and from the ISO 8601 date format (e.g. 2008-04-12T12:53Z).
 
34
  /// </summary>
 
35
  public class IsoDateTimeConverter : DateTimeConverterBase
 
36
  {
 
37
    private const string DefaultDateTimeFormat = "yyyy'-'MM'-'dd'T'HH':'mm':'ss.FFFFFFFK";
 
38
 
 
39
    private DateTimeStyles _dateTimeStyles = DateTimeStyles.RoundtripKind;
 
40
    private string _dateTimeFormat;
 
41
    private CultureInfo _culture;
 
42
 
 
43
    /// <summary>
 
44
    /// Gets or sets the date time styles used when converting a date to and from JSON.
 
45
    /// </summary>
 
46
    /// <value>The date time styles used when converting a date to and from JSON.</value>
 
47
    public DateTimeStyles DateTimeStyles
 
48
    {
 
49
      get { return _dateTimeStyles; }
 
50
      set { _dateTimeStyles = value; }
 
51
    }
 
52
 
 
53
    /// <summary>
 
54
    /// Gets or sets the date time format used when converting a date to and from JSON.
 
55
    /// </summary>
 
56
    /// <value>The date time format used when converting a date to and from JSON.</value>
 
57
    public string DateTimeFormat
 
58
    {
 
59
      get { return _dateTimeFormat ?? string.Empty; }
 
60
      set { _dateTimeFormat = StringUtils.NullEmptyString(value); }
 
61
    }
 
62
 
 
63
    /// <summary>
 
64
    /// Gets or sets the culture used when converting a date to and from JSON.
 
65
    /// </summary>
 
66
    /// <value>The culture used when converting a date to and from JSON.</value>
 
67
    public CultureInfo Culture
 
68
    {
 
69
      get { return _culture ?? CultureInfo.CurrentCulture; }
 
70
      set { _culture = value; }
 
71
    }
 
72
 
 
73
    /// <summary>
 
74
    /// Writes the JSON representation of the object.
 
75
    /// </summary>
 
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)
 
80
    {
 
81
      string text;
 
82
 
 
83
      if (value is DateTime)
 
84
      {
 
85
        DateTime dateTime = (DateTime)value;
 
86
 
 
87
        if ((_dateTimeStyles & DateTimeStyles.AdjustToUniversal) == DateTimeStyles.AdjustToUniversal
 
88
          || (_dateTimeStyles & DateTimeStyles.AssumeUniversal) == DateTimeStyles.AssumeUniversal)
 
89
          dateTime = dateTime.ToUniversalTime();
 
90
 
 
91
        text = dateTime.ToString(_dateTimeFormat ?? DefaultDateTimeFormat, Culture);
 
92
      }
 
93
#if !PocketPC && !NET20
 
94
      else if (value is DateTimeOffset)
 
95
      {
 
96
        DateTimeOffset dateTimeOffset = (DateTimeOffset)value;
 
97
        if ((_dateTimeStyles & DateTimeStyles.AdjustToUniversal) == DateTimeStyles.AdjustToUniversal
 
98
          || (_dateTimeStyles & DateTimeStyles.AssumeUniversal) == DateTimeStyles.AssumeUniversal)
 
99
          dateTimeOffset = dateTimeOffset.ToUniversalTime();
 
100
 
 
101
        text = dateTimeOffset.ToString(_dateTimeFormat ?? DefaultDateTimeFormat, Culture);
 
102
      }
 
103
#endif
 
104
      else
 
105
      {
 
106
        throw new JsonSerializationException("Unexpected value when converting date. Expected DateTime or DateTimeOffset, got {0}.".FormatWith(CultureInfo.InvariantCulture, ReflectionUtils.GetObjectType(value)));
 
107
      }
 
108
 
 
109
      writer.WriteValue(text);
 
110
    }
 
111
 
 
112
    /// <summary>
 
113
    /// Reads the JSON representation of the object.
 
114
    /// </summary>
 
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)
 
121
    {
 
122
      bool nullable = ReflectionUtils.IsNullableType(objectType);
 
123
      Type t = (nullable)
 
124
        ? Nullable.GetUnderlyingType(objectType)
 
125
        : objectType;
 
126
 
 
127
      if (reader.TokenType == JsonToken.Null)
 
128
      {
 
129
        if (!ReflectionUtils.IsNullableType(objectType))
 
130
          throw JsonSerializationException.Create(reader, "Cannot convert null value to {0}.".FormatWith(CultureInfo.InvariantCulture, objectType));
 
131
 
 
132
        return null;
 
133
      }
 
134
 
 
135
      if (reader.TokenType == JsonToken.Date)
 
136
      {
 
137
#if !PocketPC && !NET20
 
138
        if (t == typeof(DateTimeOffset))
 
139
          return new DateTimeOffset((DateTime)reader.Value);
 
140
#endif
 
141
 
 
142
        return reader.Value;
 
143
      }
 
144
 
 
145
      if (reader.TokenType != JsonToken.String)
 
146
        throw JsonSerializationException.Create(reader, "Unexpected token parsing date. Expected String, got {0}.".FormatWith(CultureInfo.InvariantCulture, reader.TokenType));
 
147
 
 
148
      string dateText = reader.Value.ToString();
 
149
 
 
150
      if (string.IsNullOrEmpty(dateText) && nullable)
 
151
        return null;
 
152
 
 
153
#if !PocketPC && !NET20
 
154
      if (t == typeof(DateTimeOffset))
 
155
      {
 
156
        if (!string.IsNullOrEmpty(_dateTimeFormat))
 
157
          return DateTimeOffset.ParseExact(dateText, _dateTimeFormat, Culture, _dateTimeStyles);
 
158
        else
 
159
          return DateTimeOffset.Parse(dateText, Culture, _dateTimeStyles);
 
160
      }
 
161
#endif
 
162
 
 
163
      if (!string.IsNullOrEmpty(_dateTimeFormat))
 
164
        return DateTime.ParseExact(dateText, _dateTimeFormat, Culture, _dateTimeStyles);
 
165
      else
 
166
        return DateTime.Parse(dateText, Culture, _dateTimeStyles);
 
167
    }
 
168
  }
 
169
}
 
 
b'\\ No newline at end of file'