IBsonWriterExtensions.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. /* Copyright 2010-2016 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. namespace MongoDB.Bson.IO
  16. {
  17. /// <summary>
  18. /// Contains extension methods for IBsonWriter.
  19. /// </summary>
  20. public static class IBsonWriterExtensions
  21. {
  22. /// <summary>
  23. /// Writes a BSON binary data element to the writer.
  24. /// </summary>
  25. /// <param name="writer">The writer.</param>
  26. /// <param name="name">The name of the element.</param>
  27. /// <param name="binaryData">The binary data.</param>
  28. public static void WriteBinaryData(this IBsonWriter writer, string name, BsonBinaryData binaryData)
  29. {
  30. writer.WriteName(name);
  31. writer.WriteBinaryData(binaryData);
  32. }
  33. /// <summary>
  34. /// Writes a BSON Boolean element to the writer.
  35. /// </summary>
  36. /// <param name="writer">The writer.</param>
  37. /// <param name="name">The name of the element.</param>
  38. /// <param name="value">The Boolean value.</param>
  39. public static void WriteBoolean(this IBsonWriter writer, string name, bool value)
  40. {
  41. writer.WriteName(name);
  42. writer.WriteBoolean(value);
  43. }
  44. /// <summary>
  45. /// Writes a BSON binary data element to the writer.
  46. /// </summary>
  47. /// <param name="writer">The writer.</param>
  48. /// <param name="name">The name of the element.</param>
  49. /// <param name="bytes">The bytes.</param>
  50. public static void WriteBytes(this IBsonWriter writer, string name, byte[] bytes)
  51. {
  52. writer.WriteName(name);
  53. writer.WriteBytes(bytes);
  54. }
  55. /// <summary>
  56. /// Writes a BSON DateTime element to the writer.
  57. /// </summary>
  58. /// <param name="writer">The writer.</param>
  59. /// <param name="name">The name of the element.</param>
  60. /// <param name="value">The number of milliseconds since the Unix epoch.</param>
  61. public static void WriteDateTime(this IBsonWriter writer, string name, long value)
  62. {
  63. writer.WriteName(name);
  64. writer.WriteDateTime(value);
  65. }
  66. /// <summary>
  67. /// Writes a BSON Decimal128 element to the writer.
  68. /// </summary>
  69. /// <param name="writer">The writer.</param>
  70. /// <param name="name">The name of the element.</param>
  71. /// <param name="value">The <see cref="Decimal128"/> value.</param>
  72. public static void WriteDecimal128(this IBsonWriter writer, string name, Decimal128 value)
  73. {
  74. writer.WriteName(name);
  75. writer.WriteDecimal128(value);
  76. }
  77. /// <summary>
  78. /// Writes a BSON Double element to the writer.
  79. /// </summary>
  80. /// <param name="writer">The writer.</param>
  81. /// <param name="name">The name of the element.</param>
  82. /// <param name="value">The Double value.</param>
  83. public static void WriteDouble(this IBsonWriter writer, string name, double value)
  84. {
  85. writer.WriteName(name);
  86. writer.WriteDouble(value);
  87. }
  88. /// <summary>
  89. /// Writes a BSON Int32 element to the writer.
  90. /// </summary>
  91. /// <param name="writer">The writer.</param>
  92. /// <param name="name">The name of the element.</param>
  93. /// <param name="value">The Int32 value.</param>
  94. public static void WriteInt32(this IBsonWriter writer, string name, int value)
  95. {
  96. writer.WriteName(name);
  97. writer.WriteInt32(value);
  98. }
  99. /// <summary>
  100. /// Writes a BSON Int64 element to the writer.
  101. /// </summary>
  102. /// <param name="writer">The writer.</param>
  103. /// <param name="name">The name of the element.</param>
  104. /// <param name="value">The Int64 value.</param>
  105. public static void WriteInt64(this IBsonWriter writer, string name, long value)
  106. {
  107. writer.WriteName(name);
  108. writer.WriteInt64(value);
  109. }
  110. /// <summary>
  111. /// Writes a BSON JavaScript element to the writer.
  112. /// </summary>
  113. /// <param name="writer">The writer.</param>
  114. /// <param name="name">The name of the element.</param>
  115. /// <param name="code">The JavaScript code.</param>
  116. public static void WriteJavaScript(this IBsonWriter writer, string name, string code)
  117. {
  118. writer.WriteName(name);
  119. writer.WriteJavaScript(code);
  120. }
  121. /// <summary>
  122. /// Writes a BSON JavaScript element to the writer (call WriteStartDocument to start writing the scope).
  123. /// </summary>
  124. /// <param name="writer">The writer.</param>
  125. /// <param name="name">The name of the element.</param>
  126. /// <param name="code">The JavaScript code.</param>
  127. public static void WriteJavaScriptWithScope(this IBsonWriter writer, string name, string code)
  128. {
  129. writer.WriteName(name);
  130. writer.WriteJavaScriptWithScope(code);
  131. }
  132. /// <summary>
  133. /// Writes a BSON MaxKey element to the writer.
  134. /// </summary>
  135. /// <param name="writer">The writer.</param>
  136. /// <param name="name">The name of the element.</param>
  137. public static void WriteMaxKey(this IBsonWriter writer, string name)
  138. {
  139. writer.WriteName(name);
  140. writer.WriteMaxKey();
  141. }
  142. /// <summary>
  143. /// Writes a BSON MinKey element to the writer.
  144. /// </summary>
  145. /// <param name="writer">The writer.</param>
  146. /// <param name="name">The name of the element.</param>
  147. public static void WriteMinKey(this IBsonWriter writer, string name)
  148. {
  149. writer.WriteName(name);
  150. writer.WriteMinKey();
  151. }
  152. /// <summary>
  153. /// Writes a BSON null element to the writer.
  154. /// </summary>
  155. /// <param name="writer">The writer.</param>
  156. /// <param name="name">The name of the element.</param>
  157. public static void WriteNull(this IBsonWriter writer, string name)
  158. {
  159. writer.WriteName(name);
  160. writer.WriteNull();
  161. }
  162. /// <summary>
  163. /// Writes a BSON ObjectId element to the writer.
  164. /// </summary>
  165. /// <param name="writer">The writer.</param>
  166. /// <param name="name">The name of the element.</param>
  167. /// <param name="objectId">The ObjectId.</param>
  168. public static void WriteObjectId(this IBsonWriter writer, string name, ObjectId objectId)
  169. {
  170. writer.WriteName(name);
  171. writer.WriteObjectId(objectId);
  172. }
  173. /// <summary>
  174. /// Writes a raw BSON array.
  175. /// </summary>
  176. /// <param name="writer">The writer.</param>
  177. /// <param name="name">The name.</param>
  178. /// <param name="slice">The byte buffer containing the raw BSON array.</param>
  179. public static void WriteRawBsonArray(this IBsonWriter writer, string name, IByteBuffer slice)
  180. {
  181. writer.WriteName(name);
  182. writer.WriteRawBsonArray(slice);
  183. }
  184. /// <summary>
  185. /// Writes a raw BSON document.
  186. /// </summary>
  187. /// <param name="writer">The writer.</param>
  188. /// <param name="name">The name.</param>
  189. /// <param name="slice">The byte buffer containing the raw BSON document.</param>
  190. public static void WriteRawBsonDocument(this IBsonWriter writer, string name, IByteBuffer slice)
  191. {
  192. writer.WriteName(name);
  193. writer.WriteRawBsonDocument(slice);
  194. }
  195. /// <summary>
  196. /// Writes a BSON regular expression element to the writer.
  197. /// </summary>
  198. /// <param name="writer">The writer.</param>
  199. /// <param name="name">The name of the element.</param>
  200. /// <param name="regex">A BsonRegularExpression.</param>
  201. public static void WriteRegularExpression(this IBsonWriter writer, string name, BsonRegularExpression regex)
  202. {
  203. writer.WriteName(name);
  204. writer.WriteRegularExpression(regex);
  205. }
  206. /// <summary>
  207. /// Writes the start of a BSON array element to the writer.
  208. /// </summary>
  209. /// <param name="writer">The writer.</param>
  210. /// <param name="name">The name of the element.</param>
  211. public static void WriteStartArray(this IBsonWriter writer, string name)
  212. {
  213. writer.WriteName(name);
  214. writer.WriteStartArray();
  215. }
  216. /// <summary>
  217. /// Writes the start of a BSON document element to the writer.
  218. /// </summary>
  219. /// <param name="writer">The writer.</param>
  220. /// <param name="name">The name of the element.</param>
  221. public static void WriteStartDocument(this IBsonWriter writer, string name)
  222. {
  223. writer.WriteName(name);
  224. writer.WriteStartDocument();
  225. }
  226. /// <summary>
  227. /// Writes a BSON String element to the writer.
  228. /// </summary>
  229. /// <param name="writer">The writer.</param>
  230. /// <param name="name">The name of the element.</param>
  231. /// <param name="value">The String value.</param>
  232. public static void WriteString(this IBsonWriter writer, string name, string value)
  233. {
  234. writer.WriteName(name);
  235. writer.WriteString(value);
  236. }
  237. /// <summary>
  238. /// Writes a BSON Symbol element to the writer.
  239. /// </summary>
  240. /// <param name="writer">The writer.</param>
  241. /// <param name="name">The name of the element.</param>
  242. /// <param name="value">The symbol.</param>
  243. public static void WriteSymbol(this IBsonWriter writer, string name, string value)
  244. {
  245. writer.WriteName(name);
  246. writer.WriteSymbol(value);
  247. }
  248. /// <summary>
  249. /// Writes a BSON timestamp element to the writer.
  250. /// </summary>
  251. /// <param name="writer">The writer.</param>
  252. /// <param name="name">The name of the element.</param>
  253. /// <param name="value">The combined timestamp/increment value.</param>
  254. public static void WriteTimestamp(this IBsonWriter writer, string name, long value)
  255. {
  256. writer.WriteName(name);
  257. writer.WriteTimestamp(value);
  258. }
  259. /// <summary>
  260. /// Writes a BSON undefined element to the writer.
  261. /// </summary>
  262. /// <param name="writer">The writer.</param>
  263. /// <param name="name">The name of the element.</param>
  264. public static void WriteUndefined(this IBsonWriter writer, string name)
  265. {
  266. writer.WriteName(name);
  267. writer.WriteUndefined();
  268. }
  269. }
  270. }