PageRenderTime 68ms CodeModel.GetById 3ms app.highlight 55ms RepoModel.GetById 1ms app.codeStats 1ms

/Src/Newtonsoft.Json.Tests/PerformanceTests.cs

https://github.com/99strong99/Newtonsoft.Json
C# | 1156 lines | 1086 code | 45 blank | 25 comment | 9 complexity | c6857ca285d0e5cc918af12540eea04c MD5 | raw file
   1#region License
   2// Copyright (c) 2007 James Newton-King
   3//
   4// Permission is hereby granted, free of charge, to any person
   5// obtaining a copy of this software and associated documentation
   6// files (the "Software"), to deal in the Software without
   7// restriction, including without limitation the rights to use,
   8// copy, modify, merge, publish, distribute, sublicense, and/or sell
   9// copies of the Software, and to permit persons to whom the
  10// Software is furnished to do so, subject to the following
  11// conditions:
  12//
  13// The above copyright notice and this permission notice shall be
  14// included in all copies or substantial portions of the Software.
  15//
  16// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  17// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  18// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  19// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  20// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  21// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  22// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  23// OTHER DEALINGS IN THE SOFTWARE.
  24#endregion
  25
  26#if !(NET20 || NET35 || NETFX_CORE || PORTABLE)
  27using System;
  28using System.Collections;
  29using System.Collections.Generic;
  30using System.Globalization;
  31using System.Diagnostics;
  32using System.IO;
  33using System.Linq;
  34using System.Runtime.Serialization;
  35using System.Web.Script.Serialization;
  36using System.Xml.Linq;
  37using Newtonsoft.Json.Utilities;
  38using NUnit.Framework;
  39using System.Runtime.Serialization.Json;
  40using System.Text;
  41using Newtonsoft.Json.Bson;
  42using System.Runtime.Serialization.Formatters.Binary;
  43using Newtonsoft.Json.Linq;
  44using Newtonsoft.Json.Converters;
  45
  46namespace Newtonsoft.Json.Tests
  47{
  48    [Serializable]
  49    [DataContract]
  50    public class Image
  51    {
  52        [DataMember]
  53        public string FileName { get; set; }
  54
  55        [DataMember]
  56        public string Author { get; set; }
  57
  58        [DataMember]
  59        public string Caption { get; set; }
  60
  61        [DataMember]
  62        public byte[] Data { get; set; }
  63    }
  64
  65    [TestFixture]
  66    public class PerformanceTests : TestFixtureBase
  67    {
  68        private const int Iterations = 100;
  69        //private const int Iterations = 5000;
  70
  71        #region Data
  72        private const string BsonHex =
  73            @"A9-01-00-00-04-73-74-72-69-6E-67-73-00-2B-00-00-00-0A-30-00-02-31-00-19-00-00-00-4D-61-72-6B-75-73-20-65-67-67-65-72-20-5D-3E-3C-5B-2C-20-28-32-6E-64-29-00-0A-32-00-00-03-64-69-63-74-69-6F-6E-61-72-79-00-37-00-00-00-10-56-61-6C-20-26-20-61-73-64-31-00-01-00-00-00-10-56-61-6C-32-20-26-20-61-73-64-31-00-03-00-00-00-10-56-61-6C-33-20-26-20-61-73-64-31-00-04-00-00-00-00-02-4E-61-6D-65-00-05-00-00-00-52-69-63-6B-00-09-4E-6F-77-00-EF-BD-69-EC-25-01-00-00-01-42-69-67-4E-75-6D-62-65-72-00-E7-7B-CC-26-96-C7-1F-42-03-41-64-64-72-65-73-73-31-00-47-00-00-00-02-53-74-72-65-65-74-00-0B-00-00-00-66-66-66-20-53-74-72-65-65-74-00-02-50-68-6F-6E-65-00-0F-00-00-00-28-35-30-33-29-20-38-31-34-2D-36-33-33-35-00-09-45-6E-74-65-72-65-64-00-6F-FF-31-53-26-01-00-00-00-04-41-64-64-72-65-73-73-65-73-00-A2-00-00-00-03-30-00-4B-00-00-00-02-53-74-72-65-65-74-00-0F-00-00-00-1F-61-72-72-61-79-3C-61-64-64-72-65-73-73-00-02-50-68-6F-6E-65-00-0F-00-00-00-28-35-30-33-29-20-38-31-34-2D-36-33-33-35-00-09-45-6E-74-65-72-65-64-00-6F-73-0C-E7-25-01-00-00-00-03-31-00-4C-00-00-00-02-53-74-72-65-65-74-00-10-00-00-00-61-72-72-61-79-20-32-20-61-64-64-72-65-73-73-00-02-50-68-6F-6E-65-00-0F-00-00-00-28-35-30-33-29-20-38-31-34-2D-36-33-33-35-00-09-45-6E-74-65-72-65-64-00-6F-17-E6-E1-25-01-00-00-00-00-00";
  74
  75        private const string BinaryFormatterHex =
  76            @"00-01-00-00-00-FF-FF-FF-FF-01-00-00-00-00-00-00-00-0C-02-00-00-00-4C-4E-65-77-74-6F-6E-73-6F-66-74-2E-4A-73-6F-6E-2E-54-65-73-74-73-2C-20-56-65-72-73-69-6F-6E-3D-33-2E-35-2E-30-2E-30-2C-20-43-75-6C-74-75-72-65-3D-6E-65-75-74-72-61-6C-2C-20-50-75-62-6C-69-63-4B-65-79-54-6F-6B-65-6E-3D-6E-75-6C-6C-05-01-00-00-00-1F-4E-65-77-74-6F-6E-73-6F-66-74-2E-4A-73-6F-6E-2E-54-65-73-74-73-2E-54-65-73-74-43-6C-61-73-73-07-00-00-00-05-5F-4E-61-6D-65-04-5F-4E-6F-77-0A-5F-42-69-67-4E-75-6D-62-65-72-09-5F-41-64-64-72-65-73-73-31-0A-5F-41-64-64-72-65-73-73-65-73-07-73-74-72-69-6E-67-73-0A-64-69-63-74-69-6F-6E-61-72-79-01-00-00-04-03-03-03-0D-05-1D-4E-65-77-74-6F-6E-73-6F-66-74-2E-4A-73-6F-6E-2E-54-65-73-74-73-2E-41-64-64-72-65-73-73-02-00-00-00-90-01-53-79-73-74-65-6D-2E-43-6F-6C-6C-65-63-74-69-6F-6E-73-2E-47-65-6E-65-72-69-63-2E-4C-69-73-74-60-31-5B-5B-4E-65-77-74-6F-6E-73-6F-66-74-2E-4A-73-6F-6E-2E-54-65-73-74-73-2E-41-64-64-72-65-73-73-2C-20-4E-65-77-74-6F-6E-73-6F-66-74-2E-4A-73-6F-6E-2E-54-65-73-74-73-2C-20-56-65-72-73-69-6F-6E-3D-33-2E-35-2E-30-2E-30-2C-20-43-75-6C-74-75-72-65-3D-6E-65-75-74-72-61-6C-2C-20-50-75-62-6C-69-63-4B-65-79-54-6F-6B-65-6E-3D-6E-75-6C-6C-5D-5D-7F-53-79-73-74-65-6D-2E-43-6F-6C-6C-65-63-74-69-6F-6E-73-2E-47-65-6E-65-72-69-63-2E-4C-69-73-74-60-31-5B-5B-53-79-73-74-65-6D-2E-53-74-72-69-6E-67-2C-20-6D-73-63-6F-72-6C-69-62-2C-20-56-65-72-73-69-6F-6E-3D-32-2E-30-2E-30-2E-30-2C-20-43-75-6C-74-75-72-65-3D-6E-65-75-74-72-61-6C-2C-20-50-75-62-6C-69-63-4B-65-79-54-6F-6B-65-6E-3D-62-37-37-61-35-63-35-36-31-39-33-34-65-30-38-39-5D-5D-E1-01-53-79-73-74-65-6D-2E-43-6F-6C-6C-65-63-74-69-6F-6E-73-2E-47-65-6E-65-72-69-63-2E-44-69-63-74-69-6F-6E-61-72-79-60-32-5B-5B-53-79-73-74-65-6D-2E-53-74-72-69-6E-67-2C-20-6D-73-63-6F-72-6C-69-62-2C-20-56-65-72-73-69-6F-6E-3D-32-2E-30-2E-30-2E-30-2C-20-43-75-6C-74-75-72-65-3D-6E-65-75-74-72-61-6C-2C-20-50-75-62-6C-69-63-4B-65-79-54-6F-6B-65-6E-3D-62-37-37-61-35-63-35-36-31-39-33-34-65-30-38-39-5D-2C-5B-53-79-73-74-65-6D-2E-49-6E-74-33-32-2C-20-6D-73-63-6F-72-6C-69-62-2C-20-56-65-72-73-69-6F-6E-3D-32-2E-30-2E-30-2E-30-2C-20-43-75-6C-74-75-72-65-3D-6E-65-75-74-72-61-6C-2C-20-50-75-62-6C-69-63-4B-65-79-54-6F-6B-65-6E-3D-62-37-37-61-35-63-35-36-31-39-33-34-65-30-38-39-5D-5D-02-00-00-00-06-03-00-00-00-04-52-69-63-6B-B6-25-3A-D1-C5-59-CC-88-0F-33-34-31-32-33-31-32-33-31-32-33-2E-31-32-31-09-04-00-00-00-09-05-00-00-00-09-06-00-00-00-09-07-00-00-00-05-04-00-00-00-1D-4E-65-77-74-6F-6E-73-6F-66-74-2E-4A-73-6F-6E-2E-54-65-73-74-73-2E-41-64-64-72-65-73-73-03-00-00-00-07-5F-73-74-72-65-65-74-06-5F-50-68-6F-6E-65-08-5F-45-6E-74-65-72-65-64-01-01-00-0D-02-00-00-00-06-08-00-00-00-0A-66-66-66-20-53-74-72-65-65-74-06-09-00-00-00-0E-28-35-30-33-29-20-38-31-34-2D-36-33-33-35-B6-BD-B8-BF-74-69-CC-88-04-05-00-00-00-90-01-53-79-73-74-65-6D-2E-43-6F-6C-6C-65-63-74-69-6F-6E-73-2E-47-65-6E-65-72-69-63-2E-4C-69-73-74-60-31-5B-5B-4E-65-77-74-6F-6E-73-6F-66-74-2E-4A-73-6F-6E-2E-54-65-73-74-73-2E-41-64-64-72-65-73-73-2C-20-4E-65-77-74-6F-6E-73-6F-66-74-2E-4A-73-6F-6E-2E-54-65-73-74-73-2C-20-56-65-72-73-69-6F-6E-3D-33-2E-35-2E-30-2E-30-2C-20-43-75-6C-74-75-72-65-3D-6E-65-75-74-72-61-6C-2C-20-50-75-62-6C-69-63-4B-65-79-54-6F-6B-65-6E-3D-6E-75-6C-6C-5D-5D-03-00-00-00-06-5F-69-74-65-6D-73-05-5F-73-69-7A-65-08-5F-76-65-72-73-69-6F-6E-04-00-00-1F-4E-65-77-74-6F-6E-73-6F-66-74-2E-4A-73-6F-6E-2E-54-65-73-74-73-2E-41-64-64-72-65-73-73-5B-5D-02-00-00-00-08-08-09-0A-00-00-00-02-00-00-00-02-00-00-00-04-06-00-00-00-7F-53-79-73-74-65-6D-2E-43-6F-6C-6C-65-63-74-69-6F-6E-73-2E-47-65-6E-65-72-69-63-2E-4C-69-73-74-60-31-5B-5B-53-79-73-74-65-6D-2E-53-74-72-69-6E-67-2C-20-6D-73-63-6F-72-6C-69-62-2C-20-56-65-72-73-69-6F-6E-3D-32-2E-30-2E-30-2E-30-2C-20-43-75-6C-74-75-72-65-3D-6E-65-75-74-72-61-6C-2C-20-50-75-62-6C-69-63-4B-65-79-54-6F-6B-65-6E-3D-62-37-37-61-35-63-35-36-31-39-33-34-65-30-38-39-5D-5D-03-00-00-00-06-5F-69-74-65-6D-73-05-5F-73-69-7A-65-08-5F-76-65-72-73-69-6F-6E-06-00-00-08-08-09-0B-00-00-00-03-00-00-00-03-00-00-00-04-07-00-00-00-E1-01-53-79-73-74-65-6D-2E-43-6F-6C-6C-65-63-74-69-6F-6E-73-2E-47-65-6E-65-72-69-63-2E-44-69-63-74-69-6F-6E-61-72-79-60-32-5B-5B-53-79-73-74-65-6D-2E-53-74-72-69-6E-67-2C-20-6D-73-63-6F-72-6C-69-62-2C-20-56-65-72-73-69-6F-6E-3D-32-2E-30-2E-30-2E-30-2C-20-43-75-6C-74-75-72-65-3D-6E-65-75-74-72-61-6C-2C-20-50-75-62-6C-69-63-4B-65-79-54-6F-6B-65-6E-3D-62-37-37-61-35-63-35-36-31-39-33-34-65-30-38-39-5D-2C-5B-53-79-73-74-65-6D-2E-49-6E-74-33-32-2C-20-6D-73-63-6F-72-6C-69-62-2C-20-56-65-72-73-69-6F-6E-3D-32-2E-30-2E-30-2E-30-2C-20-43-75-6C-74-75-72-65-3D-6E-65-75-74-72-61-6C-2C-20-50-75-62-6C-69-63-4B-65-79-54-6F-6B-65-6E-3D-62-37-37-61-35-63-35-36-31-39-33-34-65-30-38-39-5D-5D-04-00-00-00-07-56-65-72-73-69-6F-6E-08-43-6F-6D-70-61-72-65-72-08-48-61-73-68-53-69-7A-65-0D-4B-65-79-56-61-6C-75-65-50-61-69-72-73-00-03-00-03-08-92-01-53-79-73-74-65-6D-2E-43-6F-6C-6C-65-63-74-69-6F-6E-73-2E-47-65-6E-65-72-69-63-2E-47-65-6E-65-72-69-63-45-71-75-61-6C-69-74-79-43-6F-6D-70-61-72-65-72-60-31-5B-5B-53-79-73-74-65-6D-2E-53-74-72-69-6E-67-2C-20-6D-73-63-6F-72-6C-69-62-2C-20-56-65-72-73-69-6F-6E-3D-32-2E-30-2E-30-2E-30-2C-20-43-75-6C-74-75-72-65-3D-6E-65-75-74-72-61-6C-2C-20-50-75-62-6C-69-63-4B-65-79-54-6F-6B-65-6E-3D-62-37-37-61-35-63-35-36-31-39-33-34-65-30-38-39-5D-5D-08-E5-01-53-79-73-74-65-6D-2E-43-6F-6C-6C-65-63-74-69-6F-6E-73-2E-47-65-6E-65-72-69-63-2E-4B-65-79-56-61-6C-75-65-50-61-69-72-60-32-5B-5B-53-79-73-74-65-6D-2E-53-74-72-69-6E-67-2C-20-6D-73-63-6F-72-6C-69-62-2C-20-56-65-72-73-69-6F-6E-3D-32-2E-30-2E-30-2E-30-2C-20-43-75-6C-74-75-72-65-3D-6E-65-75-74-72-61-6C-2C-20-50-75-62-6C-69-63-4B-65-79-54-6F-6B-65-6E-3D-62-37-37-61-35-63-35-36-31-39-33-34-65-30-38-39-5D-2C-5B-53-79-73-74-65-6D-2E-49-6E-74-33-32-2C-20-6D-73-63-6F-72-6C-69-62-2C-20-56-65-72-73-69-6F-6E-3D-32-2E-30-2E-30-2E-30-2C-20-43-75-6C-74-75-72-65-3D-6E-65-75-74-72-61-6C-2C-20-50-75-62-6C-69-63-4B-65-79-54-6F-6B-65-6E-3D-62-37-37-61-35-63-35-36-31-39-33-34-65-30-38-39-5D-5D-5B-5D-03-00-00-00-09-0C-00-00-00-03-00-00-00-09-0D-00-00-00-07-0A-00-00-00-00-01-00-00-00-04-00-00-00-04-1D-4E-65-77-74-6F-6E-73-6F-66-74-2E-4A-73-6F-6E-2E-54-65-73-74-73-2E-41-64-64-72-65-73-73-02-00-00-00-09-0E-00-00-00-09-0F-00-00-00-0D-02-11-0B-00-00-00-04-00-00-00-0A-06-10-00-00-00-18-4D-61-72-6B-75-73-20-65-67-67-65-72-20-5D-3E-3C-5B-2C-20-28-32-6E-64-29-0D-02-04-0C-00-00-00-92-01-53-79-73-74-65-6D-2E-43-6F-6C-6C-65-63-74-69-6F-6E-73-2E-47-65-6E-65-72-69-63-2E-47-65-6E-65-72-69-63-45-71-75-61-6C-69-74-79-43-6F-6D-70-61-72-65-72-60-31-5B-5B-53-79-73-74-65-6D-2E-53-74-72-69-6E-67-2C-20-6D-73-63-6F-72-6C-69-62-2C-20-56-65-72-73-69-6F-6E-3D-32-2E-30-2E-30-2E-30-2C-20-43-75-6C-74-75-72-65-3D-6E-65-75-74-72-61-6C-2C-20-50-75-62-6C-69-63-4B-65-79-54-6F-6B-65-6E-3D-62-37-37-61-35-63-35-36-31-39-33-34-65-30-38-39-5D-5D-00-00-00-00-07-0D-00-00-00-00-01-00-00-00-03-00-00-00-03-E3-01-53-79-73-74-65-6D-2E-43-6F-6C-6C-65-63-74-69-6F-6E-73-2E-47-65-6E-65-72-69-63-2E-4B-65-79-56-61-6C-75-65-50-61-69-72-60-32-5B-5B-53-79-73-74-65-6D-2E-53-74-72-69-6E-67-2C-20-6D-73-63-6F-72-6C-69-62-2C-20-56-65-72-73-69-6F-6E-3D-32-2E-30-2E-30-2E-30-2C-20-43-75-6C-74-75-72-65-3D-6E-65-75-74-72-61-6C-2C-20-50-75-62-6C-69-63-4B-65-79-54-6F-6B-65-6E-3D-62-37-37-61-35-63-35-36-31-39-33-34-65-30-38-39-5D-2C-5B-53-79-73-74-65-6D-2E-49-6E-74-33-32-2C-20-6D-73-63-6F-72-6C-69-62-2C-20-56-65-72-73-69-6F-6E-3D-32-2E-30-2E-30-2E-30-2C-20-43-75-6C-74-75-72-65-3D-6E-65-75-74-72-61-6C-2C-20-50-75-62-6C-69-63-4B-65-79-54-6F-6B-65-6E-3D-62-37-37-61-35-63-35-36-31-39-33-34-65-30-38-39-5D-5D-04-EF-FF-FF-FF-E3-01-53-79-73-74-65-6D-2E-43-6F-6C-6C-65-63-74-69-6F-6E-73-2E-47-65-6E-65-72-69-63-2E-4B-65-79-56-61-6C-75-65-50-61-69-72-60-32-5B-5B-53-79-73-74-65-6D-2E-53-74-72-69-6E-67-2C-20-6D-73-63-6F-72-6C-69-62-2C-20-56-65-72-73-69-6F-6E-3D-32-2E-30-2E-30-2E-30-2C-20-43-75-6C-74-75-72-65-3D-6E-65-75-74-72-61-6C-2C-20-50-75-62-6C-69-63-4B-65-79-54-6F-6B-65-6E-3D-62-37-37-61-35-63-35-36-31-39-33-34-65-30-38-39-5D-2C-5B-53-79-73-74-65-6D-2E-49-6E-74-33-32-2C-20-6D-73-63-6F-72-6C-69-62-2C-20-56-65-72-73-69-6F-6E-3D-32-2E-30-2E-30-2E-30-2C-20-43-75-6C-74-75-72-65-3D-6E-65-75-74-72-61-6C-2C-20-50-75-62-6C-69-63-4B-65-79-54-6F-6B-65-6E-3D-62-37-37-61-35-63-35-36-31-39-33-34-65-30-38-39-5D-5D-02-00-00-00-03-6B-65-79-05-76-61-6C-75-65-01-00-08-06-12-00-00-00-0A-56-61-6C-20-26-20-61-73-64-31-01-00-00-00-01-ED-FF-FF-FF-EF-FF-FF-FF-06-14-00-00-00-0B-56-61-6C-32-20-26-20-61-73-64-31-03-00-00-00-01-EB-FF-FF-FF-EF-FF-FF-FF-06-16-00-00-00-0B-56-61-6C-33-20-26-20-61-73-64-31-04-00-00-00-01-0E-00-00-00-04-00-00-00-06-17-00-00-00-0E-1F-61-72-72-61-79-3C-61-64-64-72-65-73-73-09-09-00-00-00-B6-FD-0B-45-F4-58-CC-88-01-0F-00-00-00-04-00-00-00-06-19-00-00-00-0F-61-72-72-61-79-20-32-20-61-64-64-72-65-73-73-09-09-00-00-00-B6-3D-A2-1A-2B-58-CC-88-0B";
  77
  78        private const string XmlText =
  79            @"<TestClass xmlns=""http://schemas.datacontract.org/2004/07/Newtonsoft.Json.Tests"" xmlns:i=""http://www.w3.org/2001/XMLSchema-instance""><Address1><Entered>2010-01-21T11:12:16.0809174+13:00</Entered><Phone>(503) 814-6335</Phone><Street>fff Street</Street></Address1><Addresses><Address><Entered>2009-12-31T11:12:16.0809174+13:00</Entered><Phone>(503) 814-6335</Phone><Street>&#x1F;array&lt;address</Street></Address><Address><Entered>2009-12-30T11:12:16.0809174+13:00</Entered><Phone>(503) 814-6335</Phone><Street>array 2 address</Street></Address></Addresses><BigNumber>34123123123.121</BigNumber><Name>Rick</Name><Now>2010-01-01T12:12:16.0809174+13:00</Now><dictionary xmlns:a=""http://schemas.microsoft.com/2003/10/Serialization/Arrays""><a:KeyValueOfstringint><a:Key>Val &amp; asd1</a:Key><a:Value>1</a:Value></a:KeyValueOfstringint><a:KeyValueOfstringint><a:Key>Val2 &amp; asd1</a:Key><a:Value>3</a:Value></a:KeyValueOfstringint><a:KeyValueOfstringint><a:Key>Val3 &amp; asd1</a:Key><a:Value>4</a:Value></a:KeyValueOfstringint></dictionary><strings xmlns:a=""http://schemas.microsoft.com/2003/10/Serialization/Arrays""><a:string i:nil=""true""/><a:string>Markus egger ]&gt;&lt;[, (2nd)</a:string><a:string i:nil=""true""/></strings></TestClass>";
  80
  81        private const string JsonText =
  82            @"{""strings"":[null,""Markus egger ]><[, (2nd)"",null],""dictionary"":{""Val & asd1"":1,""Val2 & asd1"":3,""Val3 & asd1"":4},""Name"":""Rick"",""Now"":""\/Date(1262301136080+1300)\/"",""BigNumber"":34123123123.121,""Address1"":{""Street"":""fff Street"",""Phone"":""(503) 814-6335"",""Entered"":""\/Date(1264025536080+1300)\/""},""Addresses"":[{""Street"":""\u001farray<address"",""Phone"":""(503) 814-6335"",""Entered"":""\/Date(1262211136080+1300)\/""},{""Street"":""array 2 address"",""Phone"":""(503) 814-6335"",""Entered"":""\/Date(1262124736080+1300)\/""}]}";
  83
  84        private const string JsonIsoText =
  85            @"{""strings"":[null,""Markus egger ]><[, (2nd)"",null],""dictionary"":{""Val & asd1"":1,""Val2 & asd1"":3,""Val3 & asd1"":4},""Name"":""Rick"",""Now"":""2012-02-25T19:55:50.6095676+13:00"",""BigNumber"":34123123123.121,""Address1"":{""Street"":""fff Street"",""Phone"":""(503) 814-6335"",""Entered"":""2012-02-24T18:55:50.6095676+13:00""},""Addresses"":[{""Street"":""\u001farray<address"",""Phone"":""(503) 814-6335"",""Entered"":""2012-02-24T18:55:50.6095676+13:00""},{""Street"":""array 2 address"",""Phone"":""(503) 814-6335"",""Entered"":""2012-02-24T18:55:50.6095676+13:00""}]}";
  86
  87        private const string SimpleJsonText =
  88            @"{""Id"":2311,""Name"":""Simple-1"",""Address"":""Planet Earth"",""Scores"":[82,96,49,40,38,38,78,96,2,39]}";
  89
  90        public enum SerializeMethod
  91        {
  92            JsonNet,
  93            JsonNetWithIsoConverter,
  94            JsonNetBinary,
  95            JsonNetLinq,
  96            JsonNetManual,
  97            BinaryFormatter,
  98            JavaScriptSerializer,
  99            DataContractSerializer,
 100            DataContractJsonSerializer
 101        }
 102        #endregion
 103
 104        [Test]
 105        public void SerializeSimpleObject()
 106        {
 107            SimpleObject value = CreateSimpleObject();
 108
 109            SerializeTests(value);
 110        }
 111
 112        [Test]
 113        public void SerializeAnonymous()
 114        {
 115            var helloWorld = new { message = "Hello, World!" };
 116
 117            BenchmarkSerializeMethod(SerializeMethod.JsonNet, helloWorld);
 118        }
 119
 120        [Test]
 121        public void DeserializeSimpleObject()
 122        {
 123            DeserializeTests<SimpleObject>(SimpleJsonText);
 124        }
 125
 126        [Test]
 127        public void Serialize()
 128        {
 129            TestClass test = CreateSerializationObject();
 130
 131            SerializeTests(test);
 132        }
 133
 134        private void SerializeTests(object value)
 135        {
 136            BenchmarkSerializeMethod(SerializeMethod.DataContractSerializer, value);
 137            BenchmarkSerializeMethod(SerializeMethod.BinaryFormatter, value);
 138            BenchmarkSerializeMethod(SerializeMethod.JavaScriptSerializer, value);
 139            BenchmarkSerializeMethod(SerializeMethod.DataContractJsonSerializer, value);
 140            BenchmarkSerializeMethod(SerializeMethod.JsonNet, value);
 141            BenchmarkSerializeMethod(SerializeMethod.JsonNetLinq, value);
 142            BenchmarkSerializeMethod(SerializeMethod.JsonNetManual, value);
 143            BenchmarkSerializeMethod(SerializeMethod.JsonNetWithIsoConverter, value);
 144            BenchmarkSerializeMethod(SerializeMethod.JsonNetBinary, value);
 145        }
 146
 147        [Test]
 148        public void Deserialize()
 149        {
 150            BenchmarkDeserializeMethod<TestClass>(SerializeMethod.DataContractSerializer, XmlText);
 151            BenchmarkDeserializeMethod<TestClass>(SerializeMethod.BinaryFormatter, HexToBytes(BinaryFormatterHex));
 152            DeserializeTests<TestClass>(JsonText);
 153            BenchmarkDeserializeMethod<TestClass>(SerializeMethod.JsonNetWithIsoConverter, JsonIsoText);
 154            BenchmarkDeserializeMethod<TestClass>(SerializeMethod.JsonNetBinary, HexToBytes(BsonHex));
 155        }
 156
 157        public void DeserializeTests<T>(string json)
 158        {
 159            BenchmarkDeserializeMethod<T>(SerializeMethod.JavaScriptSerializer, json);
 160            BenchmarkDeserializeMethod<T>(SerializeMethod.DataContractJsonSerializer, json);
 161            BenchmarkDeserializeMethod<T>(SerializeMethod.JsonNet, json);
 162            BenchmarkDeserializeMethod<T>(SerializeMethod.JsonNetManual, json);
 163        }
 164
 165        [Test]
 166        public void SerializeSizeNormal()
 167        {
 168            SerializeSize(CreateSerializationObject());
 169        }
 170
 171        [Test]
 172        public void SerializeSizeData()
 173        {
 174            Image image = new Image();
 175            image.Data = System.IO.File.ReadAllBytes(@"bunny_pancake.jpg");
 176            image.FileName = "bunny_pancake.jpg";
 177            image.Author = "Hironori Akutagawa";
 178            image.Caption = "I have no idea what you are talking about so here's a bunny with a pancake on its head";
 179
 180            SerializeSize(image);
 181        }
 182
 183        private T TimeOperation<T>(Func<T> operation, string name)
 184        {
 185            // warm up
 186            operation();
 187
 188            Stopwatch timed = new Stopwatch();
 189            timed.Start();
 190
 191            T result = operation();
 192
 193            Console.WriteLine(name);
 194            Console.WriteLine("{0} ms", timed.ElapsedMilliseconds);
 195
 196            timed.Stop();
 197
 198            return result;
 199        }
 200
 201        [Test]
 202        public void BuildJObject()
 203        {
 204            JObject o = new JObject();
 205            for (int i = 0; i < 50; i++)
 206            {
 207                o[i.ToString()] = i;
 208            }
 209            string jsonText = o.ToString();
 210
 211            // this is extremely slow with 5000 interations
 212            int interations = 1000;
 213
 214            TimeOperation(() =>
 215            {
 216                JObject oo = null;
 217                for (int i = 0; i < interations; i++)
 218                {
 219                    oo = JObject.Parse(jsonText);
 220                }
 221
 222                return oo;
 223            }, "JObject");
 224        }
 225
 226        [Test]
 227        public void BuildJObjectComparedToXml()
 228        {
 229            const long totalIterations = 100000;
 230
 231            const String xml =
 232                @"<?xml  version=""1.0"" encoding=""ISO-8859-1""?>
 233                <root>
 234                    <property name=""Property1"">1</property>
 235                    <property name=""Property2"">2</property>
 236                    <property name=""Property3"">3</property>
 237                    <property name=""Property4"">4</property>
 238                    <property name=""Property5"">5</property>
 239                </root>";
 240
 241            const String json =
 242                @"{
 243                    ""Property1"":""1"",
 244                    ""Property2"":""2"",
 245                    ""Property3"":""3"",
 246                    ""Property4"":""4"",
 247                    ""Property5"":""5""
 248                }";
 249
 250
 251            var watch = new Stopwatch();
 252            watch.Start();
 253            for (long iteration = 0; iteration < totalIterations; ++iteration)
 254            {
 255                var obj = JObject.Parse(json);
 256                obj["Property1"].Value<Int32>();
 257                obj["Property2"].Value<Int32>();
 258                obj["Property3"].Value<Int32>();
 259                obj["Property4"].Value<Int32>();
 260                obj["Property5"].Value<Int32>();
 261            }
 262            watch.Stop();
 263            var performance1 = (totalIterations / watch.ElapsedMilliseconds) * 1000;
 264            Console.WriteLine("JSON: " + watch.Elapsed.TotalSeconds);
 265
 266            watch.Reset();
 267            watch.Start();
 268            for (long iteration = 0; iteration < totalIterations; ++iteration)
 269            {
 270                var doc = XDocument.Parse(xml);
 271                var alarmProperties = doc.Descendants("property");
 272                foreach (var property in alarmProperties)
 273                {
 274                    var attr = property.Attribute("name");
 275                    var name = attr.Value;
 276                    switch (name)
 277                    {
 278                        case "Property1":
 279                            Int32.Parse(property.Value);
 280                            break;
 281                        case "Property2":
 282                            Int32.Parse(property.Value);
 283                            break;
 284                        case "Property3":
 285                            Int32.Parse(property.Value);
 286                            break;
 287                        case "Property4":
 288                            Int32.Parse(property.Value);
 289                            break;
 290                        case "Property5":
 291                            Int32.Parse(property.Value);
 292                            break;
 293                    }
 294                }
 295            }
 296            watch.Stop();
 297            var performance2 = (totalIterations / watch.ElapsedMilliseconds) * 1000;
 298            Console.WriteLine("XML: " + watch.Elapsed.TotalSeconds);
 299        }
 300
 301        [Test]
 302        public void SerializeString()
 303        {
 304            string text = @"The general form of an HTML element is therefore: <tag attribute1=""value1"" attribute2=""value2"">content</tag>.
 305Some HTML elements are defined as empty elements and take the form <tag attribute1=""value1"" attribute2=""value2"" >.
 306Empty elements may enclose no content, for instance, the BR tag or the inline IMG tag.
 307The name of an HTML element is the name used in the tags.
 308Note that the end tag's name is preceded by a slash character, ""/"", and that in empty elements the end tag is neither required nor allowed.
 309If attributes are not mentioned, default values are used in each case.
 310
 311The general form of an HTML element is therefore: <tag attribute1=""value1"" attribute2=""value2"">content</tag>.
 312Some HTML elements are defined as empty elements and take the form <tag attribute1=""value1"" attribute2=""value2"" >.
 313Empty elements may enclose no content, for instance, the BR tag or the inline IMG tag.
 314The name of an HTML element is the name used in the tags.
 315Note that the end tag's name is preceded by a slash character, ""/"", and that in empty elements the end tag is neither required nor allowed.
 316If attributes are not mentioned, default values are used in each case.
 317
 318The general form of an HTML element is therefore: <tag attribute1=""value1"" attribute2=""value2"">content</tag>.
 319Some HTML elements are defined as empty elements and take the form <tag attribute1=""value1"" attribute2=""value2"" >.
 320Empty elements may enclose no content, for instance, the BR tag or the inline IMG tag.
 321The name of an HTML element is the name used in the tags.
 322Note that the end tag's name is preceded by a slash character, ""/"", and that in empty elements the end tag is neither required nor allowed.
 323If attributes are not mentioned, default values are used in each case.
 324";
 325
 326            int interations = 1000;
 327
 328            TimeOperation(() =>
 329            {
 330                for (int i = 0; i < interations; i++)
 331                {
 332                    using (StringWriter w = StringUtils.CreateStringWriter(StringUtils.GetLength(text) ?? 16))
 333                    {
 334                        char[] buffer = null;
 335                        JavaScriptUtils.WriteEscapedJavaScriptString(w, text, '"', true, JavaScriptUtils.DoubleQuoteCharEscapeFlags, StringEscapeHandling.Default, ref buffer);
 336                    }
 337                }
 338
 339                return "";
 340            }, "New");
 341        }
 342
 343        [Test]
 344        public void JTokenToObject()
 345        {
 346            JValue s = new JValue("String!");
 347
 348            int interations = 1000000;
 349
 350            TimeOperation(() =>
 351            {
 352                for (int i = 0; i < interations; i++)
 353                {
 354                    s.ToObject(typeof(string));
 355                }
 356
 357                return "";
 358            }, "New");
 359
 360            TimeOperation(() =>
 361            {
 362                for (int i = 0; i < interations; i++)
 363                {
 364                    s.ToObject(typeof(string), new JsonSerializer());
 365                }
 366
 367                return "";
 368            }, "Old");
 369
 370            TimeOperation(() =>
 371            {
 372                for (int i = 0; i < interations; i++)
 373                {
 374                    s.Value<string>();
 375                }
 376
 377                return "";
 378            }, "Value");
 379        }
 380
 381        private void SerializeSize(object value)
 382        {
 383            // this is extremely slow with 5000 interations
 384            int interations = 100;
 385
 386            byte[] jsonBytes = TimeOperation(() =>
 387            {
 388                string json = null;
 389                for (int i = 0; i < interations; i++)
 390                {
 391                    json = JsonConvert.SerializeObject(value, Formatting.None);
 392                }
 393
 394                return Encoding.UTF8.GetBytes(json);
 395            }, "Json.NET");
 396
 397            byte[] bsonBytes = TimeOperation(() =>
 398            {
 399                MemoryStream ms = null;
 400                for (int i = 0; i < interations; i++)
 401                {
 402                    ms = new MemoryStream();
 403                    JsonSerializer serializer = new JsonSerializer();
 404                    BsonWriter writer = new BsonWriter(ms);
 405
 406                    serializer.Serialize(writer, value);
 407                    writer.Flush();
 408                }
 409
 410                return ms.ToArray();
 411            }, "Json.NET BSON");
 412
 413            byte[] xmlBytes = TimeOperation(() =>
 414            {
 415                MemoryStream ms = null;
 416                for (int i = 0; i < interations; i++)
 417                {
 418                    ms = new MemoryStream();
 419                    DataContractSerializer dataContractSerializer = new DataContractSerializer(value.GetType());
 420                    dataContractSerializer.WriteObject(ms, value);
 421                }
 422
 423                return ms.ToArray();
 424            }, "DataContractSerializer");
 425
 426            byte[] wcfJsonBytes = TimeOperation(() =>
 427            {
 428                MemoryStream ms = null;
 429                for (int i = 0; i < interations; i++)
 430                {
 431                    ms = new MemoryStream();
 432                    DataContractJsonSerializer dataContractJsonSerializer = new DataContractJsonSerializer(value.GetType());
 433                    dataContractJsonSerializer.WriteObject(ms, value);
 434                }
 435
 436                return ms.ToArray();
 437            }, "DataContractJsonSerializer");
 438
 439            byte[] binaryFormatterBytes = TimeOperation(() =>
 440            {
 441                MemoryStream ms = null;
 442                for (int i = 0; i < interations; i++)
 443                {
 444                    ms = new MemoryStream();
 445                    BinaryFormatter formatter = new BinaryFormatter();
 446                    formatter.Serialize(ms, value);
 447                }
 448
 449                return ms.ToArray();
 450            }, "BinaryFormatter");
 451
 452            Console.WriteLine("Json.NET size: {0} bytes", jsonBytes.Length);
 453            Console.WriteLine("BSON size: {0} bytes", bsonBytes.Length);
 454            Console.WriteLine("WCF JSON size: {0} bytes", wcfJsonBytes.Length);
 455            Console.WriteLine("WCF XML size: {0} bytes", xmlBytes.Length);
 456            Console.WriteLine("BinaryFormatter size: {0} bytes", binaryFormatterBytes.Length);
 457        }
 458
 459        #region Serialize
 460        private static readonly byte[] Buffer = new byte[4096];
 461
 462        public void BenchmarkSerializeMethod(SerializeMethod method, object value)
 463        {
 464            Serialize(method, value);
 465
 466            Stopwatch timed = new Stopwatch();
 467            timed.Start();
 468
 469            string json = null;
 470            for (int x = 0; x < Iterations; x++)
 471            {
 472                json = Serialize(method, value);
 473            }
 474
 475            timed.Stop();
 476
 477            Console.WriteLine("Serialize method: {0}", method);
 478            Console.WriteLine("{0} ms", timed.ElapsedMilliseconds);
 479            Console.WriteLine(json);
 480            Console.WriteLine();
 481        }
 482
 483        private TestClass CreateSerializationObject()
 484        {
 485            TestClass test = new TestClass();
 486
 487            test.dictionary = new Dictionary<string, int> { { "Val & asd1", 1 }, { "Val2 & asd1", 3 }, { "Val3 & asd1", 4 } };
 488
 489
 490            test.Address1.Street = "fff Street";
 491            test.Address1.Entered = DateTime.Now.AddDays(20);
 492
 493            test.BigNumber = 34123123123.121M;
 494            test.Now = DateTime.Now.AddHours(1);
 495            test.strings = new List<string>() { null, "Markus egger ]><[, (2nd)", null };
 496
 497            Address address = new Address();
 498            address.Entered = DateTime.Now.AddDays(-1);
 499            address.Street = "\u001farray\u003caddress";
 500
 501            test.Addresses.Add(address);
 502
 503            address = new Address();
 504            address.Entered = DateTime.Now.AddDays(-2);
 505            address.Street = "array 2 address";
 506            test.Addresses.Add(address);
 507            return test;
 508        }
 509
 510        private static SimpleObject CreateSimpleObject()
 511        {
 512            return new SimpleObject
 513            {
 514                Name = "Simple-1",
 515                Id = 2311,
 516                Address = "Planet Earth",
 517                Scores = new[] { 82, 96, 49, 40, 38, 38, 78, 96, 2, 39 }
 518            };
 519        }
 520
 521        public string SerializeWebExtensions(object value)
 522        {
 523            JavaScriptSerializer ser = new JavaScriptSerializer();
 524
 525            return ser.Serialize(value);
 526        }
 527
 528        public string SerializeDataContractJson(object value)
 529        {
 530            DataContractJsonSerializer dataContractSerializer
 531                = new DataContractJsonSerializer(value.GetType());
 532
 533            MemoryStream ms = new MemoryStream();
 534            dataContractSerializer.WriteObject(ms, value);
 535
 536            ms.Seek(0, SeekOrigin.Begin);
 537
 538            using (StreamReader sr = new StreamReader(ms))
 539            {
 540                return sr.ReadToEnd();
 541            }
 542        }
 543
 544        public string SerializeDataContract(object value)
 545        {
 546            DataContractSerializer dataContractSerializer
 547                = new DataContractSerializer(value.GetType());
 548
 549            MemoryStream ms = new MemoryStream();
 550            dataContractSerializer.WriteObject(ms, value);
 551
 552            ms.Seek(0, SeekOrigin.Begin);
 553
 554            using (StreamReader sr = new StreamReader(ms))
 555            {
 556                return sr.ReadToEnd();
 557            }
 558        }
 559
 560        private string Serialize(SerializeMethod method, object value)
 561        {
 562            string json;
 563
 564            switch (method)
 565            {
 566                case SerializeMethod.JsonNet:
 567                    json = JsonConvert.SerializeObject(value);
 568                    break;
 569                case SerializeMethod.JsonNetWithIsoConverter:
 570                    json = JsonConvert.SerializeObject(value, new IsoDateTimeConverter());
 571                    break;
 572                case SerializeMethod.JsonNetLinq:
 573                {
 574                    TestClass c = value as TestClass;
 575                    if (c != null)
 576                    {
 577                        JObject o = new JObject(
 578                            new JProperty("strings", new JArray(
 579                                c.strings
 580                                )),
 581                            new JProperty("dictionary", new JObject(c.dictionary.Select(d => new JProperty(d.Key, d.Value)))),
 582                            new JProperty("Name", c.Name),
 583                            new JProperty("Now", c.Now),
 584                            new JProperty("BigNumber", c.BigNumber),
 585                            new JProperty("Address1", new JObject(
 586                                new JProperty("Street", c.Address1.Street),
 587                                new JProperty("Phone", c.Address1.Phone),
 588                                new JProperty("Entered", c.Address1.Entered))),
 589                            new JProperty("Addresses", new JArray(c.Addresses.Select(a =>
 590                                new JObject(
 591                                    new JProperty("Street", a.Street),
 592                                    new JProperty("Phone", a.Phone),
 593                                    new JProperty("Entered", a.Entered)))))
 594                            );
 595
 596                        json = o.ToString(Formatting.None);
 597                    }
 598                    else
 599                    {
 600                        json = string.Empty;
 601                    }
 602                    break;
 603                }
 604                case SerializeMethod.JsonNetManual:
 605                {
 606                    TestClass c = value as TestClass;
 607                    if (c != null)
 608                    {
 609                        StringWriter sw = new StringWriter();
 610                        JsonTextWriter writer = new JsonTextWriter(sw);
 611                        writer.WriteStartObject();
 612                        writer.WritePropertyName("strings");
 613                        writer.WriteStartArray();
 614                        foreach (string s in c.strings)
 615                        {
 616                            writer.WriteValue(s);
 617                        }
 618                        writer.WriteEndArray();
 619                        writer.WritePropertyName("dictionary");
 620                        writer.WriteStartObject();
 621                        foreach (KeyValuePair<string, int> keyValuePair in c.dictionary)
 622                        {
 623                            writer.WritePropertyName(keyValuePair.Key);
 624                            writer.WriteValue(keyValuePair.Value);
 625                        }
 626                        writer.WriteEndObject();
 627                        writer.WritePropertyName("Name");
 628                        writer.WriteValue(c.Name);
 629                        writer.WritePropertyName("Now");
 630                        writer.WriteValue(c.Now);
 631                        writer.WritePropertyName("BigNumber");
 632                        writer.WriteValue(c.BigNumber);
 633                        writer.WritePropertyName("Address1");
 634                        writer.WriteStartObject();
 635                        writer.WritePropertyName("Street");
 636                        writer.WriteValue(c.BigNumber);
 637                        writer.WritePropertyName("Street");
 638                        writer.WriteValue(c.BigNumber);
 639                        writer.WritePropertyName("Street");
 640                        writer.WriteValue(c.BigNumber);
 641                        writer.WriteEndObject();
 642                        writer.WritePropertyName("Addresses");
 643                        writer.WriteStartArray();
 644                        foreach (Address address in c.Addresses)
 645                        {
 646                            writer.WriteStartObject();
 647                            writer.WritePropertyName("Street");
 648                            writer.WriteValue(address.Street);
 649                            writer.WritePropertyName("Phone");
 650                            writer.WriteValue(address.Phone);
 651                            writer.WritePropertyName("Entered");
 652                            writer.WriteValue(address.Entered);
 653                            writer.WriteEndObject();
 654                        }
 655                        writer.WriteEndArray();
 656                        writer.WriteEndObject();
 657
 658                        writer.Flush();
 659                        json = sw.ToString();
 660                    }
 661                    else
 662                    {
 663                        json = string.Empty;
 664                    }
 665                    break;
 666                }
 667                case SerializeMethod.JsonNetBinary:
 668                {
 669                    MemoryStream ms = new MemoryStream(Buffer);
 670                    JsonSerializer serializer = new JsonSerializer();
 671                    BsonWriter writer = new BsonWriter(ms);
 672                    serializer.Serialize(writer, value);
 673
 674                    //json = BitConverter.ToString(ms.ToArray(), 0, (int)ms.Position);
 675                    json = "Bytes = " + ms.Position;
 676                    break;
 677                }
 678                case SerializeMethod.JavaScriptSerializer:
 679                    json = SerializeWebExtensions(value);
 680                    break;
 681                case SerializeMethod.DataContractJsonSerializer:
 682                    json = SerializeDataContractJson(value);
 683                    break;
 684                case SerializeMethod.DataContractSerializer:
 685                    json = SerializeDataContract(value);
 686                    break;
 687                case SerializeMethod.BinaryFormatter:
 688                    json = SerializeBinaryFormatter(value);
 689                    break;
 690                default:
 691                    throw new ArgumentOutOfRangeException("method");
 692            }
 693
 694            return json;
 695        }
 696
 697        private string SerializeBinaryFormatter(object value)
 698        {
 699            string json;
 700            MemoryStream ms = new MemoryStream(Buffer);
 701            BinaryFormatter formatter = new BinaryFormatter();
 702            formatter.Serialize(ms, value);
 703
 704            json = "Bytes = " + ms.Position;
 705            //json = BitConverter.ToString(ms.ToArray(), 0, (int)ms.Position);
 706            return json;
 707        }
 708        #endregion
 709
 710        #region Deserialize
 711        public void BenchmarkDeserializeMethod<T>(SerializeMethod method, object json)
 712        {
 713            Deserialize<T>(method, json);
 714
 715            Stopwatch timed = new Stopwatch();
 716            timed.Start();
 717
 718            T value = default(T);
 719            for (int x = 0; x < Iterations; x++)
 720            {
 721                value = Deserialize<T>(method, json);
 722            }
 723
 724            timed.Stop();
 725
 726            Console.WriteLine("Deserialize method: {0}", method);
 727            Console.WriteLine("{0} ms", timed.ElapsedMilliseconds);
 728            Console.WriteLine(value);
 729            Console.WriteLine();
 730        }
 731
 732        public T DeserializeJsonNet<T>(string json, bool isoDateTimeConverter)
 733        {
 734            Type type = typeof(T);
 735
 736            JsonSerializer serializer = new JsonSerializer();
 737            //serializer.ObjectCreationHandling = Newtonsoft.Json.ObjectCreationHandling.Replace;
 738            //serializer.MissingMemberHandling = Newtonsoft.Json.MissingMemberHandling.Ignore;
 739            //serializer.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
 740            if (isoDateTimeConverter)
 741                serializer.Converters.Add(new IsoDateTimeConverter());
 742
 743            var value = (T)serializer.Deserialize(new StringReader(json), type);
 744            return value;
 745        }
 746
 747        public TestClass DeserializeJsonNetManual(string json)
 748        {
 749            TestClass c = new TestClass();
 750
 751            JsonTextReader reader = new JsonTextReader(new StringReader(json));
 752            reader.Read();
 753            while (reader.Read())
 754            {
 755                if (reader.TokenType == JsonToken.PropertyName)
 756                {
 757                    string propertyName = (string)reader.Value;
 758                    switch (propertyName)
 759                    {
 760                        case "strings":
 761                            reader.Read();
 762                            while (reader.Read() && reader.TokenType != JsonToken.EndArray)
 763                            {
 764                                c.strings.Add((string)reader.Value);
 765                            }
 766                            break;
 767                        case "dictionary":
 768                            reader.Read();
 769                            while (reader.Read() && reader.TokenType != JsonToken.EndObject)
 770                            {
 771                                string key = (string)reader.Value;
 772                                c.dictionary.Add(key, reader.ReadAsInt32().GetValueOrDefault());
 773                            }
 774                            break;
 775                        case "Name":
 776                            c.Name = reader.ReadAsString();
 777                            break;
 778                        case "Now":
 779                            c.Now = reader.ReadAsDateTime().GetValueOrDefault();
 780                            break;
 781                        case "BigNumber":
 782                            c.BigNumber = reader.ReadAsDecimal().GetValueOrDefault();
 783                            break;
 784                        case "Address1":
 785                            reader.Read();
 786                            c.Address1 = CreateAddress(reader);
 787                            break;
 788                        case "Addresses":
 789                            reader.Read();
 790                            while (reader.Read() && reader.TokenType != JsonToken.EndArray)
 791                            {
 792                                var address = CreateAddress(reader);
 793                                c.Addresses.Add(address);
 794                            }
 795                            break;
 796                    }
 797                }
 798                else
 799                {
 800                    break;
 801                }
 802            }
 803
 804            return c;
 805        }
 806
 807        private static Address CreateAddress(JsonTextReader reader)
 808        {
 809            Address a = new Address();
 810            while (reader.Read())
 811            {
 812                if (reader.TokenType == JsonToken.PropertyName)
 813                {
 814                    switch ((string)reader.Value)
 815                    {
 816                        case "Street":
 817                            a.Street = reader.ReadAsString();
 818                            break;
 819                        case "Phone":
 820                            a.Phone = reader.ReadAsString();
 821                            break;
 822                        case "Entered":
 823                            a.Entered = reader.ReadAsDateTime().GetValueOrDefault();
 824                            break;
 825                    }
 826                }
 827                else
 828                {
 829                    break;
 830                }
 831            }
 832            return a;
 833        }
 834
 835        public T DeserializeJsonNetBinary<T>(byte[] bson)
 836        {
 837            Type type = typeof(T);
 838
 839            JsonSerializer serializer = new JsonSerializer();
 840            serializer.ObjectCreationHandling = Newtonsoft.Json.ObjectCreationHandling.Replace;
 841            serializer.MissingMemberHandling = Newtonsoft.Json.MissingMemberHandling.Ignore;
 842            serializer.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
 843
 844            return (T)serializer.Deserialize(new BsonReader(new MemoryStream(bson)), type);
 845        }
 846
 847        public T DeserializeWebExtensions<T>(string json)
 848        {
 849            JavaScriptSerializer ser = new JavaScriptSerializer();
 850
 851            return ser.Deserialize<T>(json);
 852        }
 853
 854        public T DeserializeDataContractJson<T>(string json)
 855        {
 856            DataContractJsonSerializer dataContractSerializer
 857                = new DataContractJsonSerializer(typeof(T));
 858
 859            MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(json));
 860
 861            return (T)dataContractSerializer.ReadObject(ms);
 862        }
 863
 864        private T Deserialize<T>(SerializeMethod method, object json)
 865        {
 866            switch (method)
 867            {
 868                case SerializeMethod.JsonNet:
 869                    return DeserializeJsonNet<T>((string)json, false);
 870                case SerializeMethod.JsonNetWithIsoConverter:
 871                    return DeserializeJsonNet<T>((string)json, true);
 872                case SerializeMethod.JsonNetManual:
 873                    if (typeof(T) == typeof(TestClass))
 874                        return (T)(object)DeserializeJsonNetManual((string)json);
 875
 876                    return default(T);
 877                case SerializeMethod.JsonNetBinary:
 878                    return DeserializeJsonNetBinary<T>((byte[])json);
 879                case SerializeMethod.BinaryFormatter:
 880                    return DeserializeBinaryFormatter<T>((byte[])json);
 881                case SerializeMethod.JavaScriptSerializer:
 882                    return DeserializeWebExtensions<T>((string)json);
 883                case SerializeMethod.DataContractSerializer:
 884                    return DeserializeDataContract<T>((string)json);
 885                case SerializeMethod.DataContractJsonSerializer:
 886                    return DeserializeDataContractJson<T>((string)json);
 887                default:
 888                    throw new ArgumentOutOfRangeException("method");
 889            }
 890        }
 891
 892        private T DeserializeDataContract<T>(string xml)
 893        {
 894            MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(xml));
 895
 896            DataContractSerializer serializer = new DataContractSerializer(typeof(T));
 897            return (T)serializer.ReadObject(ms);
 898        }
 899
 900        private T DeserializeBinaryFormatter<T>(byte[] bytes)
 901        {
 902            BinaryFormatter formatter = new BinaryFormatter();
 903            return (T)formatter.Deserialize(new MemoryStream(bytes));
 904        }
 905        #endregion
 906
 907        [Test]
 908        public void SerializeLargeObject()
 909        {
 910            LargeRecursiveTestClass rootValue = null;
 911            LargeRecursiveTestClass parentValue = null;
 912            for (int i = 0; i < 20; i++)
 913            {
 914                LargeRecursiveTestClass currentValue = new LargeRecursiveTestClass()
 915                {
 916                    Integer = int.MaxValue,
 917                    Text = "The quick red fox jumped over the lazy dog."
 918                };
 919
 920                if (rootValue == null)
 921                    rootValue = currentValue;
 922                if (parentValue != null)
 923                    parentValue.Child = currentValue;
 924
 925                parentValue = currentValue;
 926            }
 927
 928            BenchmarkSerializeMethod(SerializeMethod.JsonNetBinary, rootValue);
 929        }
 930
 931        [Test]
 932        public void SerializeUnicodeChars()
 933        {
 934            string s = (new string('\0', 30));
 935
 936            BenchmarkSerializeMethod(SerializeMethod.JsonNet, s);
 937        }
 938
 939        [Test]
 940        public void ParseJObject()
 941        {
 942            Stopwatch timer = new Stopwatch();
 943            timer.Start();
 944            for (int i = 0; i < 100000; i++)
 945            {
 946                JObject o = JObject.Parse(@"{
 947  ""CPU"": ""Intel"",
 948  ""Drives"": [
 949    ""DVD read/writer"",
 950    ""500 gigabyte hard drive""
 951  ]
 952}");
 953            }
 954            timer.Stop();
 955
 956            string linq = timer.Elapsed.TotalSeconds.ToString();
 957            Console.WriteLine(linq);
 958        }
 959
 960        [Test]
 961        public void JObjectToString()
 962        {
 963            JObject test = JObject.Parse(JsonText);
 964
 965            TimeOperation<object>(() =>
 966            {
 967                for (int i = 0; i < Iterations; i++)
 968                {
 969                    test["dummy"] = new JValue(i);
 970                    test.ToString(Formatting.None);
 971                }
 972                return null;
 973            }, "JObject.ToString");
 974        }
 975
 976        [Test]
 977        public void JObjectToString2()
 978        {
 979            JObject test = JObject.Parse(JsonText);
 980            MemoryStream ms = new MemoryStream();
 981
 982            TimeOperation<object>(() =>
 983            {
 984                for (int i = 0; i < Iterations; i++)
 985                {
 986                    test["dummy"] = new JValue(i);
 987                    ms.Seek(0, SeekOrigin.Begin);
 988                    JsonTextWriter jsonTextWriter = new JsonTextWriter(new StreamWriter(ms));
 989                    test.WriteTo(jsonTextWriter);
 990                    jsonTextWriter.Flush();
 991                    ms.ToArray();
 992
 993                    //Encoding.UTF8.GetBytes(test.ToString(Formatting.None));
 994                }
 995                return null;
 996            }, "JObject.ToString");
 997        }
 998
 999        [Test]
1000        public void NestedJToken()
1001        {
1002            Stopwatch sw;
1003            for (int i = 10000; i <= 100000; i += 10000)
1004            {
1005                sw = new Stopwatch();
1006                sw.Start();
1007                JArray ija = new JArray();
1008                JToken ijt = ija;
1009                for (int j = 0; j < i; j++)
1010                {
1011                    JArray temp = new JArray();
1012                    ija.Add(temp);
1013                    ija = temp;
1014                }
1015                ija.Add(1);
1016                sw.Stop();
1017                Console.WriteLine("Created a JToken of depth {0} (using OM) in {1} millis", i, sw.ElapsedMilliseconds);
1018            }
1019        }
1020
1021        [Test]
1022        public void DeserializeNestedJToken()
1023        {
1024            string json = (new string('[', 100000)) + "1" + ((new string(']', 100000)));
1025
1026            Stopwatch sw;
1027            sw = new Stopwatch();
1028            sw.Start();
1029
1030            var a = (JArray)JsonConvert.DeserializeObject(json);
1031
1032            sw.Stop();
1033
1034            Assert.AreEqual(1, a.Count);
1035
1036            Console.WriteLine("Deserialize big ass nested array in {0} millis", sw.ElapsedMilliseconds);
1037        }
1038    }
1039
1040    public class LargeRecursiveTestClass
1041    {
1042        public LargeRecursiveTestClass Child { get; set; }
1043        public string Text { get; set; }
1044        public int Integer { get; set; }
1045    }
1046
1047    #region Classes
1048    [Serializable]
1049    [DataContract]
1050    public class TestClass
1051    {
1052        [DataMember]
1053        public string Name
1054        {
1055            get { return _Name; }
1056            set { _Name = value; }
1057        }
1058
1059        private string _Name = "Rick";
1060
1061        [DataMember]
1062        public DateTime Now
1063        {
1064            get { return _Now; }
1065            set { _Now = value; }
1066        }
1067
1068        private DateTime _Now = DateTime.Now;
1069
1070        [DataMember]
1071        public decimal BigNumber
1072        {
1073            get { return _BigNumber; }
1074            set { _BigNumber = value; }
1075        }
1076
1077        private decimal _BigNumber = 1212121.22M;
1078
1079        [DataMember]
1080        public Address Address1
1081        {
1082            get { return _Address1; }
1083            set { _Address1 = value; }
1084        }
1085
1086        private Address _Address1 = new Address();
1087
1088
1089        [DataMember]
1090        public List<Address> Addresses
1091        {
1092            get { return _Addresses; }
1093            set { _Addresses = value; }
1094        }
1095
1096        private List<Address> _Addresses = new List<Address>();
1097
1098        [DataMember]
1099        public List<string> strings = new List<string>();
1100
1101        [DataMember]
1102        public Dictionary<string, int> dictionary = new Dictionary<string, int>();
1103    }
1104
1105    [Serializable]
1106    [DataContract]
1107    public class Address
1108    {
1109        [DataMember]
1110        public string Street
1111        {
1112            get { return _street; }
1113            set { _street = value; }
1114        }
1115
1116        private string _street = "32 Kaiea";
1117
1118        [DataMember]
1119        public string Phone
1120        {
1121            get { return _Phone; }
1122            set { _Phone = value; }
1123        }
1124
1125        private string _Phone = "(503) 814-6335";
1126
1127        [DataMember]
1128        public DateTime Entered
1129        {
1130            get { return _Entered; }
1131            set { _Entered = value; }
1132        }
1133
1134        private DateTime _Entered = DateTime.Parse("01/01/2007", CultureInfo.CurrentCulture.DateTimeFormat);
1135    }
1136
1137    [DataContract]
1138    [Serializable]
1139    public class SimpleObject
1140    {
1141        [DataMember]
1142        public int Id { get; set; }
1143
1144        [DataMember]
1145        public string Name { get; set; }
1146
1147        [DataMember]
1148        public string Address { get; set; }
1149
1150        [DataMember]
1151        public int[] Scores { get; set; }
1152    }
1153    #endregion
1154}
1155
1156#endif