-Changed serializer to better work with ICollection<T> when deserializing

-Improved serializer caching of static type data
This commit is contained in:
JamesNK
2008-09-08 08:46:23 +00:00
parent 7b0f3a7ccc
commit d193ae37be
11 changed files with 896 additions and 25 deletions
@@ -0,0 +1,182 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Data;
using System.Diagnostics;
using Newtonsoft.Json;
using System.IO;
using System.Web.Script.Serialization;
using NUnit.Framework;
namespace Newtonsoft.Json.Tests
{
/// <summary>
/// Summary description for JsonSerializerTest
/// </summary>
public class PerformanceTests : TestFixtureBase
{
private DateTime BaseDate = DateTime.Parse("01/01/2000");
//JSONSerializer ser = null;
public string SerializeJsonNet(object value)
{
Type type = value.GetType();
Newtonsoft.Json.JsonSerializer json = new Newtonsoft.Json.JsonSerializer();
json.NullValueHandling = NullValueHandling.Ignore;
json.ObjectCreationHandling = Newtonsoft.Json.ObjectCreationHandling.Replace;
json.MissingMemberHandling = Newtonsoft.Json.MissingMemberHandling.Ignore;
json.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
StringWriter sw = new StringWriter();
Newtonsoft.Json.JsonTextWriter writer = new JsonTextWriter(sw);
if (false)
writer.Formatting = Formatting.Indented;
else
writer.Formatting = Formatting.None;
writer.QuoteChar = '"';
json.Serialize(writer, value);
string output = sw.ToString();
writer.Close();
return output;
}
public string SerializeWebExtensions(object value)
{
JavaScriptSerializer ser = new JavaScriptSerializer();
List<JavaScriptConverter> converters = new List<JavaScriptConverter>();
return ser.Serialize(value);
}
[Test]
public void PerformanceTest2()
{
TestClass test = new TestClass();
test.Address1.Address = "fff Street";
test.Address1.Entered = DateTime.Now.AddDays(20);
test.BigNumber = 34123123123.121M;
test.Now = DateTime.Now.AddHours(1);
test.strings = new List<string>() { "Rick", "Markus egger ][, (2nd)", "Kevin McNeish" };
cAddress address = new cAddress();
address.Entered = DateTime.Now.AddDays(-1);
address.Address = "array address";
test.Addresses.Add(address);
address = new cAddress();
address.Entered = DateTime.Now.AddDays(-2);
address.Address = "array 2 address";
test.Addresses.Add(address);
Stopwatch timed = new Stopwatch();
timed.Start();
string json = null;
for (int x = 0; x < 10000; x++)
{
json = this.SerializeJsonNet(test);
//json = this.SerializeWebExtensions(test);
}
timed.Stop();
Console.WriteLine("{0}", timed.ElapsedMilliseconds + " ms");
Console.WriteLine("{0}", json);
}
}
public class TestClass
{
public string Name
{
get { return _Name; }
set { _Name = value; }
}
private string _Name = "Rick";
public DateTime Now
{
get { return _Now; }
set { _Now = value; }
}
private DateTime _Now = DateTime.Now;
public decimal BigNumber
{
get { return _BigNumber; }
set { _BigNumber = value; }
}
private decimal _BigNumber = 1212121.22M;
public cAddress Address1
{
get { return _Address1; }
set { _Address1 = value; }
}
private cAddress _Address1 = new cAddress();
public List<cAddress> Addresses
{
get { return _Addresses; }
set { _Addresses = value; }
}
private List<cAddress> _Addresses = new List<cAddress>();
public List<string> strings = new List<string>() { "Rick", "Markus", "Kevin" };
public Dictionary<string, int> dictionary = new Dictionary<string, int> { { "Val asd1", 1 }, { "Val2", 3 }, { "Val3", 4 } };
}
public class cAddress
{
public string Address
{
get { return _Address; }
set { _Address = value; }
}
private string _Address = "32 Kaiea";
public string Phone
{
get { return _Phone; }
set { _Phone = value; }
}
private string _Phone = "(503) 814-6335";
public DateTime Entered
{
get { return _Entered; }
set { _Entered = value; }
}
private DateTime _Entered = DateTime.Parse("01/01/2007", CultureInfo.CurrentCulture.DateTimeFormat);
}
}