/// <summary> /// Writes a <see cref="DateTime"/> value. /// </summary> /// <param name="value">The <see cref="DateTime"/> value to write.</param> public override void WriteValue(DateTime value) { InternalWriteValue(JsonToken.Date); value = JsonConvert.EnsureDateTime(value, DateTimeZoneHandling); _writer.Write(_quoteChar); JsonConvert.WriteDateTimeString(_writer, value, DateFormatHandling, DateFormatString, Culture); _writer.Write(_quoteChar); }
public static string ToString(DateTime value, DateFormatHandling format, DateTimeZoneHandling timeZoneHandling) { DateTime d = JsonConvert.EnsureDateTime(value, timeZoneHandling); using (StringWriter stringWriter = StringUtils.CreateStringWriter(64)) { JsonConvert.WriteDateTimeString((TextWriter)stringWriter, d, DateTimeUtils.GetUtcOffset(d), d.Kind, format); return(stringWriter.ToString()); } }
internal DateTime?ReadAsDateTimeInternal() { _readType = ReadType.ReadAsDateTime; do { if (!ReadInternal()) { SetToken(JsonToken.None); return(null); } } while (TokenType == JsonToken.Comment); if (TokenType == JsonToken.Date) { return((DateTime)Value); } if (TokenType == JsonToken.Null) { return(null); } DateTime dt; if (TokenType == JsonToken.String) { string s = (string)Value; if (string.IsNullOrEmpty(s)) { SetToken(JsonToken.Null); return(null); } if (DateTime.TryParse(s, Culture, DateTimeStyles.RoundtripKind, out dt)) { dt = JsonConvert.EnsureDateTime(dt, DateTimeZoneHandling); SetToken(JsonToken.Date, dt); return(dt); } else { throw CreateReaderException(this, "Could not convert string to DateTime: {0}.".FormatWith(CultureInfo.InvariantCulture, Value)); } } if (TokenType == JsonToken.EndArray) { return(null); } throw CreateReaderException(this, "Error reading date. Unexpected token: {0}.".FormatWith(CultureInfo.InvariantCulture, TokenType)); }
internal DateTime?ReadAsDateTimeInternal() { this._readType = ReadType.ReadAsDateTime; while (this.ReadInternal()) { if (this.TokenType != JsonToken.Comment) { if (this.TokenType == JsonToken.Date) { return(new DateTime?((DateTime)this.Value)); } if (this.TokenType == JsonToken.Null) { return(new DateTime?()); } if (this.TokenType == JsonToken.String) { string s = (string)this.Value; if (string.IsNullOrEmpty(s)) { this.SetToken(JsonToken.Null); return(new DateTime?()); } else { DateTime result; if (!DateTime.TryParse(s, (IFormatProvider)this.Culture, DateTimeStyles.RoundtripKind, out result)) { throw JsonReaderException.Create(this, StringUtils.FormatWith("Could not convert string to DateTime: {0}.", (IFormatProvider)CultureInfo.InvariantCulture, this.Value)); } DateTime dateTime = JsonConvert.EnsureDateTime(result, this.DateTimeZoneHandling); this.SetToken(JsonToken.Date, (object)dateTime); return(new DateTime?(dateTime)); } } else if (this.TokenType == JsonToken.EndArray) { return(new DateTime?()); } else { throw JsonReaderException.Create(this, StringUtils.FormatWith("Error reading date. Unexpected token: {0}.", (IFormatProvider)CultureInfo.InvariantCulture, (object)this.TokenType)); } } } this.SetToken(JsonToken.None); return(new DateTime?()); }
public override void WriteValue(DateTime value) { base.WriteValue(value); value = JsonConvert.EnsureDateTime(value, this.DateTimeZoneHandling); JsonConvert.WriteDateTimeString(this._writer, value, this.DateFormatHandling); }