PageRenderTime 55ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/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. #if !(NET20 || NET35 || NETFX_CORE || PORTABLE)
  26. using System;
  27. using System.Collections;
  28. using System.Collections.Generic;
  29. using System.Globalization;
  30. using System.Diagnostics;
  31. using System.IO;
  32. using System.Linq;
  33. using System.Runtime.Serialization;
  34. using System.Web.Script.Serialization;
  35. using System.Xml.Linq;
  36. using Newtonsoft.Json.Utilities;
  37. using NUnit.Framework;
  38. using System.Runtime.Serialization.Json;
  39. using System.Text;
  40. using Newtonsoft.Json.Bson;
  41. using System.Runtime.Serialization.Formatters.Binary;
  42. using Newtonsoft.Json.Linq;
  43. using Newtonsoft.Json.Converters;
  44. namespace Newtonsoft.Json.Tests
  45. {
  46. [Serializable]
  47. [DataContract]
  48. public class Image
  49. {
  50. [DataMember]
  51. public string FileName { get; set; }
  52. [DataMember]
  53. public string Author { get; set; }
  54. [DataMember]
  55. public string Caption { get; set; }
  56. [DataMember]
  57. public byte[] Data { get; set; }
  58. }
  59. [TestFixture]
  60. public class PerformanceTests : TestFixtureBase
  61. {
  62. private const int Iterations = 100;
  63. //private const int Iterations = 5000;
  64. #region Data
  65. private const string BsonHex =
  66. @"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";
  67. private const string BinaryFormatterHex =
  68. @"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";
  69. private const string XmlText =
  70. @"<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>";
  71. private const string JsonText =
  72. @"{""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)\/""}]}";
  73. private const string JsonIsoText =
  74. @"{""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""}]}";
  75. private const string SimpleJsonText =
  76. @"{""Id"":2311,""Name"":""Simple-1"",""Address"":""Planet Earth"",""Scores"":[82,96,49,40,38,38,78,96,2,39]}";
  77. public enum SerializeMethod
  78. {
  79. JsonNet,
  80. JsonNetWithIsoConverter,
  81. JsonNetBinary,
  82. JsonNetLinq,
  83. JsonNetManual,
  84. BinaryFormatter,
  85. JavaScriptSerializer,
  86. DataContractSerializer,
  87. DataContractJsonSerializer
  88. }
  89. #endregion
  90. [Test]
  91. public void SerializeSimpleObject()
  92. {
  93. SimpleObject value = CreateSimpleObject();
  94. SerializeTests(value);
  95. }
  96. [Test]
  97. public void SerializeAnonymous()
  98. {
  99. var helloWorld = new { message = "Hello, World!" };
  100. BenchmarkSerializeMethod(SerializeMethod.JsonNet, helloWorld);
  101. }
  102. [Test]
  103. public void DeserializeSimpleObject()
  104. {
  105. DeserializeTests<SimpleObject>(SimpleJsonText);
  106. }
  107. [Test]
  108. public void Serialize()
  109. {
  110. TestClass test = CreateSerializationObject();
  111. SerializeTests(test);
  112. }
  113. private void SerializeTests(object value)
  114. {
  115. BenchmarkSerializeMethod(SerializeMethod.DataContractSerializer, value);
  116. BenchmarkSerializeMethod(SerializeMethod.BinaryFormatter, value);
  117. BenchmarkSerializeMethod(SerializeMethod.JavaScriptSerializer, value);
  118. BenchmarkSerializeMethod(SerializeMethod.DataContractJsonSerializer, value);
  119. BenchmarkSerializeMethod(SerializeMethod.JsonNet, value);
  120. BenchmarkSerializeMethod(SerializeMethod.JsonNetLinq, value);
  121. BenchmarkSerializeMethod(SerializeMethod.JsonNetManual, value);
  122. BenchmarkSerializeMethod(SerializeMethod.JsonNetWithIsoConverter, value);
  123. BenchmarkSerializeMethod(SerializeMethod.JsonNetBinary, value);
  124. }
  125. [Test]
  126. public void Deserialize()
  127. {
  128. BenchmarkDeserializeMethod<TestClass>(SerializeMethod.DataContractSerializer, XmlText);
  129. BenchmarkDeserializeMethod<TestClass>(SerializeMethod.BinaryFormatter, HexToBytes(BinaryFormatterHex));
  130. DeserializeTests<TestClass>(JsonText);
  131. BenchmarkDeserializeMethod<TestClass>(SerializeMethod.JsonNetWithIsoConverter, JsonIsoText);
  132. BenchmarkDeserializeMethod<TestClass>(SerializeMethod.JsonNetBinary, HexToBytes(BsonHex));
  133. }
  134. public void DeserializeTests<T>(string json)
  135. {
  136. BenchmarkDeserializeMethod<T>(SerializeMethod.JavaScriptSerializer, json);
  137. BenchmarkDeserializeMethod<T>(SerializeMethod.DataContractJsonSerializer, json);
  138. BenchmarkDeserializeMethod<T>(SerializeMethod.JsonNet, json);
  139. BenchmarkDeserializeMethod<T>(SerializeMethod.JsonNetManual, json);
  140. }
  141. [Test]
  142. public void SerializeSizeNormal()
  143. {
  144. SerializeSize(CreateSerializationObject());
  145. }
  146. [Test]
  147. public void SerializeSizeData()
  148. {
  149. Image image = new Image();
  150. image.Data = System.IO.File.ReadAllBytes(@"bunny_pancake.jpg");
  151. image.FileName = "bunny_pancake.jpg";
  152. image.Author = "Hironori Akutagawa";
  153. image.Caption = "I have no idea what you are talking about so here's a bunny with a pancake on its head";
  154. SerializeSize(image);
  155. }
  156. private T TimeOperation<T>(Func<T> operation, string name)
  157. {
  158. // warm up
  159. operation();
  160. Stopwatch timed = new Stopwatch();
  161. timed.Start();
  162. T result = operation();
  163. Console.WriteLine(name);
  164. Console.WriteLine("{0} ms", timed.ElapsedMilliseconds);
  165. timed.Stop();
  166. return result;
  167. }
  168. [Test]
  169. public void BuildJObject()
  170. {
  171. JObject o = new JObject();
  172. for (int i = 0; i < 50; i++)
  173. {
  174. o[i.ToString()] = i;
  175. }
  176. string jsonText = o.ToString();
  177. // this is extremely slow with 5000 interations
  178. int interations = 1000;
  179. TimeOperation(() =>
  180. {
  181. JObject oo = null;
  182. for (int i = 0; i < interations; i++)
  183. {
  184. oo = JObject.Parse(jsonText);
  185. }
  186. return oo;
  187. }, "JObject");
  188. }
  189. [Test]
  190. public void BuildJObjectComparedToXml()
  191. {
  192. const long totalIterations = 100000;
  193. const String xml =
  194. @"<?xml version=""1.0"" encoding=""ISO-8859-1""?>
  195. <root>
  196. <property name=""Property1"">1</property>
  197. <property name=""Property2"">2</property>
  198. <property name=""Property3"">3</property>
  199. <property name=""Property4"">4</property>
  200. <property name=""Property5"">5</property>
  201. </root>";
  202. const String json =
  203. @"{
  204. ""Property1"":""1"",
  205. ""Property2"":""2"",
  206. ""Property3"":""3"",
  207. ""Property4"":""4"",
  208. ""Property5"":""5""
  209. }";
  210. var watch = new Stopwatch();
  211. watch.Start();
  212. for (long iteration = 0; iteration < totalIterations; ++iteration)
  213. {
  214. var obj = JObject.Parse(json);
  215. obj["Property1"].Value<Int32>();
  216. obj["Property2"].Value<Int32>();
  217. obj["Property3"].Value<Int32>();
  218. obj["Property4"].Value<Int32>();
  219. obj["Property5"].Value<Int32>();
  220. }
  221. watch.Stop();
  222. var performance1 = (totalIterations / watch.ElapsedMilliseconds) * 1000;
  223. Console.WriteLine("JSON: " + watch.Elapsed.TotalSeconds);
  224. watch.Reset();
  225. watch.Start();
  226. for (long iteration = 0; iteration < totalIterations; ++iteration)
  227. {
  228. var doc = XDocument.Parse(xml);
  229. var alarmProperties = doc.Descendants("property");
  230. foreach (var property in alarmProperties)
  231. {
  232. var attr = property.Attribute("name");
  233. var name = attr.Value;
  234. switch (name)
  235. {
  236. case "Property1":
  237. Int32.Parse(property.Value);
  238. break;
  239. case "Property2":
  240. Int32.Parse(property.Value);
  241. break;
  242. case "Property3":
  243. Int32.Parse(property.Value);
  244. break;
  245. case "Property4":
  246. Int32.Parse(property.Value);
  247. break;
  248. case "Property5":
  249. Int32.Parse(property.Value);
  250. break;
  251. }
  252. }
  253. }
  254. watch.Stop();
  255. var performance2 = (totalIterations / watch.ElapsedMilliseconds) * 1000;
  256. Console.WriteLine("XML: " + watch.Elapsed.TotalSeconds);
  257. }
  258. [Test]
  259. public void SerializeString()
  260. {
  261. string text = @"The general form of an HTML element is therefore: <tag attribute1=""value1"" attribute2=""value2"">content</tag>.
  262. Some HTML elements are defined as empty elements and take the form <tag attribute1=""value1"" attribute2=""value2"" >.
  263. Empty elements may enclose no content, for instance, the BR tag or the inline IMG tag.
  264. The name of an HTML element is the name used in the tags.
  265. Note 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.
  266. If attributes are not mentioned, default values are used in each case.
  267. The general form of an HTML element is therefore: <tag attribute1=""value1"" attribute2=""value2"">content</tag>.
  268. Some HTML elements are defined as empty elements and take the form <tag attribute1=""value1"" attribute2=""value2"" >.
  269. Empty elements may enclose no content, for instance, the BR tag or the inline IMG tag.
  270. The name of an HTML element is the name used in the tags.
  271. Note 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.
  272. If attributes are not mentioned, default values are used in each case.
  273. The general form of an HTML element is therefore: <tag attribute1=""value1"" attribute2=""value2"">content</tag>.
  274. Some HTML elements are defined as empty elements and take the form <tag attribute1=""value1"" attribute2=""value2"" >.
  275. Empty elements may enclose no content, for instance, the BR tag or the inline IMG tag.
  276. The name of an HTML element is the name used in the tags.
  277. Note 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.
  278. If attributes are not mentioned, default values are used in each case.
  279. ";
  280. int interations = 1000;
  281. TimeOperation(() =>
  282. {
  283. for (int i = 0; i < interations; i++)
  284. {
  285. using (StringWriter w = StringUtils.CreateStringWriter(StringUtils.GetLength(text) ?? 16))
  286. {
  287. char[] buffer = null;
  288. JavaScriptUtils.WriteEscapedJavaScriptString(w, text, '"', true, JavaScriptUtils.DoubleQuoteCharEscapeFlags, StringEscapeHandling.Default, ref buffer);
  289. }
  290. }
  291. return "";
  292. }, "New");
  293. }
  294. [Test]
  295. public void JTokenToObject()
  296. {
  297. JValue s = new JValue("String!");
  298. int interations = 1000000;
  299. TimeOperation(() =>
  300. {
  301. for (int i = 0; i < interations; i++)
  302. {
  303. s.ToObject(typeof(string));
  304. }
  305. return "";
  306. }, "New");
  307. TimeOperation(() =>
  308. {
  309. for (int i = 0; i < interations; i++)
  310. {
  311. s.ToObject(typeof(string), new JsonSerializer());
  312. }
  313. return "";
  314. }, "Old");
  315. TimeOperation(() =>
  316. {
  317. for (int i = 0; i < interations; i++)
  318. {
  319. s.Value<string>();
  320. }
  321. return "";
  322. }, "Value");
  323. }
  324. private void SerializeSize(object value)
  325. {
  326. // this is extremely slow with 5000 interations
  327. int interations = 100;
  328. byte[] jsonBytes = TimeOperation(() =>
  329. {
  330. string json = null;
  331. for (int i = 0; i < interations; i++)
  332. {
  333. json = JsonConvert.SerializeObject(value, Formatting.None);
  334. }
  335. return Encoding.UTF8.GetBytes(json);
  336. }, "Json.NET");
  337. byte[] bsonBytes = TimeOperation(() =>
  338. {
  339. MemoryStream ms = null;
  340. for (int i = 0; i < interations; i++)
  341. {
  342. ms = new MemoryStream();
  343. JsonSerializer serializer = new JsonSerializer();
  344. BsonWriter writer = new BsonWriter(ms);
  345. serializer.Serialize(writer, value);
  346. writer.Flush();
  347. }
  348. return ms.ToArray();
  349. }, "Json.NET BSON");
  350. byte[] xmlBytes = TimeOperation(() =>
  351. {
  352. MemoryStream ms = null;
  353. for (int i = 0; i < interations; i++)
  354. {
  355. ms = new MemoryStream();
  356. DataContractSerializer dataContractSerializer = new DataContractSerializer(value.GetType());
  357. dataContractSerializer.WriteObject(ms, value);
  358. }
  359. return ms.ToArray();
  360. }, "DataContractSerializer");
  361. byte[] wcfJsonBytes = TimeOperation(() =>
  362. {
  363. MemoryStream ms = null;
  364. for (int i = 0; i < interations; i++)
  365. {
  366. ms = new MemoryStream();
  367. DataContractJsonSerializer dataContractJsonSerializer = new DataContractJsonSerializer(value.GetType());
  368. dataContractJsonSerializer.WriteObject(ms, value);
  369. }
  370. return ms.ToArray();
  371. }, "DataContractJsonSerializer");
  372. byte[] binaryFormatterBytes = TimeOperation(() =>
  373. {
  374. MemoryStream ms = null;
  375. for (int i = 0; i < interations; i++)
  376. {
  377. ms = new MemoryStream();
  378. BinaryFormatter formatter = new BinaryFormatter();
  379. formatter.Serialize(ms, value);
  380. }
  381. return ms.ToArray();
  382. }, "BinaryFormatter");
  383. Console.WriteLine("Json.NET size: {0} bytes", jsonBytes.Length);
  384. Console.WriteLine("BSON size: {0} bytes", bsonBytes.Length);
  385. Console.WriteLine("WCF JSON size: {0} bytes", wcfJsonBytes.Length);
  386. Console.WriteLine("WCF XML size: {0} bytes", xmlBytes.Length);
  387. Console.WriteLine("BinaryFormatter size: {0} bytes", binaryFormatterBytes.Length);
  388. }
  389. #region Serialize
  390. private static readonly byte[] Buffer = new byte[4096];
  391. public void BenchmarkSerializeMethod(SerializeMethod method, object value)
  392. {
  393. Serialize(method, value);
  394. Stopwatch timed = new Stopwatch();
  395. timed.Start();
  396. string json = null;
  397. for (int x = 0; x < Iterations; x++)
  398. {
  399. json = Serialize(method, value);
  400. }
  401. timed.Stop();
  402. Console.WriteLine("Serialize method: {0}", method);
  403. Console.WriteLine("{0} ms", timed.ElapsedMilliseconds);
  404. Console.WriteLine(json);
  405. Console.WriteLine();
  406. }
  407. private TestClass CreateSerializationObject()
  408. {
  409. TestClass test = new TestClass();
  410. test.dictionary = new Dictionary<string, int> { { "Val & asd1", 1 }, { "Val2 & asd1", 3 }, { "Val3 & asd1", 4 } };
  411. test.Address1.Street = "fff Street";
  412. test.Address1.Entered = DateTime.Now.AddDays(20);
  413. test.BigNumber = 34123123123.121M;
  414. test.Now = DateTime.Now.AddHours(1);
  415. test.strings = new List<string>() { null, "Markus egger ]><[, (2nd)", null };
  416. Address address = new Address();
  417. address.Entered = DateTime.Now.AddDays(-1);
  418. address.Street = "\u001farray\u003caddress";
  419. test.Addresses.Add(address);
  420. address = new Address();
  421. address.Entered = DateTime.Now.AddDays(-2);
  422. address.Street = "array 2 address";
  423. test.Addresses.Add(address);
  424. return test;
  425. }
  426. private static SimpleObject CreateSimpleObject()
  427. {
  428. return new SimpleObject
  429. {
  430. Name = "Simple-1",
  431. Id = 2311,
  432. Address = "Planet Earth",
  433. Scores = new[] { 82, 96, 49, 40, 38, 38, 78, 96, 2, 39 }
  434. };
  435. }
  436. public string SerializeWebExtensions(object value)
  437. {
  438. JavaScriptSerializer ser = new JavaScriptSerializer();
  439. return ser.Serialize(value);
  440. }
  441. public string SerializeDataContractJson(object value)
  442. {
  443. DataContractJsonSerializer dataContractSerializer
  444. = new DataContractJsonSerializer(value.GetType());
  445. MemoryStream ms = new MemoryStream();
  446. dataContractSerializer.WriteObject(ms, value);
  447. ms.Seek(0, SeekOrigin.Begin);
  448. using (StreamReader sr = new StreamReader(ms))
  449. {
  450. return sr.ReadToEnd();
  451. }
  452. }
  453. public string SerializeDataContract(object value)
  454. {
  455. DataContractSerializer dataContractSerializer
  456. = new DataContractSerializer(value.GetType());
  457. MemoryStream ms = new MemoryStream();
  458. dataContractSerializer.WriteObject(ms, value);
  459. ms.Seek(0, SeekOrigin.Begin);
  460. using (StreamReader sr = new StreamReader(ms))
  461. {
  462. return sr.ReadToEnd();
  463. }
  464. }
  465. private string Serialize(SerializeMethod method, object value)
  466. {
  467. string json;
  468. switch (method)
  469. {
  470. case SerializeMethod.JsonNet:
  471. json = JsonConvert.SerializeObject(value);
  472. break;
  473. case SerializeMethod.JsonNetWithIsoConverter:
  474. json = JsonConvert.SerializeObject(value, new IsoDateTimeConverter());
  475. break;
  476. case SerializeMethod.JsonNetLinq:
  477. {
  478. TestClass c = value as TestClass;
  479. if (c != null)
  480. {
  481. JObject o = new JObject(
  482. new JProperty("strings", new JArray(
  483. c.strings
  484. )),
  485. new JProperty("dictionary", new JObject(c.dictionary.Select(d => new JProperty(d.Key, d.Value)))),
  486. new JProperty("Name", c.Name),
  487. new JProperty("Now", c.Now),
  488. new JProperty("BigNumber", c.BigNumber),
  489. new JProperty("Address1", new JObject(
  490. new JProperty("Street", c.Address1.Street),
  491. new JProperty("Phone", c.Address1.Phone),
  492. new JProperty("Entered", c.Address1.Entered))),
  493. new JProperty("Addresses", new JArray(c.Addresses.Select(a =>
  494. new JObject(
  495. new JProperty("Street", a.Street),
  496. new JProperty("Phone", a.Phone),
  497. new JProperty("Entered", a.Entered)))))
  498. );
  499. json = o.ToString(Formatting.None);
  500. }
  501. else
  502. {
  503. json = string.Empty;
  504. }
  505. break;
  506. }
  507. case SerializeMethod.JsonNetManual:
  508. {
  509. TestClass c = value as TestClass;
  510. if (c != null)
  511. {
  512. StringWriter sw = new StringWriter();
  513. JsonTextWriter writer = new JsonTextWriter(sw);
  514. writer.WriteStartObject();
  515. writer.WritePropertyName("strings");
  516. writer.WriteStartArray();
  517. foreach (string s in c.strings)
  518. {
  519. writer.WriteValue(s);
  520. }
  521. writer.WriteEndArray();
  522. writer.WritePropertyName("dictionary");
  523. writer.WriteStartObject();
  524. foreach (KeyValuePair<string, int> keyValuePair in c.dictionary)
  525. {
  526. writer.WritePropertyName(keyValuePair.Key);
  527. writer.WriteValue(keyValuePair.Value);
  528. }
  529. writer.WriteEndObject();
  530. writer.WritePropertyName("Name");
  531. writer.WriteValue(c.Name);
  532. writer.WritePropertyName("Now");
  533. writer.WriteValue(c.Now);
  534. writer.WritePropertyName("BigNumber");
  535. writer.WriteValue(c.BigNumber);
  536. writer.WritePropertyName("Address1");
  537. writer.WriteStartObject();
  538. writer.WritePropertyName("Street");
  539. writer.WriteValue(c.BigNumber);
  540. writer.WritePropertyName("Street");
  541. writer.WriteValue(c.BigNumber);
  542. writer.WritePropertyName("Street");
  543. writer.WriteValue(c.BigNumber);
  544. writer.WriteEndObject();
  545. writer.WritePropertyName("Addresses");
  546. writer.WriteStartArray();
  547. foreach (Address address in c.Addresses)
  548. {
  549. writer.WriteStartObject();
  550. writer.WritePropertyName("Street");
  551. writer.WriteValue(address.Street);
  552. writer.WritePropertyName("Phone");
  553. writer.WriteValue(address.Phone);
  554. writer.WritePropertyName("Entered");
  555. writer.WriteValue(address.Entered);
  556. writer.WriteEndObject();
  557. }
  558. writer.WriteEndArray();
  559. writer.WriteEndObject();
  560. writer.Flush();
  561. json = sw.ToString();
  562. }
  563. else
  564. {
  565. json = string.Empty;
  566. }
  567. break;
  568. }
  569. case SerializeMethod.JsonNetBinary:
  570. {
  571. MemoryStream ms = new MemoryStream(Buffer);
  572. JsonSerializer serializer = new JsonSerializer();
  573. BsonWriter writer = new BsonWriter(ms);
  574. serializer.Serialize(writer, value);
  575. //json = BitConverter.ToString(ms.ToArray(), 0, (int)ms.Position);
  576. json = "Bytes = " + ms.Position;
  577. break;
  578. }
  579. case SerializeMethod.JavaScriptSerializer:
  580. json = SerializeWebExtensions(value);
  581. break;
  582. case SerializeMethod.DataContractJsonSerializer:
  583. json = SerializeDataContractJson(value);
  584. break;
  585. case SerializeMethod.DataContractSerializer:
  586. json = SerializeDataContract(value);
  587. break;
  588. case SerializeMethod.BinaryFormatter:
  589. json = SerializeBinaryFormatter(value);
  590. break;
  591. default:
  592. throw new ArgumentOutOfRangeException("method");
  593. }
  594. return json;
  595. }
  596. private string SerializeBinaryFormatter(object value)
  597. {
  598. string json;
  599. MemoryStream ms = new MemoryStream(Buffer);
  600. BinaryFormatter formatter = new BinaryFormatter();
  601. formatter.Serialize(ms, value);
  602. json = "Bytes = " + ms.Position;
  603. //json = BitConverter.ToString(ms.ToArray(), 0, (int)ms.Position);
  604. return json;
  605. }
  606. #endregion
  607. #region Deserialize
  608. public void BenchmarkDeserializeMethod<T>(SerializeMethod method, object json)
  609. {
  610. Deserialize<T>(method, json);
  611. Stopwatch timed = new Stopwatch();
  612. timed.Start();
  613. T value = default(T);
  614. for (int x = 0; x < Iterations; x++)
  615. {
  616. value = Deserialize<T>(method, json);
  617. }
  618. timed.Stop();
  619. Console.WriteLine("Deserialize method: {0}", method);
  620. Console.WriteLine("{0} ms", timed.ElapsedMilliseconds);
  621. Console.WriteLine(value);
  622. Console.WriteLine();
  623. }
  624. public T DeserializeJsonNet<T>(string json, bool isoDateTimeConverter)
  625. {
  626. Type type = typeof(T);
  627. JsonSerializer serializer = new JsonSerializer();
  628. //serializer.ObjectCreationHandling = Newtonsoft.Json.ObjectCreationHandling.Replace;
  629. //serializer.MissingMemberHandling = Newtonsoft.Json.MissingMemberHandling.Ignore;
  630. //serializer.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
  631. if (isoDateTimeConverter)
  632. serializer.Converters.Add(new IsoDateTimeConverter());
  633. var value = (T)serializer.Deserialize(new StringReader(json), type);
  634. return value;
  635. }
  636. public TestClass DeserializeJsonNetManual(string json)
  637. {
  638. TestClass c = new TestClass();
  639. JsonTextReader reader = new JsonTextReader(new StringReader(json));
  640. reader.Read();
  641. while (reader.Read())
  642. {
  643. if (reader.TokenType == JsonToken.PropertyName)
  644. {
  645. string propertyName = (string)reader.Value;
  646. switch (propertyName)
  647. {
  648. case "strings":
  649. reader.Read();
  650. while (reader.Read() && reader.TokenType != JsonToken.EndArray)
  651. {
  652. c.strings.Add((string)reader.Value);
  653. }
  654. break;
  655. case "dictionary":
  656. reader.Read();
  657. while (reader.Read() && reader.TokenType != JsonToken.EndObject)
  658. {
  659. string key = (string)reader.Value;
  660. c.dictionary.Add(key, reader.ReadAsInt32().GetValueOrDefault());
  661. }
  662. break;
  663. case "Name":
  664. c.Name = reader.ReadAsString();
  665. break;
  666. case "Now":
  667. c.Now = reader.ReadAsDateTime().GetValueOrDefault();
  668. break;
  669. case "BigNumber":
  670. c.BigNumber = reader.ReadAsDecimal().GetValueOrDefault();
  671. break;
  672. case "Address1":
  673. reader.Read();
  674. c.Address1 = CreateAddress(reader);
  675. break;
  676. case "Addresses":
  677. reader.Read();
  678. while (reader.Read() && reader.TokenType != JsonToken.EndArray)
  679. {
  680. var address = CreateAddress(reader);
  681. c.Addresses.Add(address);
  682. }
  683. break;
  684. }
  685. }
  686. else
  687. {
  688. break;
  689. }
  690. }
  691. return c;
  692. }
  693. private static Address CreateAddress(JsonTextReader reader)
  694. {
  695. Address a = new Address();
  696. while (reader.Read())
  697. {
  698. if (reader.TokenType == JsonToken.PropertyName)
  699. {
  700. switch ((string)reader.Value)
  701. {
  702. case "Street":
  703. a.Street = reader.ReadAsString();
  704. break;
  705. case "Phone":
  706. a.Phone = reader.ReadAsString();
  707. break;
  708. case "Entered":
  709. a.Entered = reader.ReadAsDateTime().GetValueOrDefault();
  710. break;
  711. }
  712. }
  713. else
  714. {
  715. break;
  716. }
  717. }
  718. return a;
  719. }
  720. public T DeserializeJsonNetBinary<T>(byte[] bson)
  721. {
  722. Type type = typeof(T);
  723. JsonSerializer serializer = new JsonSerializer();
  724. serializer.ObjectCreationHandling = Newtonsoft.Json.ObjectCreationHandling.Replace;
  725. serializer.MissingMemberHandling = Newtonsoft.Json.MissingMemberHandling.Ignore;
  726. serializer.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
  727. return (T)serializer.Deserialize(new BsonReader(new MemoryStream(bson)), type);
  728. }
  729. public T DeserializeWebExtensions<T>(string json)
  730. {
  731. JavaScriptSerializer ser = new JavaScriptSerializer();
  732. return ser.Deserialize<T>(json);
  733. }
  734. public T DeserializeDataContractJson<T>(string json)
  735. {
  736. DataContractJsonSerializer dataContractSerializer
  737. = new DataContractJsonSerializer(typeof(T));
  738. MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(json));
  739. return (T)dataContractSerializer.ReadObject(ms);
  740. }
  741. private T Deserialize<T>(SerializeMethod method, object json)
  742. {
  743. switch (method)
  744. {
  745. case SerializeMethod.JsonNet:
  746. return DeserializeJsonNet<T>((string)json, false);
  747. case SerializeMethod.JsonNetWithIsoConverter:
  748. return DeserializeJsonNet<T>((string)json, true);
  749. case SerializeMethod.JsonNetManual:
  750. if (typeof(T) == typeof(TestClass))
  751. return (T)(object)DeserializeJsonNetManual((string)json);
  752. return default(T);
  753. case SerializeMethod.JsonNetBinary:
  754. return DeserializeJsonNetBinary<T>((byte[])json);
  755. case SerializeMethod.BinaryFormatter:
  756. return DeserializeBinaryFormatter<T>((byte[])json);
  757. case SerializeMethod.JavaScriptSerializer:
  758. return DeserializeWebExtensions<T>((string)json);
  759. case SerializeMethod.DataContractSerializer:
  760. return DeserializeDataContract<T>((string)json);
  761. case SerializeMethod.DataContractJsonSerializer:
  762. return DeserializeDataContractJson<T>((string)json);
  763. default:
  764. throw new ArgumentOutOfRangeException("method");
  765. }
  766. }
  767. private T DeserializeDataContract<T>(string xml)
  768. {
  769. MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(xml));
  770. DataContractSerializer serializer = new DataContractSerializer(typeof(T));
  771. return (T)serializer.ReadObject(ms);
  772. }
  773. private T DeserializeBinaryFormatter<T>(byte[] bytes)
  774. {
  775. BinaryFormatter formatter = new BinaryFormatter();
  776. return (T)formatter.Deserialize(new MemoryStream(bytes));
  777. }
  778. #endregion
  779. [Test]
  780. public void SerializeLargeObject()
  781. {
  782. LargeRecursiveTestClass rootValue = null;
  783. LargeRecursiveTestClass parentValue = null;
  784. for (int i = 0; i < 20; i++)
  785. {
  786. LargeRecursiveTestClass currentValue = new LargeRecursiveTestClass()
  787. {
  788. Integer = int.MaxValue,
  789. Text = "The quick red fox jumped over the lazy dog."
  790. };
  791. if (rootValue == null)
  792. rootValue = currentValue;
  793. if (parentValue != null)
  794. parentValue.Child = currentValue;
  795. parentValue = currentValue;
  796. }
  797. BenchmarkSerializeMethod(SerializeMethod.JsonNetBinary, rootValue);
  798. }
  799. [Test]
  800. public void SerializeUnicodeChars()
  801. {
  802. string s = (new string('\0', 30));
  803. BenchmarkSerializeMethod(SerializeMethod.JsonNet, s);
  804. }
  805. [Test]
  806. public void ParseJObject()
  807. {
  808. Stopwatch timer = new Stopwatch();
  809. timer.Start();
  810. for (int i = 0; i < 100000; i++)
  811. {
  812. JObject o = JObject.Parse(@"{
  813. ""CPU"": ""Intel"",
  814. ""Drives"": [
  815. ""DVD read/writer"",
  816. ""500 gigabyte hard drive""
  817. ]
  818. }");
  819. }
  820. timer.Stop();
  821. string linq = timer.Elapsed.TotalSeconds.ToString();
  822. Console.WriteLine(linq);
  823. }
  824. [Test]
  825. public void JObjectToString()
  826. {
  827. JObject test = JObject.Parse(JsonText);
  828. TimeOperation<object>(() =>
  829. {
  830. for (int i = 0; i < Iterations; i++)
  831. {
  832. test["dummy"] = new JValue(i);
  833. test.ToString(Formatting.None);
  834. }
  835. return null;
  836. }, "JObject.ToString");
  837. }
  838. [Test]
  839. public void JObjectToString2()
  840. {
  841. JObject test = JObject.Parse(JsonText);
  842. MemoryStream ms = new MemoryStream();
  843. TimeOperation<object>(() =>
  844. {
  845. for (int i = 0; i < Iterations; i++)
  846. {
  847. test["dummy"] = new JValue(i);
  848. ms.Seek(0, SeekOrigin.Begin);
  849. JsonTextWriter jsonTextWriter = new JsonTextWriter(new StreamWriter(ms));
  850. test.WriteTo(jsonTextWriter);
  851. jsonTextWriter.Flush();
  852. ms.ToArray();
  853. //Encoding.UTF8.GetBytes(test.ToString(Formatting.None));
  854. }
  855. return null;
  856. }, "JObject.ToString");
  857. }
  858. [Test]
  859. public void NestedJToken()
  860. {
  861. Stopwatch sw;
  862. for (int i = 10000; i <= 100000; i += 10000)
  863. {
  864. sw = new Stopwatch();
  865. sw.Start();
  866. JArray ija = new JArray();
  867. JToken ijt = ija;
  868. for (int j = 0; j < i; j++)
  869. {
  870. JArray temp = new JArray();
  871. ija.Add(temp);
  872. ija = temp;
  873. }
  874. ija.Add(1);
  875. sw.Stop();
  876. Console.WriteLine("Created a JToken of depth {0} (using OM) in {1} millis", i, sw.ElapsedMilliseconds);
  877. }
  878. }
  879. [Test]
  880. public void DeserializeNestedJToken()
  881. {
  882. string json = (new string('[', 100000)) + "1" + ((new string(']', 100000)));
  883. Stopwatch sw;
  884. sw = new Stopwatch();
  885. sw.Start();
  886. var a = (JArray)JsonConvert.DeserializeObject(json);
  887. sw.Stop();
  888. Assert.AreEqual(1, a.Count);
  889. Console.WriteLine("Deserialize big ass nested array in {0} millis", sw.ElapsedMilliseconds);
  890. }
  891. }
  892. public class LargeRecursiveTestClass
  893. {
  894. public LargeRecursiveTestClass Child { get; set; }
  895. public string Text { get; set; }
  896. public int Integer { get; set; }
  897. }
  898. #region Classes
  899. [Serializable]
  900. [DataContract]
  901. public class TestClass
  902. {
  903. [DataMember]
  904. public string Name
  905. {
  906. get { return _Name; }
  907. set { _Name = value; }
  908. }
  909. private string _Name = "Rick";
  910. [DataMember]
  911. public DateTime Now
  912. {
  913. get { return _Now; }
  914. set { _Now = value; }
  915. }
  916. private DateTime _Now = DateTime.Now;
  917. [DataMember]
  918. public decimal BigNumber
  919. {
  920. get { return _BigNumber; }
  921. set { _BigNumber = value; }
  922. }
  923. private decimal _BigNumber = 1212121.22M;
  924. [DataMember]
  925. public Address Address1
  926. {
  927. get { return _Address1; }
  928. set { _Address1 = value; }
  929. }
  930. private Address _Address1 = new Address();
  931. [DataMember]
  932. public List<Address> Addresses
  933. {
  934. get { return _Addresses; }
  935. set { _Addresses = value; }
  936. }
  937. private List<Address> _Addresses = new List<Address>();
  938. [DataMember]
  939. public List<string> strings = new List<string>();
  940. [DataMember]
  941. public Dictionary<string, int> dictionary = new Dictionary<string, int>();
  942. }
  943. [Serializable]
  944. [DataContract]
  945. public class Address
  946. {
  947. [DataMember]
  948. public string Street
  949. {
  950. get { return _street; }
  951. set { _street = value; }
  952. }
  953. private string _street = "32 Kaiea";
  954. [DataMember]
  955. public string Phone
  956. {
  957. get { return _Phone; }
  958. set { _Phone = value; }
  959. }
  960. private string _Phone = "(503) 814-6335";
  961. [DataMember]
  962. public DateTime Entered
  963. {
  964. get { return _Entered; }
  965. set { _Entered = value; }
  966. }
  967. private DateTime _Entered = DateTime.Parse("01/01/2007", CultureInfo.CurrentCulture.DateTimeFormat);
  968. }
  969. [DataContract]
  970. [Serializable]
  971. public class SimpleObject
  972. {
  973. [DataMember]
  974. public int Id { get; set; }
  975. [DataMember]
  976. public string Name { get; set; }
  977. [DataMember]
  978. public string Address { get; set; }
  979. [DataMember]
  980. public int[] Scores { get; set; }
  981. }
  982. #endregion
  983. }
  984. #endif