-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
@@ -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()
{
@@ -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);
}
}
}
@@ -20,7 +20,7 @@ namespace Newtonsoft.Json.Tests
/// </summary>
public class PerformanceTests : TestFixtureBase
{
private int Iterations = 100;
private int Iterations = 5000;
public enum SerializeMethod
{