BsonExtensionMethods.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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. /// Converts an object to a BSON document byte array.
  28. /// </summary>
  29. /// <typeparam name="TNominalType">The nominal type of the object.</typeparam>
  30. /// <param name="obj">The object.</param>
  31. /// <returns>A byte array.</returns>
  32. public static byte[] ToBson<TNominalType>(this TNominalType obj)
  33. {
  34. return ToBson(obj, typeof(TNominalType));
  35. }
  36. /// <summary>
  37. /// Converts an object to a BSON document byte array.
  38. /// </summary>
  39. /// <typeparam name="TNominalType">The nominal type of the object.</typeparam>
  40. /// <param name="obj">The object.</param>
  41. /// <param name="options">The serialization options.</param>
  42. /// <returns>A byte array.</returns>
  43. public static byte[] ToBson<TNominalType>(this TNominalType obj, IBsonSerializationOptions options)
  44. {
  45. return ToBson(obj, typeof(TNominalType), options);
  46. }
  47. /// <summary>
  48. /// Converts an object to a BSON document byte array.
  49. /// </summary>
  50. /// <typeparam name="TNominalType">The nominal type of the object.</typeparam>
  51. /// <param name="obj">The object.</param>
  52. /// <param name="options">The serialization options.</param>
  53. /// <param name="settings">The BsonBinaryWriter settings.</param>
  54. /// <returns>A byte array.</returns>
  55. public static byte[] ToBson<TNominalType>(
  56. this TNominalType obj,
  57. IBsonSerializationOptions options,
  58. BsonBinaryWriterSettings settings)
  59. {
  60. return ToBson(obj, typeof(TNominalType), options, settings);
  61. }
  62. /// <summary>
  63. /// Converts an object to a BSON document byte array.
  64. /// </summary>
  65. /// <typeparam name="TNominalType">The nominal type of the object.</typeparam>
  66. /// <param name="obj">The object.</param>
  67. /// <param name="settings">The BsonBinaryWriter settings.</param>
  68. /// <returns>A byte array.</returns>
  69. public static byte[] ToBson<TNominalType>(this TNominalType obj, BsonBinaryWriterSettings settings)
  70. {
  71. return ToBson(obj, typeof(TNominalType), settings);
  72. }
  73. /// <summary>
  74. /// Converts an object to a BSON document byte array.
  75. /// </summary>
  76. /// <param name="obj">The object.</param>
  77. /// <param name="nominalType">The nominal type of the object.</param>
  78. /// <returns>A byte array.</returns>
  79. public static byte[] ToBson(this object obj, Type nominalType)
  80. {
  81. return ToBson(obj, nominalType, BsonBinaryWriterSettings.Defaults);
  82. }
  83. /// <summary>
  84. /// Converts an object to a BSON document byte array.
  85. /// </summary>
  86. /// <param name="obj">The object.</param>
  87. /// <param name="nominalType">The nominal type of the object.</param>
  88. /// <param name="options">The serialization options.</param>
  89. /// <returns>A byte array.</returns>
  90. public static byte[] ToBson(this object obj, Type nominalType, IBsonSerializationOptions options)
  91. {
  92. return ToBson(obj, nominalType, options, BsonBinaryWriterSettings.Defaults);
  93. }
  94. /// <summary>
  95. /// Converts an object to a BSON document byte array.
  96. /// </summary>
  97. /// <param name="obj">The object.</param>
  98. /// <param name="nominalType">The nominal type of the object.</param>
  99. /// <param name="options">The serialization options.</param>
  100. /// <param name="settings">The BsonBinaryWriter settings.</param>
  101. /// <returns>A byte array.</returns>
  102. public static byte[] ToBson(
  103. this object obj,
  104. Type nominalType,
  105. IBsonSerializationOptions options,
  106. BsonBinaryWriterSettings settings)
  107. {
  108. using (var buffer = new BsonBuffer())
  109. {
  110. using (var bsonWriter = BsonWriter.Create(buffer, settings))
  111. {
  112. BsonSerializer.Serialize(bsonWriter, nominalType, obj, options);
  113. }
  114. return buffer.ToByteArray();
  115. }
  116. }
  117. /// <summary>
  118. /// Converts an object to a BSON document byte array.
  119. /// </summary>
  120. /// <param name="obj">The object.</param>
  121. /// <param name="nominalType">The nominal type of the object.</param>
  122. /// <param name="settings">The BsonBinaryWriter settings.</param>
  123. /// <returns>A byte array.</returns>
  124. public static byte[] ToBson(this object obj, Type nominalType, BsonBinaryWriterSettings settings)
  125. {
  126. return ToBson(obj, nominalType, null, settings);
  127. }
  128. /// <summary>
  129. /// Converts an object to a BsonDocument.
  130. /// </summary>
  131. /// <typeparam name="TNominalType">The nominal type of the object.</typeparam>
  132. /// <param name="obj">The object.</param>
  133. /// <returns>A BsonDocument.</returns>
  134. public static BsonDocument ToBsonDocument<TNominalType>(this TNominalType obj)
  135. {
  136. return ToBsonDocument(obj, typeof(TNominalType));
  137. }
  138. /// <summary>
  139. /// Converts an object to a BsonDocument.
  140. /// </summary>
  141. /// <typeparam name="TNominalType">The nominal type of the object.</typeparam>
  142. /// <param name="obj">The object.</param>
  143. /// <param name="options">The serialization options.</param>
  144. /// <returns>A BsonDocument.</returns>
  145. public static BsonDocument ToBsonDocument<TNominalType>(
  146. this TNominalType obj,
  147. IBsonSerializationOptions options)
  148. {
  149. return ToBsonDocument(obj, typeof(TNominalType), options);
  150. }
  151. /// <summary>
  152. /// Converts an object to a BsonDocument.
  153. /// </summary>
  154. /// <param name="obj">The object.</param>
  155. /// <param name="nominalType">The nominal type of the object.</param>
  156. /// <returns>A BsonDocument.</returns>
  157. public static BsonDocument ToBsonDocument(this object obj, Type nominalType)
  158. {
  159. return ToBsonDocument(obj, nominalType, null);
  160. }
  161. /// <summary>
  162. /// Converts an object to a BsonDocument.
  163. /// </summary>
  164. /// <param name="obj">The object.</param>
  165. /// <param name="nominalType">The nominal type of the object.</param>
  166. /// <param name="options">The serialization options.</param>
  167. /// <returns>A BsonDocument.</returns>
  168. public static BsonDocument ToBsonDocument(
  169. this object obj,
  170. Type nominalType,
  171. IBsonSerializationOptions options)
  172. {
  173. if (obj == null)
  174. {
  175. return null;
  176. }
  177. var bsonDocument = obj as BsonDocument;
  178. if (bsonDocument != null)
  179. {
  180. return bsonDocument; // it's already a BsonDocument
  181. }
  182. var convertibleToBsonDocument = obj as IConvertibleToBsonDocument;
  183. if (convertibleToBsonDocument != null)
  184. {
  185. return convertibleToBsonDocument.ToBsonDocument(); // use the provided ToBsonDocument method
  186. }
  187. // otherwise serialize into a new BsonDocument
  188. var document = new BsonDocument();
  189. using (var writer = BsonWriter.Create(document))
  190. {
  191. BsonSerializer.Serialize(writer, nominalType, obj, options);
  192. }
  193. return document;
  194. }
  195. /// <summary>
  196. /// Converts an object to a JSON string.
  197. /// </summary>
  198. /// <typeparam name="TNominalType">The nominal type of the object.</typeparam>
  199. /// <param name="obj">The object.</param>
  200. /// <returns>A JSON string.</returns>
  201. public static string ToJson<TNominalType>(this TNominalType obj)
  202. {
  203. return ToJson(obj, typeof(TNominalType));
  204. }
  205. /// <summary>
  206. /// Converts an object to a JSON string.
  207. /// </summary>
  208. /// <typeparam name="TNominalType">The nominal type of the object.</typeparam>
  209. /// <param name="obj">The object.</param>
  210. /// <param name="options">The serialization options.</param>
  211. /// <returns>A JSON string.</returns>
  212. public static string ToJson<TNominalType>(this TNominalType obj, IBsonSerializationOptions options)
  213. {
  214. return ToJson(obj, typeof(TNominalType), options);
  215. }
  216. /// <summary>
  217. /// Converts an object to a JSON string.
  218. /// </summary>
  219. /// <typeparam name="TNominalType">The nominal type of the object.</typeparam>
  220. /// <param name="obj">The object.</param>
  221. /// <param name="options">The serialization options.</param>
  222. /// <param name="settings">The JsonWriter settings.</param>
  223. /// <returns>A JSON string.</returns>
  224. public static string ToJson<TNominalType>(
  225. this TNominalType obj,
  226. IBsonSerializationOptions options,
  227. JsonWriterSettings settings)
  228. {
  229. return ToJson(obj, typeof(TNominalType), options, settings);
  230. }
  231. /// <summary>
  232. /// Converts an object to a JSON string.
  233. /// </summary>
  234. /// <typeparam name="TNominalType">The nominal type of the object.</typeparam>
  235. /// <param name="obj">The object.</param>
  236. /// <param name="settings">The JsonWriter settings.</param>
  237. /// <returns>A JSON string.</returns>
  238. public static string ToJson<TNominalType>(this TNominalType obj, JsonWriterSettings settings)
  239. {
  240. return ToJson(obj, typeof(TNominalType), settings);
  241. }
  242. /// <summary>
  243. /// Converts an object to a JSON string.
  244. /// </summary>
  245. /// <param name="obj">The object.</param>
  246. /// <param name="nominalType">The nominal type of the object.</param>
  247. /// <returns>A JSON string.</returns>
  248. public static string ToJson(this object obj, Type nominalType)
  249. {
  250. return ToJson(obj, nominalType, JsonWriterSettings.Defaults);
  251. }
  252. /// <summary>
  253. /// Converts an object to a JSON string.
  254. /// </summary>
  255. /// <param name="obj">The object.</param>
  256. /// <param name="nominalType">The nominal type of the object.</param>
  257. /// <param name="options">The serialization options.</param>
  258. /// <returns>A JSON string.</returns>
  259. public static string ToJson(this object obj, Type nominalType, IBsonSerializationOptions options)
  260. {
  261. return ToJson(obj, nominalType, options, JsonWriterSettings.Defaults);
  262. }
  263. /// <summary>
  264. /// Converts an object to a JSON string.
  265. /// </summary>
  266. /// <param name="obj">The object.</param>
  267. /// <param name="nominalType">The nominal type of the object.</param>
  268. /// <param name="options">The serialization options.</param>
  269. /// <param name="settings">The JsonWriter settings.</param>
  270. /// <returns>A JSON string.</returns>
  271. public static string ToJson(
  272. this object obj,
  273. Type nominalType,
  274. IBsonSerializationOptions options,
  275. JsonWriterSettings settings)
  276. {
  277. using (var stringWriter = new StringWriter())
  278. {
  279. using (var bsonWriter = BsonWriter.Create(stringWriter, settings))
  280. {
  281. BsonSerializer.Serialize(bsonWriter, nominalType, obj, options);
  282. }
  283. return stringWriter.ToString();
  284. }
  285. }
  286. /// <summary>
  287. /// Converts an object to a JSON string.
  288. /// </summary>
  289. /// <param name="obj">The object.</param>
  290. /// <param name="nominalType">The nominal type of the object.</param>
  291. /// <param name="settings">The JsonWriter settings.</param>
  292. /// <returns>A JSON string.</returns>
  293. public static string ToJson(this object obj, Type nominalType, JsonWriterSettings settings)
  294. {
  295. return ToJson(obj, nominalType, null, settings);
  296. }
  297. }
  298. }