From 70dabfb409ace66e26ad79c0f3a270bb2fb3a9e1 Mon Sep 17 00:00:00 2001 From: wybb Date: Tue, 11 Sep 2018 10:25:06 +0800 Subject: [PATCH] Update JsonWriter.cs --- Src/Newtonsoft.Json/JsonWriter.cs | 34 +++++++++++++++++-------------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/Src/Newtonsoft.Json/JsonWriter.cs b/Src/Newtonsoft.Json/JsonWriter.cs index 57b1203d..b79ffda8 100644 --- a/Src/Newtonsoft.Json/JsonWriter.cs +++ b/Src/Newtonsoft.Json/JsonWriter.cs @@ -696,27 +696,31 @@ namespace Newtonsoft.Json private void WriteConstructorDate(JsonReader reader) { - if (!reader.Read()) + List DateParams = new List(); + while (true) { - throw JsonWriterException.Create(this, "Unexpected end when reading date constructor.", null); + if (!reader.Read()) + throw JsonWriterException.Create(this, "Unexpected end when reading date constructor.", null); + if (reader.TokenType == JsonToken.EndConstructor) + break; + if (reader.TokenType != JsonToken.Integer) + throw JsonWriterException.Create(this, "Unexpected token when reading date constructor. Expected Integer, got " + reader.TokenType, null); + DateParams.Add((long)reader.Value); + if (DateParams.Count > 7) + throw JsonWriterException.Create(this, "Unexpected token when reading date constructor. Wrong number of arguments", null); } - if (reader.TokenType != JsonToken.Integer) + DateTime date; + if (DateParams.Count == 1) { - throw JsonWriterException.Create(this, "Unexpected token when reading date constructor. Expected Integer, got " + reader.TokenType, null); + date = DateTimeUtils.ConvertJavaScriptTicksToDateTime(DateParams[0]); } - - long ticks = (long)reader.Value; - DateTime date = DateTimeUtils.ConvertJavaScriptTicksToDateTime(ticks); - - if (!reader.Read()) + else if (DateParams.Count == 7) //Add support: new Date(2018,5,26,11,14,31,36) { - throw JsonWriterException.Create(this, "Unexpected end when reading date constructor.", null); + date = new DateTime((int)DateParams[0], (int)DateParams[1] + 1, (int)DateParams[2], (int)DateParams[3], + (int)DateParams[4], (int)DateParams[5], (int)DateParams[6]); } - if (reader.TokenType != JsonToken.EndConstructor) - { - throw JsonWriterException.Create(this, "Unexpected token when reading date constructor. Expected EndConstructor, got " + reader.TokenType, null); - } - + else + throw JsonWriterException.Create(this, "Unexpected token when reading date constructor. Wrong number of arguments", null); WriteValue(date); }