BsonExtensionMethods.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /* Copyright 2010-present MongoDB Inc.
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. using System;
  16. using System.IO;
  17. using MongoDB.Bson.IO;
  18. using MongoDB.Bson.Serialization;
  19. namespace MongoDB.Bson
  20. {
  21. /// <summary>
  22. /// A static class containing BSON extension methods.
  23. /// </summary>
  24. public static class BsonExtensionMethods
  25. {
  26. /// <summary>
  27. /// Serializes an object to a BSON byte array.
  28. /// </summary>
  29. /// <typeparam name="TNominalType">The nominal type of the object.</typeparam>
  30. /// <param name="obj">The object.</param>
  31. /// <param name="serializer">The serializer.</param>
  32. /// <param name="writerSettings">The writer settings.</param>
  33. /// <param name="configurator">The serialization context configurator.</param>
  34. /// <param name="args">The serialization args.</param>
  35. /// <returns>A BSON byte array.</returns>
  36. public static byte[] ToBson<TNominalType>(
  37. this TNominalType obj,
  38. IBsonSerializer<TNominalType> serializer = null,
  39. BsonBinaryWriterSettings writerSettings = null,
  40. Action<BsonSerializationContext.Builder> configurator = null,
  41. BsonSerializationArgs args = default(BsonSerializationArgs)
  42. )
  43. {
  44. args.SetOrValidateNominalType(typeof(TNominalType), "<TNominalType>");
  45. return ToBson(obj, typeof(TNominalType), writerSettings, serializer, configurator, args);
  46. }
  47. /// <summary>
  48. /// Serializes an object to a BSON byte array.
  49. /// </summary>
  50. /// <param name="obj">The object.</param>
  51. /// <param name="nominalType">The nominal type of the object..</param>
  52. /// <param name="writerSettings">The writer settings.</param>
  53. /// <param name="serializer">The serializer.</param>
  54. /// <param name="configurator">The serialization context configurator.</param>
  55. /// <param name="args">The serialization args.</param>
  56. /// <returns>A BSON byte array.</returns>
  57. /// <exception cref="System.ArgumentNullException">nominalType</exception>
  58. /// <exception cref="System.ArgumentException">serializer</exception>
  59. public static byte[] ToBson(
  60. this object obj,
  61. Type nominalType,
  62. BsonBinaryWriterSettings writerSettings = null,
  63. IBsonSerializer serializer = null,
  64. Action<BsonSerializationContext.Builder> configurator = null,
  65. BsonSerializationArgs args = default(BsonSerializationArgs))
  66. {
  67. if (nominalType == null)
  68. {
  69. throw new ArgumentNullException("nominalType");
  70. }
  71. args.SetOrValidateNominalType(nominalType, "nominalType");
  72. if (serializer == null)
  73. {
  74. serializer = BsonSerializer.LookupSerializer(nominalType);
  75. }
  76. if (serializer.ValueType != nominalType)
  77. {
  78. var message = string.Format("Serializer type {0} value type does not match document types {1}.", serializer.GetType().FullName, nominalType.FullName);
  79. throw new ArgumentException(message, "serializer");
  80. }
  81. using (var memoryStream = new MemoryStream())
  82. {
  83. using (var bsonWriter = new BsonBinaryWriter(memoryStream, writerSettings ?? BsonBinaryWriterSettings.Defaults))
  84. {
  85. var context = BsonSerializationContext.CreateRoot(bsonWriter, configurator);
  86. serializer.Serialize(context, args, obj);
  87. }
  88. return memoryStream.ToArray();
  89. }
  90. }
  91. /// <summary>
  92. /// Serializes an object to a BsonDocument.
  93. /// </summary>
  94. /// <typeparam name="TNominalType">The nominal type of the object.</typeparam>
  95. /// <param name="obj">The object.</param>
  96. /// <param name="serializer">The serializer.</param>
  97. /// <param name="configurator">The serialization context configurator.</param>
  98. /// <param name="args">The serialization args.</param>
  99. /// <returns>A BsonDocument.</returns>
  100. public static BsonDocument ToBsonDocument<TNominalType>(
  101. this TNominalType obj,
  102. IBsonSerializer<TNominalType> serializer = null,
  103. Action<BsonSerializationContext.Builder> configurator = null,
  104. BsonSerializationArgs args = default(BsonSerializationArgs))
  105. {
  106. args.SetOrValidateNominalType(typeof(TNominalType), "<TNominalType>");
  107. return ToBsonDocument(obj, typeof(TNominalType), serializer, configurator, args);
  108. }
  109. /// <summary>
  110. /// Serializes an object to a BsonDocument.
  111. /// </summary>
  112. /// <param name="obj">The object.</param>
  113. /// <param name="nominalType">The nominal type of the object.</param>
  114. /// <param name="serializer">The serializer.</param>
  115. /// <param name="configurator">The serialization context configurator.</param>
  116. /// <param name="args">The serialization args.</param>
  117. /// <returns>A BsonDocument.</returns>
  118. /// <exception cref="System.ArgumentNullException">nominalType</exception>
  119. /// <exception cref="System.ArgumentException">serializer</exception>
  120. public static BsonDocument ToBsonDocument(
  121. this object obj,
  122. Type nominalType,
  123. IBsonSerializer serializer = null,
  124. Action<BsonSerializationContext.Builder> configurator = null,
  125. BsonSerializationArgs args = default(BsonSerializationArgs))
  126. {
  127. if (nominalType == null)
  128. {
  129. throw new ArgumentNullException("nominalType");
  130. }
  131. args.SetOrValidateNominalType(nominalType, "nominalType");
  132. if (obj == null)
  133. {
  134. return null;
  135. }
  136. if (serializer == null)
  137. {
  138. var bsonDocument = obj as BsonDocument;
  139. if (bsonDocument != null)
  140. {
  141. return bsonDocument; // it's already a BsonDocument
  142. }
  143. var convertibleToBsonDocument = obj as IConvertibleToBsonDocument;
  144. if (convertibleToBsonDocument != null)
  145. {
  146. return convertibleToBsonDocument.ToBsonDocument(); // use the provided ToBsonDocument method
  147. }
  148. serializer = BsonSerializer.LookupSerializer(nominalType);
  149. }
  150. if (serializer.ValueType != nominalType)
  151. {
  152. var message = string.Format("Serializer type {0} value type does not match document types {1}.", serializer.GetType().FullName, nominalType.FullName);
  153. throw new ArgumentException(message, "serializer");
  154. }
  155. // otherwise serialize into a new BsonDocument
  156. var document = new BsonDocument();
  157. using (var bsonWriter = new BsonDocumentWriter(document))
  158. {
  159. var context = BsonSerializationContext.CreateRoot(bsonWriter, configurator);
  160. serializer.Serialize(context, args, obj);
  161. }
  162. return document;
  163. }
  164. /// <summary>
  165. /// Serializes an object to a JSON string.
  166. /// </summary>
  167. /// <typeparam name="TNominalType">The nominal type of the object.</typeparam>
  168. /// <param name="obj">The object.</param>
  169. /// <param name="writerSettings">The JsonWriter settings.</param>
  170. /// <param name="serializer">The serializer.</param>
  171. /// <param name="configurator">The serializastion context configurator.</param>
  172. /// <param name="args">The serialization args.</param>
  173. /// <returns>
  174. /// A JSON string.
  175. /// </returns>
  176. public static string ToJson<TNominalType>(
  177. this TNominalType obj,
  178. JsonWriterSettings writerSettings = null,
  179. IBsonSerializer<TNominalType> serializer = null,
  180. Action<BsonSerializationContext.Builder> configurator = null,
  181. BsonSerializationArgs args = default(BsonSerializationArgs))
  182. {
  183. args.SetOrValidateNominalType(typeof(TNominalType), "<TNominalType>");
  184. return ToJson(obj, typeof(TNominalType), writerSettings, serializer, configurator, args);
  185. }
  186. /// <summary>
  187. /// Serializes an object to a JSON string.
  188. /// </summary>
  189. /// <param name="obj">The object.</param>
  190. /// <param name="nominalType">The nominal type of the objectt.</param>
  191. /// <param name="writerSettings">The JsonWriter settings.</param>
  192. /// <param name="serializer">The serializer.</param>
  193. /// <param name="configurator">The serialization context configurator.</param>
  194. /// <param name="args">The serialization args.</param>
  195. /// <returns>
  196. /// A JSON string.
  197. /// </returns>
  198. /// <exception cref="System.ArgumentNullException">nominalType</exception>
  199. /// <exception cref="System.ArgumentException">serializer</exception>
  200. public static string ToJson(
  201. this object obj,
  202. Type nominalType,
  203. JsonWriterSettings writerSettings = null,
  204. IBsonSerializer serializer = null,
  205. Action<BsonSerializationContext.Builder> configurator = null,
  206. BsonSerializationArgs args = default(BsonSerializationArgs))
  207. {
  208. if (nominalType == null)
  209. {
  210. throw new ArgumentNullException("nominalType");
  211. }
  212. args.SetOrValidateNominalType(nominalType, "nominalType");
  213. if (serializer == null)
  214. {
  215. serializer = BsonSerializer.LookupSerializer(nominalType);
  216. }
  217. if (serializer.ValueType != nominalType)
  218. {
  219. var message = string.Format("Serializer type {0} value type does not match document types {1}.", serializer.GetType().FullName, nominalType.FullName);
  220. throw new ArgumentException(message, "serializer");
  221. }
  222. using (var stringWriter = new StringWriter())
  223. {
  224. using (var bsonWriter = new JsonWriter(stringWriter, writerSettings ?? JsonWriterSettings.Defaults))
  225. {
  226. var context = BsonSerializationContext.CreateRoot(bsonWriter, configurator);
  227. serializer.Serialize(context, args, obj);
  228. }
  229. return stringWriter.ToString();
  230. }
  231. }
  232. }
  233. }