JsonSerializerTests.cs 4.1 KB

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