-Changed JSON Date constructors to automatically deserialize to a date

This commit is contained in:
JamesNK
2008-10-27 07:57:26 +00:00
parent 08b4e4001e
commit a858bebc93
4 changed files with 71 additions and 2 deletions
+24 -1
View File
@@ -333,7 +333,12 @@ namespace Newtonsoft.Json
WriteStartArray();
break;
case JsonToken.StartConstructor:
WriteStartConstructor(reader.Value.ToString());
string constructorName = reader.Value.ToString();
// write a JValue date when the constructor is for a date
if (string.Compare(constructorName, "Date", StringComparison.Ordinal) == 0)
WriteConstructorDate(reader);
else
WriteStartConstructor(reader.Value.ToString());
break;
case JsonToken.PropertyName:
WritePropertyName(reader.Value.ToString());
@@ -384,6 +389,24 @@ namespace Newtonsoft.Json
&& reader.Read());
}
private void WriteConstructorDate(JsonReader reader)
{
if (!reader.Read())
throw new Exception("Unexpected end while reading date constructor.");
if (reader.TokenType != JsonToken.Integer)
throw new Exception("Unexpected token while reading date constructor. Expected Integer, got " + reader.TokenType);
long ticks = (long)reader.Value;
DateTime date = JavaScriptConvert.ConvertJavaScriptTicksToDateTime(ticks);
if (!reader.Read())
throw new Exception("Unexpected end while reading date constructor.");
if (reader.TokenType != JsonToken.EndConstructor)
throw new Exception("Unexpected token while reading date constructor. Expected EndConstructor, got " + reader.TokenType);
WriteValue(date);
}
private bool IsEndToken(JsonToken token)
{
switch (token)