MongoHelper.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. // using System;
  2. // using System.ComponentModel;
  3. // using System.IO;
  4. // using MongoDB.Bson;
  5. // using MongoDB.Bson.IO;
  6. // using MongoDB.Bson.Serialization;
  7. //
  8. // namespace ET
  9. // {
  10. // public static class MongoHelper
  11. // {
  12. // [StaticField]
  13. // private static readonly JsonWriterSettings defaultSettings = new() { OutputMode = JsonOutputMode.RelaxedExtendedJson };
  14. //
  15. // public static string ToJson(object obj)
  16. // {
  17. // if (obj is ISupportInitialize supportInitialize)
  18. // {
  19. // supportInitialize.BeginInit();
  20. // }
  21. // return obj.ToJson(defaultSettings);
  22. // }
  23. //
  24. // public static string ToJson(object obj, JsonWriterSettings settings)
  25. // {
  26. // if (obj is ISupportInitialize supportInitialize)
  27. // {
  28. // supportInitialize.BeginInit();
  29. // }
  30. // return obj.ToJson(settings);
  31. // }
  32. //
  33. // public static T FromJson<T>(string str)
  34. // {
  35. // try
  36. // {
  37. // return BsonSerializer.Deserialize<T>(str);
  38. // }
  39. // catch (Exception e)
  40. // {
  41. // throw new Exception($"{str}\n{e}");
  42. // }
  43. // }
  44. //
  45. // public static object FromJson(Type type, string str)
  46. // {
  47. // return BsonSerializer.Deserialize(str, type);
  48. // }
  49. //
  50. // public static byte[] Serialize(object obj)
  51. // {
  52. // if (obj is ISupportInitialize supportInitialize)
  53. // {
  54. // supportInitialize.BeginInit();
  55. // }
  56. // return obj.ToBson();
  57. // }
  58. //
  59. // public static void Serialize(object message, MemoryStream stream)
  60. // {
  61. // if (message is ISupportInitialize supportInitialize)
  62. // {
  63. // supportInitialize.BeginInit();
  64. // }
  65. //
  66. // using BsonBinaryWriter bsonWriter = new(stream, BsonBinaryWriterSettings.Defaults);
  67. //
  68. // BsonSerializationContext context = BsonSerializationContext.CreateRoot(bsonWriter);
  69. // BsonSerializationArgs args = default;
  70. // args.NominalType = typeof (object);
  71. // IBsonSerializer serializer = BsonSerializer.LookupSerializer(args.NominalType);
  72. // serializer.Serialize(context, args, message);
  73. // }
  74. //
  75. // public static object Deserialize(Type type, byte[] bytes)
  76. // {
  77. // try
  78. // {
  79. // return BsonSerializer.Deserialize(bytes, type);
  80. // }
  81. // catch (Exception e)
  82. // {
  83. // throw new Exception($"from bson error: {type.FullName} {bytes.Length}", e);
  84. // }
  85. // }
  86. //
  87. // public static object Deserialize(Type type, byte[] bytes, int index, int count)
  88. // {
  89. // try
  90. // {
  91. // using MemoryStream memoryStream = new(bytes, index, count);
  92. //
  93. // return BsonSerializer.Deserialize(memoryStream, type);
  94. // }
  95. // catch (Exception e)
  96. // {
  97. // throw new Exception($"from bson error: {type.FullName} {bytes.Length} {index} {count}", e);
  98. // }
  99. // }
  100. //
  101. // public static object Deserialize(Type type, Stream stream)
  102. // {
  103. // try
  104. // {
  105. // return BsonSerializer.Deserialize(stream, type);
  106. // }
  107. // catch (Exception e)
  108. // {
  109. // throw new Exception($"from bson error: {type.FullName} {stream.Position} {stream.Length}", e);
  110. // }
  111. // }
  112. //
  113. // public static T Deserialize<T>(byte[] bytes)
  114. // {
  115. // try
  116. // {
  117. // using MemoryStream memoryStream = new(bytes);
  118. //
  119. // return (T)BsonSerializer.Deserialize(memoryStream, typeof (T));
  120. // }
  121. // catch (Exception e)
  122. // {
  123. // throw new Exception($"from bson error: {typeof (T).FullName} {bytes.Length}", e);
  124. // }
  125. // }
  126. //
  127. // public static T Deserialize<T>(byte[] bytes, int index, int count)
  128. // {
  129. // return (T)Deserialize(typeof (T), bytes, index, count);
  130. // }
  131. //
  132. // public static T Clone<T>(T t)
  133. // {
  134. // return Deserialize<T>(Serialize(t));
  135. // }
  136. // }
  137. // }