BsonExtensionMethods.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /* Copyright 2010-2014 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. return ToBson(obj, typeof(TNominalType), writerSettings, serializer, configurator, args);
  45. }
  46. /// <summary>
  47. /// Serializes an object to a BSON byte array.
  48. /// </summary>
  49. /// <param name="obj">The object.</param>
  50. /// <param name="nominalType">The nominal type of the object..</param>
  51. /// <param name="writerSettings">The writer settings.</param>
  52. /// <param name="serializer">The serializer.</param>
  53. /// <param name="configurator">The serialization context configurator.</param>
  54. /// <param name="args">The serialization args.</param>
  55. /// <returns>A BSON byte array.</returns>
  56. /// <exception cref="System.ArgumentNullException">nominalType</exception>
  57. /// <exception cref="System.ArgumentException">serializer</exception>
  58. public static byte[] ToBson(
  59. this object obj,
  60. Type nominalType,
  61. BsonBinaryWriterSettings writerSettings = null,
  62. IBsonSerializer serializer = null,
  63. Action<BsonSerializationContext.Builder> configurator = null,
  64. BsonSerializationArgs args = default(BsonSerializationArgs))
  65. {
  66. if (nominalType == null)
  67. {
  68. throw new ArgumentNullException("nominalType");
  69. }
  70. if (serializer == null)
  71. {
  72. serializer = BsonSerializer.LookupSerializer(nominalType);
  73. }
  74. if (serializer.ValueType != nominalType)
  75. {
  76. var message = string.Format("Serializer type {0} value type does not match document types {1}.", serializer.GetType().FullName, nominalType.FullName);
  77. throw new ArgumentException(message, "serializer");
  78. }
  79. using (var memoryStream = new MemoryStream())
  80. {
  81. using (var bsonWriter = new BsonBinaryWriter(memoryStream, writerSettings ?? BsonBinaryWriterSettings.Defaults))
  82. {
  83. var context = BsonSerializationContext.CreateRoot(bsonWriter, configurator);
  84. args.NominalType = nominalType;
  85. serializer.Serialize(context, args, obj);
  86. }
  87. return memoryStream.ToArray();
  88. }
  89. }
  90. /// <summary>
  91. /// Serializes an object to a BsonDocument.
  92. /// </summary>
  93. /// <typeparam name="TNominalType">The nominal type of the object.</typeparam>
  94. /// <param name="obj">The object.</param>
  95. /// <param name="serializer">The serializer.</param>
  96. /// <param name="configurator">The serialization context configurator.</param>
  97. /// <param name="args">The serialization args.</param>
  98. /// <returns>A BsonDocument.</returns>
  99. public static BsonDocument ToBsonDocument<TNominalType>(
  100. this TNominalType obj,
  101. IBsonSerializer<TNominalType> serializer = null,
  102. Action<BsonSerializationContext.Builder> configurator = null,
  103. BsonSerializationArgs args = default(BsonSerializationArgs))
  104. {
  105. return ToBsonDocument(obj, typeof(TNominalType), serializer, configurator, args);
  106. }
  107. /// <summary>
  108. /// Serializes an object to a BsonDocument.
  109. /// </summary>
  110. /// <param name="obj">The object.</param>
  111. /// <param name="nominalType">The nominal type of the object.</param>
  112. /// <param name="serializer">The serializer.</param>
  113. /// <param name="configurator">The serialization context configurator.</param>
  114. /// <param name="args">The serialization args.</param>
  115. /// <returns>A BsonDocument.</returns>
  116. /// <exception cref="System.ArgumentNullException">nominalType</exception>
  117. /// <exception cref="System.ArgumentException">serializer</exception>
  118. public static BsonDocument ToBsonDocument(
  119. this object obj,
  120. Type nominalType,
  121. IBsonSerializer serializer = null,
  122. Action<BsonSerializationContext.Builder> configurator = null,
  123. BsonSerializationArgs args = default(BsonSerializationArgs))
  124. {
  125. if (nominalType == null)
  126. {
  127. throw new ArgumentNullException("nominalType");
  128. }
  129. if (obj == null)
  130. {
  131. return null;
  132. }
  133. if (serializer == null)
  134. {
  135. var bsonDocument = obj as BsonDocument;
  136. if (bsonDocument != null)
  137. {
  138. return bsonDocument; // it's already a BsonDocument
  139. }
  140. var convertibleToBsonDocument = obj as IConvertibleToBsonDocument;
  141. if (convertibleToBsonDocument != null)
  142. {
  143. return convertibleToBsonDocument.ToBsonDocument(); // use the provided ToBsonDocument method
  144. }
  145. serializer = BsonSerializer.LookupSerializer(nominalType);
  146. }
  147. if (serializer.ValueType != nominalType)
  148. {
  149. var message = string.Format("Serializer type {0} value type does not match document types {1}.", serializer.GetType().FullName, nominalType.FullName);
  150. throw new ArgumentException(message, "serializer");
  151. }
  152. // otherwise serialize into a new BsonDocument
  153. var document = new BsonDocument();
  154. using (var bsonWriter = new BsonDocumentWriter(document))
  155. {
  156. var context = BsonSerializationContext.CreateRoot(bsonWriter, configurator);
  157. args.NominalType = nominalType;
  158. serializer.Serialize(context, args, obj);
  159. }
  160. return document;
  161. }
  162. /// <summary>
  163. /// Serializes an object to a JSON string.
  164. /// </summary>
  165. /// <typeparam name="TNominalType">The nominal type of the object.</typeparam>
  166. /// <param name="obj">The object.</param>
  167. /// <param name="writerSettings">The JsonWriter settings.</param>
  168. /// <param name="serializer">The serializer.</param>
  169. /// <param name="configurator">The serializastion context configurator.</param>
  170. /// <param name="args">The serialization args.</param>
  171. /// <returns>
  172. /// A JSON string.
  173. /// </returns>
  174. public static string ToJson<TNominalType>(
  175. this TNominalType obj,
  176. JsonWriterSettings writerSettings = null,
  177. IBsonSerializer<TNominalType> serializer = null,
  178. Action<BsonSerializationContext.Builder> configurator = null,
  179. BsonSerializationArgs args = default(BsonSerializationArgs))
  180. {
  181. return ToJson(obj, typeof(TNominalType), writerSettings, serializer, configurator, args);
  182. }
  183. /// <summary>
  184. /// Serializes an object to a JSON string.
  185. /// </summary>
  186. /// <param name="obj">The object.</param>
  187. /// <param name="nominalType">The nominal type of the objectt.</param>
  188. /// <param name="writerSettings">The JsonWriter settings.</param>
  189. /// <param name="serializer">The serializer.</param>
  190. /// <param name="configurator">The serialization context configurator.</param>
  191. /// <param name="args">The serialization args.</param>
  192. /// <returns>
  193. /// A JSON string.
  194. /// </returns>
  195. /// <exception cref="System.ArgumentNullException">nominalType</exception>
  196. /// <exception cref="System.ArgumentException">serializer</exception>
  197. public static string ToJson(
  198. this object obj,
  199. Type nominalType,
  200. JsonWriterSettings writerSettings = null,
  201. IBsonSerializer serializer = null,
  202. Action<BsonSerializationContext.Builder> configurator = null,
  203. BsonSerializationArgs args = default(BsonSerializationArgs))
  204. {
  205. if (nominalType == null)
  206. {
  207. throw new ArgumentNullException("nominalType");
  208. }
  209. if (serializer == null)
  210. {
  211. serializer = BsonSerializer.LookupSerializer(nominalType);
  212. }
  213. if (serializer.ValueType != nominalType)
  214. {
  215. var message = string.Format("Serializer type {0} value type does not match document types {1}.", serializer.GetType().FullName, nominalType.FullName);
  216. throw new ArgumentException(message, "serializer");
  217. }
  218. using (var stringWriter = new StringWriter())
  219. {
  220. using (var bsonWriter = new JsonWriter(stringWriter, writerSettings ?? JsonWriterSettings.Defaults))
  221. {
  222. var context = BsonSerializationContext.CreateRoot(bsonWriter, configurator);
  223. args.NominalType = nominalType;
  224. serializer.Serialize(context, args, obj);
  225. }
  226. return stringWriter.ToString();
  227. }
  228. }
  229. }
  230. }