-Added IConvertible support to JValue
-Changed IDictionary<DateTime, object> to be serialized in ISO date format -Fixed DataContractAttribute to not be inherited
This commit is contained in:
@@ -137,21 +137,21 @@ namespace Newtonsoft.Json
|
||||
|
||||
if (_dateParseHandling != DateParseHandling.None)
|
||||
{
|
||||
if (text.Length > 0)
|
||||
DateParseHandling dateParseHandling;
|
||||
if (_readType == ReadType.ReadAsDateTime)
|
||||
dateParseHandling = DateParseHandling.DateTime;
|
||||
#if !NET20
|
||||
else if (_readType == ReadType.ReadAsDateTimeOffset)
|
||||
dateParseHandling = DateParseHandling.DateTimeOffset;
|
||||
#endif
|
||||
else
|
||||
dateParseHandling = _dateParseHandling;
|
||||
|
||||
object dt;
|
||||
if (JsonConvert.TryParseDateTime(text, dateParseHandling, DateTimeZoneHandling, out dt))
|
||||
{
|
||||
if (text[0] == '/')
|
||||
{
|
||||
if (text.StartsWith("/Date(", StringComparison.Ordinal) && text.EndsWith(")/", StringComparison.Ordinal))
|
||||
{
|
||||
ParseDateMicrosoft(text);
|
||||
return;
|
||||
}
|
||||
}
|
||||
else if (char.IsDigit(text[0]) && text.Length >= 19 && text.Length <= 40)
|
||||
{
|
||||
if (ParseDateIso(text))
|
||||
return;
|
||||
}
|
||||
SetToken(JsonToken.Date, dt);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -160,88 +160,6 @@ namespace Newtonsoft.Json
|
||||
}
|
||||
}
|
||||
|
||||
private bool ParseDateIso(string text)
|
||||
{
|
||||
const string isoDateFormat = "yyyy-MM-ddTHH:mm:ss.FFFFFFFK";
|
||||
|
||||
#if !NET20
|
||||
if (_readType == ReadType.ReadAsDateTimeOffset || (_readType == ReadType.Read && _dateParseHandling == DateParseHandling.DateTimeOffset))
|
||||
{
|
||||
DateTimeOffset dateTimeOffset;
|
||||
if (DateTimeOffset.TryParseExact(text, isoDateFormat, CultureInfo.InvariantCulture, DateTimeStyles.RoundtripKind, out dateTimeOffset))
|
||||
{
|
||||
SetToken(JsonToken.Date, dateTimeOffset);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
else
|
||||
#endif
|
||||
{
|
||||
DateTime dateTime;
|
||||
if (DateTime.TryParseExact(text, isoDateFormat, CultureInfo.InvariantCulture, DateTimeStyles.RoundtripKind, out dateTime))
|
||||
{
|
||||
dateTime = JsonConvert.EnsureDateTime(dateTime, DateTimeZoneHandling);
|
||||
|
||||
SetToken(JsonToken.Date, dateTime);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private void ParseDateMicrosoft(string text)
|
||||
{
|
||||
string value = text.Substring(6, text.Length - 8);
|
||||
DateTimeKind kind = DateTimeKind.Utc;
|
||||
|
||||
int index = value.IndexOf('+', 1);
|
||||
|
||||
if (index == -1)
|
||||
index = value.IndexOf('-', 1);
|
||||
|
||||
TimeSpan offset = TimeSpan.Zero;
|
||||
|
||||
if (index != -1)
|
||||
{
|
||||
kind = DateTimeKind.Local;
|
||||
offset = ReadOffset(value.Substring(index));
|
||||
value = value.Substring(0, index);
|
||||
}
|
||||
|
||||
long javaScriptTicks = long.Parse(value, NumberStyles.Integer, CultureInfo.InvariantCulture);
|
||||
|
||||
DateTime utcDateTime = JsonConvert.ConvertJavaScriptTicksToDateTime(javaScriptTicks);
|
||||
|
||||
#if !NET20
|
||||
if (_readType == ReadType.ReadAsDateTimeOffset || (_readType == ReadType.Read && _dateParseHandling == DateParseHandling.DateTimeOffset))
|
||||
{
|
||||
SetToken(JsonToken.Date, new DateTimeOffset(utcDateTime.Add(offset).Ticks, offset));
|
||||
}
|
||||
else
|
||||
#endif
|
||||
{
|
||||
DateTime dateTime;
|
||||
|
||||
switch (kind)
|
||||
{
|
||||
case DateTimeKind.Unspecified:
|
||||
dateTime = DateTime.SpecifyKind(utcDateTime.ToLocalTime(), DateTimeKind.Unspecified);
|
||||
break;
|
||||
case DateTimeKind.Local:
|
||||
dateTime = utcDateTime.ToLocalTime();
|
||||
break;
|
||||
default:
|
||||
dateTime = utcDateTime;
|
||||
break;
|
||||
}
|
||||
|
||||
dateTime = JsonConvert.EnsureDateTime(dateTime, DateTimeZoneHandling);
|
||||
|
||||
SetToken(JsonToken.Date, dateTime);
|
||||
}
|
||||
}
|
||||
|
||||
private static void BlockCopyChars(char[] src, int srcOffset, char[] dst, int dstOffset, int count)
|
||||
{
|
||||
const int charByteCount = 2;
|
||||
@@ -368,22 +286,6 @@ namespace Newtonsoft.Json
|
||||
return true;
|
||||
}
|
||||
|
||||
private static TimeSpan ReadOffset(string offsetText)
|
||||
{
|
||||
bool negative = (offsetText[0] == '-');
|
||||
|
||||
int hours = int.Parse(offsetText.Substring(1, 2), NumberStyles.Integer, CultureInfo.InvariantCulture);
|
||||
int minutes = 0;
|
||||
if (offsetText.Length >= 5)
|
||||
minutes = int.Parse(offsetText.Substring(3, 2), NumberStyles.Integer, CultureInfo.InvariantCulture);
|
||||
|
||||
TimeSpan offset = TimeSpan.FromHours(hours) + TimeSpan.FromMinutes(minutes);
|
||||
if (negative)
|
||||
offset = offset.Negate();
|
||||
|
||||
return offset;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reads the next JSON token from the stream.
|
||||
/// </summary>
|
||||
|
||||
Reference in New Issue
Block a user