JsonSerializerTests.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. using Microsoft.VisualStudio.TestTools.UnitTesting;
  2. using Newtonsoft.Json;
  3. using NTMiner;
  4. using NTMiner.Core;
  5. using NTMiner.Ws;
  6. using System;
  7. using System.Collections.Generic;
  8. namespace NTMiner {
  9. public class ClassA {
  10. public ClassA() { }
  11. public string Property1 { get; set; }
  12. // 必须标记上JsonIgnore才能避免序列化
  13. [JsonIgnore]
  14. public string ReadOnlyProperty1 {
  15. get {
  16. return this.Property1;
  17. }
  18. }
  19. }
  20. [TestClass]
  21. public class JsonSerializerTests {
  22. [TestMethod]
  23. public void ReadOnlyPropertyTest() {
  24. ClassA a = new ClassA {
  25. Property1 = "this is a test"
  26. };
  27. string json = VirtualRoot.JsonSerializer.Serialize(a);
  28. Console.WriteLine(json);
  29. }
  30. [TestMethod]
  31. public void DictonaryTest() {
  32. Dictionary<string, object> dic = new Dictionary<string, object> {
  33. {"A", 1 },
  34. {"B", DateTime.Now },
  35. {"C", "this is a test" }
  36. };
  37. Console.WriteLine(VirtualRoot.JsonSerializer.Serialize(dic));
  38. }
  39. [TestMethod]
  40. public void ConsoleOutLineTest() {
  41. ConsoleOutLine consoleOutLine = new ConsoleOutLine {
  42. Timestamp = Timestamp.GetTimestamp(),
  43. Line = "this is a test"
  44. };
  45. string json = VirtualRoot.JsonSerializer.Serialize(consoleOutLine);
  46. Console.WriteLine(json);
  47. }
  48. [TestMethod]
  49. public void DeserializeTest() {
  50. var dic = VirtualRoot.JsonSerializer.Deserialize<Dictionary<string, object>>(string.Empty);
  51. Assert.IsNull(dic);
  52. dic = VirtualRoot.JsonSerializer.Deserialize<Dictionary<string, object>>("{}");
  53. Assert.IsNotNull(dic);
  54. bool raiseException = false;
  55. try {
  56. dic = VirtualRoot.JsonSerializer.Deserialize<Dictionary<string, object>>("aa");
  57. }
  58. catch {
  59. raiseException = true;
  60. }
  61. finally {
  62. Assert.IsFalse(raiseException);
  63. }
  64. }
  65. [TestMethod]
  66. public void IntPtrTest() {
  67. IntPtr p = new IntPtr(1234567890);
  68. var json = VirtualRoot.JsonSerializer.Serialize(p);
  69. Console.WriteLine(json);
  70. var p1 = VirtualRoot.JsonSerializer.Deserialize<IntPtr>(json);
  71. Console.WriteLine(p1);
  72. }
  73. [TestMethod]
  74. public void ListTest() {
  75. List<string> minerIds = new List<string> {
  76. "aaaaaaa",
  77. "bbbbbbb",
  78. "ccccccc"
  79. };
  80. var json = VirtualRoot.JsonSerializer.Serialize(minerIds);
  81. Assert.AreEqual("[\"aaaaaaa\",\"bbbbbbb\",\"ccccccc\"]", json);
  82. }
  83. [TestMethod]
  84. public void BenchmarkTest() {
  85. int n = 10000;
  86. List<WsMessage> messages = new List<WsMessage>();
  87. for (int i = 0; i < n; i++) {
  88. messages.Add(new WsMessage(Guid.NewGuid(), "test") {
  89. Id = Guid.NewGuid(),
  90. Timestamp = Timestamp.GetTimestamp(),
  91. Sign = Guid.NewGuid().ToString(),
  92. Data = new Dictionary<string, object> {
  93. {"AAAAAAAA", 1 },
  94. {"BBBBBBBB", DateTime.Now },
  95. {"CCCCCCCC", "hello world this is a test" },
  96. {"DDDDDDDD", Guid.NewGuid() }
  97. }
  98. });
  99. }
  100. NTStopwatch.Start();
  101. for (int i = 0; i < messages.Count; i++) {
  102. var message = messages[i];
  103. string json = VirtualRoot.JsonSerializer.Serialize(message);
  104. VirtualRoot.JsonSerializer.Deserialize<WsMessage>(json);
  105. }
  106. var elapsedMilliseconds = NTStopwatch.Stop();
  107. Console.WriteLine(elapsedMilliseconds);
  108. }
  109. }
  110. }