From a858bebc9393a5d1ac06ed43ac6bab077d34e949 Mon Sep 17 00:00:00 2001 From: JamesNK Date: Mon, 27 Oct 2008 07:57:26 +0000 Subject: [PATCH] -Changed JSON Date constructors to automatically deserialize to a date --- .../JsonSerializerTest.cs | 33 +++++++++++++++++++ .../Linq/JObjectTests.cs | 13 ++++++++ Src/Newtonsoft.Json.Tests/PerformanceTests.cs | 2 +- Src/Newtonsoft.Json/JsonWriter.cs | 25 +++++++++++++- 4 files changed, 71 insertions(+), 2 deletions(-) diff --git a/Src/Newtonsoft.Json.Tests/JsonSerializerTest.cs b/Src/Newtonsoft.Json.Tests/JsonSerializerTest.cs index 5e036b13..ae81d4b3 100644 --- a/Src/Newtonsoft.Json.Tests/JsonSerializerTest.cs +++ b/Src/Newtonsoft.Json.Tests/JsonSerializerTest.cs @@ -1324,6 +1324,39 @@ keyword such as type of business."" Assert.AreEqual(@"{}", json); } + public class Invoice + { + public string Company { get; set; } + public decimal Amount { get; set; } + [DefaultValue(false)] + public bool Paid { get; set; } + [DefaultValue(null)] + public DateTime? PaidDate { get; set; } + } + + [Test] + public void SerializeInvoice() + { + Invoice invoice = new Invoice + { + Company = "Acme Ltd.", + Amount = 50.0m, + Paid = false + }; + + string json = JavaScriptConvert.SerializeObject(invoice); + + Console.WriteLine(json); + // {"Company":"Acme Ltd.","Amount":50.0,"Paid":false,"PaidDate":null} + + json = JavaScriptConvert.SerializeObject(invoice, + Formatting.None, + new JsonSerializerSettings { DefaultValueHandling = DefaultValueHandling.Ignore }); + + Console.WriteLine(json); + // {"Company":"Acme Ltd.","Amount":50.0} + } + [Test] public void DeserializeNullable() { diff --git a/Src/Newtonsoft.Json.Tests/Linq/JObjectTests.cs b/Src/Newtonsoft.Json.Tests/Linq/JObjectTests.cs index 9cfab0f8..c87bf5c0 100644 --- a/Src/Newtonsoft.Json.Tests/Linq/JObjectTests.cs +++ b/Src/Newtonsoft.Json.Tests/Linq/JObjectTests.cs @@ -4,6 +4,8 @@ using System.Linq; using System.Text; using NUnit.Framework; using Newtonsoft.Json.Linq; +using Newtonsoft.Json.Converters; +using System.IO; namespace Newtonsoft.Json.Tests.Linq { @@ -317,5 +319,16 @@ Parameter name: arrayIndex")] string json = @"[""prop""]"; JObject.Parse(json); } + + [Test] + public void ParseJavaScriptDate() + { + string json = @"[new Date(1207285200000)]"; + + JArray a = (JArray)JavaScriptConvert.DeserializeObject(json, null); + JValue v = (JValue)a[0]; + + Assert.AreEqual(JavaScriptConvert.ConvertJavaScriptTicksToDateTime(1207285200000), (DateTime)v); + } } } diff --git a/Src/Newtonsoft.Json.Tests/PerformanceTests.cs b/Src/Newtonsoft.Json.Tests/PerformanceTests.cs index 2f117300..dbc7c1ea 100644 --- a/Src/Newtonsoft.Json.Tests/PerformanceTests.cs +++ b/Src/Newtonsoft.Json.Tests/PerformanceTests.cs @@ -20,7 +20,7 @@ namespace Newtonsoft.Json.Tests /// public class PerformanceTests : TestFixtureBase { - private int Iterations = 100; + private int Iterations = 5000; public enum SerializeMethod { diff --git a/Src/Newtonsoft.Json/JsonWriter.cs b/Src/Newtonsoft.Json/JsonWriter.cs index 7ebaf012..471ebaf2 100644 --- a/Src/Newtonsoft.Json/JsonWriter.cs +++ b/Src/Newtonsoft.Json/JsonWriter.cs @@ -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)