MongoHelper.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using MongoDB.Bson;
  5. using MongoDB.Bson.IO;
  6. using MongoDB.Bson.Serialization;
  7. using MongoDB.Bson.Serialization.Conventions;
  8. using MongoDB.Bson.Serialization.Serializers;
  9. using UnityEngine;
  10. namespace ET
  11. {
  12. public static class MongoHelper
  13. {
  14. public static string ToJson(object obj)
  15. {
  16. return obj.ToJson();
  17. }
  18. public static string ToJson(object obj, JsonWriterSettings settings)
  19. {
  20. return obj.ToJson(settings);
  21. }
  22. public static T FromJson<T>(string str)
  23. {
  24. try
  25. {
  26. return BsonSerializer.Deserialize<T>(str);
  27. }
  28. catch (Exception e)
  29. {
  30. throw new Exception($"{str}\n{e}");
  31. }
  32. }
  33. public static object FromJson(Type type, string str)
  34. {
  35. return BsonSerializer.Deserialize(str, type);
  36. }
  37. public static byte[] ToBson(object obj)
  38. {
  39. return obj.ToBson();
  40. }
  41. public static void ToStream(object message, MemoryStream stream)
  42. {
  43. using (BsonBinaryWriter bsonWriter = new BsonBinaryWriter(stream, BsonBinaryWriterSettings.Defaults))
  44. {
  45. BsonSerializationContext context = BsonSerializationContext.CreateRoot(bsonWriter);
  46. BsonSerializationArgs args = default;
  47. args.NominalType = typeof (object);
  48. IBsonSerializer serializer = BsonSerializer.LookupSerializer(args.NominalType);
  49. serializer.Serialize(context, args, message);
  50. }
  51. }
  52. public static object FromBson(Type type, byte[] bytes)
  53. {
  54. try
  55. {
  56. return BsonSerializer.Deserialize(bytes, type);
  57. }
  58. catch (Exception e)
  59. {
  60. throw new Exception($"from bson error: {type.Name}", e);
  61. }
  62. }
  63. public static object FromBson(Type type, byte[] bytes, int index, int count)
  64. {
  65. try
  66. {
  67. using (MemoryStream memoryStream = new MemoryStream(bytes, index, count))
  68. {
  69. return BsonSerializer.Deserialize(memoryStream, type);
  70. }
  71. }
  72. catch (Exception e)
  73. {
  74. throw new Exception($"from bson error: {type.Name}", e);
  75. }
  76. }
  77. public static object FromStream(Type type, Stream stream)
  78. {
  79. try
  80. {
  81. return BsonSerializer.Deserialize(stream, type);
  82. }
  83. catch (Exception e)
  84. {
  85. throw new Exception($"from bson error: {type.Name}", e);
  86. }
  87. }
  88. public static T FromBson<T>(byte[] bytes)
  89. {
  90. try
  91. {
  92. using (MemoryStream memoryStream = new MemoryStream(bytes))
  93. {
  94. return (T) BsonSerializer.Deserialize(memoryStream, typeof (T));
  95. }
  96. }
  97. catch (Exception e)
  98. {
  99. throw new Exception($"from bson error: {typeof (T).Name}", e);
  100. }
  101. }
  102. public static T FromBson<T>(byte[] bytes, int index, int count)
  103. {
  104. return (T) FromBson(typeof (T), bytes, index, count);
  105. }
  106. public static T Clone<T>(T t)
  107. {
  108. return FromBson<T>(ToBson(t));
  109. }
  110. }
  111. }