IBsonReaderExtensions.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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. using System;
  16. namespace MongoDB.Bson.IO
  17. {
  18. /// <summary>
  19. /// Contains extensions methods for IBsonReader.
  20. /// </summary>
  21. public static class IBsonReaderExtensions
  22. {
  23. /// <summary>
  24. /// Positions the reader to an element by name.
  25. /// </summary>
  26. /// <param name="reader">The reader.</param>
  27. /// <param name="name">The name of the element.</param>
  28. /// <returns>True if the element was found.</returns>
  29. public static bool FindElement(this IBsonReader reader, string name)
  30. {
  31. while (reader.ReadBsonType() != BsonType.EndOfDocument)
  32. {
  33. var elementName = reader.ReadName();
  34. if (elementName == name)
  35. {
  36. return true;
  37. }
  38. reader.SkipValue();
  39. }
  40. return false;
  41. }
  42. /// <summary>
  43. /// Positions the reader to a string element by name.
  44. /// </summary>
  45. /// <param name="reader">The reader.</param>
  46. /// <param name="name">The name of the element.</param>
  47. /// <returns>True if the element was found.</returns>
  48. public static string FindStringElement(this IBsonReader reader, string name)
  49. {
  50. BsonType bsonType;
  51. while ((bsonType = reader.ReadBsonType()) != BsonType.EndOfDocument)
  52. {
  53. if (bsonType == BsonType.String)
  54. {
  55. var elementName = reader.ReadName();
  56. if (elementName == name)
  57. {
  58. return reader.ReadString();
  59. }
  60. else
  61. {
  62. reader.SkipValue();
  63. }
  64. }
  65. else
  66. {
  67. reader.SkipName();
  68. reader.SkipValue();
  69. }
  70. }
  71. return null;
  72. }
  73. /// <summary>
  74. /// Reads a BSON binary data element from the reader.
  75. /// </summary>
  76. /// <param name="reader">The reader.</param>
  77. /// <param name="name">The name of the element.</param>
  78. /// <returns>A BsonBinaryData.</returns>
  79. public static BsonBinaryData ReadBinaryData(this IBsonReader reader, string name)
  80. {
  81. VerifyName(reader, name);
  82. return reader.ReadBinaryData();
  83. }
  84. /// <summary>
  85. /// Reads a BSON boolean element from the reader.
  86. /// </summary>
  87. /// <param name="reader">The reader.</param>
  88. /// <param name="name">The name of the element.</param>
  89. /// <returns>A Boolean.</returns>
  90. public static bool ReadBoolean(this IBsonReader reader, string name)
  91. {
  92. VerifyName(reader, name);
  93. return reader.ReadBoolean();
  94. }
  95. /// <summary>
  96. /// Reads a BSON binary data element from the reader.
  97. /// </summary>
  98. /// <param name="reader">The reader.</param>
  99. /// <param name="name">The name of the element.</param>
  100. /// <returns>A byte array.</returns>
  101. public static byte[] ReadBytes(this IBsonReader reader, string name)
  102. {
  103. VerifyName(reader, name);
  104. return reader.ReadBytes();
  105. }
  106. /// <summary>
  107. /// Reads a BSON DateTime element from the reader.
  108. /// </summary>
  109. /// <param name="reader">The reader.</param>
  110. /// <param name="name">The name of the element.</param>
  111. /// <returns>The number of milliseconds since the Unix epoch.</returns>
  112. public static long ReadDateTime(this IBsonReader reader, string name)
  113. {
  114. VerifyName(reader, name);
  115. return reader.ReadDateTime();
  116. }
  117. /// <summary>
  118. /// Reads a BSON Decimal128 element from the reader.
  119. /// </summary>
  120. /// <param name="reader">The reader.</param>
  121. /// <param name="name">The name of the element.</param>
  122. /// <returns>A <see cref="Decimal128"/>.</returns>
  123. public static Decimal128 ReadDecimal128(this IBsonReader reader, string name)
  124. {
  125. VerifyName(reader, name);
  126. return reader.ReadDecimal128();
  127. }
  128. /// <summary>
  129. /// Reads a BSON Double element from the reader.
  130. /// </summary>
  131. /// <param name="reader">The reader.</param>
  132. /// <param name="name">The name of the element.</param>
  133. /// <returns>A Double.</returns>
  134. public static double ReadDouble(this IBsonReader reader, string name)
  135. {
  136. VerifyName(reader, name);
  137. return reader.ReadDouble();
  138. }
  139. /// <summary>
  140. /// Reads a BSON Int32 element from the reader.
  141. /// </summary>
  142. /// <param name="reader">The reader.</param>
  143. /// <param name="name">The name of the element.</param>
  144. /// <returns>An Int32.</returns>
  145. public static int ReadInt32(this IBsonReader reader, string name)
  146. {
  147. VerifyName(reader, name);
  148. return reader.ReadInt32();
  149. }
  150. /// <summary>
  151. /// Reads a BSON Int64 element from the reader.
  152. /// </summary>
  153. /// <param name="reader">The reader.</param>
  154. /// <param name="name">The name of the element.</param>
  155. /// <returns>An Int64.</returns>
  156. public static long ReadInt64(this IBsonReader reader, string name)
  157. {
  158. VerifyName(reader, name);
  159. return reader.ReadInt64();
  160. }
  161. /// <summary>
  162. /// Reads a BSON JavaScript element from the reader.
  163. /// </summary>
  164. /// <param name="reader">The reader.</param>
  165. /// <param name="name">The name of the element.</param>
  166. /// <returns>A string.</returns>
  167. public static string ReadJavaScript(this IBsonReader reader, string name)
  168. {
  169. VerifyName(reader, name);
  170. return reader.ReadJavaScript();
  171. }
  172. /// <summary>
  173. /// Reads a BSON JavaScript with scope element from the reader (call ReadStartDocument next to read the scope).
  174. /// </summary>
  175. /// <param name="reader">The reader.</param>
  176. /// <param name="name">The name of the element.</param>
  177. /// <returns>A string.</returns>
  178. public static string ReadJavaScriptWithScope(this IBsonReader reader, string name)
  179. {
  180. VerifyName(reader, name);
  181. return reader.ReadJavaScriptWithScope();
  182. }
  183. /// <summary>
  184. /// Reads a BSON MaxKey element from the reader.
  185. /// </summary>
  186. /// <param name="reader">The reader.</param>
  187. /// <param name="name">The name of the element.</param>
  188. public static void ReadMaxKey(this IBsonReader reader, string name)
  189. {
  190. VerifyName(reader, name);
  191. reader.ReadMaxKey();
  192. }
  193. /// <summary>
  194. /// Reads a BSON MinKey element from the reader.
  195. /// </summary>
  196. /// <param name="reader">The reader.</param>
  197. /// <param name="name">The name of the element.</param>
  198. public static void ReadMinKey(this IBsonReader reader, string name)
  199. {
  200. VerifyName(reader, name);
  201. reader.ReadMinKey();
  202. }
  203. /// <summary>
  204. /// Reads the name of an element from the reader.
  205. /// </summary>
  206. /// <param name="reader">The reader.</param>
  207. /// <returns>The name of the element.</returns>
  208. public static string ReadName(this IBsonReader reader)
  209. {
  210. return reader.ReadName(Utf8NameDecoder.Instance);
  211. }
  212. /// <summary>
  213. /// Reads the name of an element from the reader.
  214. /// </summary>
  215. /// <param name="reader">The reader.</param>
  216. /// <param name="name">The name of the element.</param>
  217. public static void ReadName(this IBsonReader reader, string name)
  218. {
  219. VerifyName(reader, name);
  220. }
  221. /// <summary>
  222. /// Reads a BSON null element from the reader.
  223. /// </summary>
  224. /// <param name="reader">The reader.</param>
  225. /// <param name="name">The name of the element.</param>
  226. public static void ReadNull(this IBsonReader reader, string name)
  227. {
  228. VerifyName(reader, name);
  229. reader.ReadNull();
  230. }
  231. /// <summary>
  232. /// Reads a BSON ObjectId element from the reader.
  233. /// </summary>
  234. /// <param name="reader">The reader.</param>
  235. /// <param name="name">The name of the element.</param>
  236. /// <returns>An ObjectId.</returns>
  237. public static ObjectId ReadObjectId(this IBsonReader reader, string name)
  238. {
  239. VerifyName(reader, name);
  240. return reader.ReadObjectId();
  241. }
  242. /// <summary>
  243. /// Reads a raw BSON array.
  244. /// </summary>
  245. /// <param name="reader">The reader.</param>
  246. /// <param name="name">The name.</param>
  247. /// <returns>
  248. /// The raw BSON array.
  249. /// </returns>
  250. public static IByteBuffer ReadRawBsonArray(this IBsonReader reader, string name)
  251. {
  252. VerifyName(reader, name);
  253. return reader.ReadRawBsonArray();
  254. }
  255. /// <summary>
  256. /// Reads a raw BSON document.
  257. /// </summary>
  258. /// <param name="reader">The reader.</param>
  259. /// <param name="name">The name.</param>
  260. /// <returns>The raw BSON document.</returns>
  261. public static IByteBuffer ReadRawBsonDocument(this IBsonReader reader, string name)
  262. {
  263. VerifyName(reader, name);
  264. return reader.ReadRawBsonDocument();
  265. }
  266. /// <summary>
  267. /// Reads a BSON regular expression element from the reader.
  268. /// </summary>
  269. /// <param name="reader">The reader.</param>
  270. /// <param name="name">The name of the element.</param>
  271. /// <returns>A BsonRegularExpression.</returns>
  272. public static BsonRegularExpression ReadRegularExpression(this IBsonReader reader, string name)
  273. {
  274. VerifyName(reader, name);
  275. return reader.ReadRegularExpression();
  276. }
  277. /// <summary>
  278. /// Reads a BSON string element from the reader.
  279. /// </summary>
  280. /// <param name="reader">The reader.</param>
  281. /// <param name="name">The name of the element.</param>
  282. /// <returns>A String.</returns>
  283. public static string ReadString(this IBsonReader reader, string name)
  284. {
  285. VerifyName(reader, name);
  286. return reader.ReadString();
  287. }
  288. /// <summary>
  289. /// Reads a BSON symbol element from the reader.
  290. /// </summary>
  291. /// <param name="reader">The reader.</param>
  292. /// <param name="name">The name of the element.</param>
  293. /// <returns>A string.</returns>
  294. public static string ReadSymbol(this IBsonReader reader, string name)
  295. {
  296. VerifyName(reader, name);
  297. return reader.ReadSymbol();
  298. }
  299. /// <summary>
  300. /// Reads a BSON timestamp element from the reader.
  301. /// </summary>
  302. /// <returns>The combined timestamp/increment.</returns>
  303. /// <param name="reader">The reader.</param>
  304. /// <param name="name">The name of the element.</param>
  305. public static long ReadTimestamp(this IBsonReader reader, string name)
  306. {
  307. VerifyName(reader, name);
  308. return reader.ReadTimestamp();
  309. }
  310. /// <summary>
  311. /// Reads a BSON undefined element from the reader.
  312. /// </summary>
  313. /// <param name="reader">The reader.</param>
  314. /// <param name="name">The name of the element.</param>
  315. public static void ReadUndefined(this IBsonReader reader, string name)
  316. {
  317. VerifyName(reader, name);
  318. reader.ReadUndefined();
  319. }
  320. private static void VerifyName(IBsonReader reader, string expectedName)
  321. {
  322. var actualName = reader.ReadName();
  323. if (actualName != expectedName)
  324. {
  325. var message = string.Format(
  326. "Expected element name to be '{0}', not '{1}'.",
  327. expectedName, actualName);
  328. throw new FormatException(message);
  329. }
  330. }
  331. }
  332. }