IBsonReaderExtensions.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  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. 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 binary data element from the reader temporarily setting the GuidRepresentation to Unspecified.
  86. /// </summary>
  87. /// <param name="reader">The reader.</param>
  88. /// <returns>A BsonBinaryData.</returns>
  89. [Obsolete("In V3 mode use ReadBinaryData instead.")]
  90. public static BsonBinaryData ReadBinaryDataWithGuidRepresentationUnspecified(this IBsonReader reader)
  91. {
  92. #pragma warning disable 618
  93. if (BsonDefaults.GuidRepresentationMode == GuidRepresentationMode.V2)
  94. {
  95. reader.PushSettings(s => s.GuidRepresentation = GuidRepresentation.Unspecified);
  96. try
  97. {
  98. return reader.ReadBinaryData();
  99. }
  100. finally
  101. {
  102. reader.PopSettings();
  103. }
  104. }
  105. else
  106. {
  107. return reader.ReadBinaryData();
  108. }
  109. #pragma warning restore 618
  110. }
  111. /// <summary>
  112. /// Reads a BSON boolean element from the reader.
  113. /// </summary>
  114. /// <param name="reader">The reader.</param>
  115. /// <param name="name">The name of the element.</param>
  116. /// <returns>A Boolean.</returns>
  117. public static bool ReadBoolean(this IBsonReader reader, string name)
  118. {
  119. VerifyName(reader, name);
  120. return reader.ReadBoolean();
  121. }
  122. /// <summary>
  123. /// Reads a BSON binary data element from the reader.
  124. /// </summary>
  125. /// <param name="reader">The reader.</param>
  126. /// <param name="name">The name of the element.</param>
  127. /// <returns>A byte array.</returns>
  128. public static byte[] ReadBytes(this IBsonReader reader, string name)
  129. {
  130. VerifyName(reader, name);
  131. return reader.ReadBytes();
  132. }
  133. /// <summary>
  134. /// Reads a BSON DateTime element from the reader.
  135. /// </summary>
  136. /// <param name="reader">The reader.</param>
  137. /// <param name="name">The name of the element.</param>
  138. /// <returns>The number of milliseconds since the Unix epoch.</returns>
  139. public static long ReadDateTime(this IBsonReader reader, string name)
  140. {
  141. VerifyName(reader, name);
  142. return reader.ReadDateTime();
  143. }
  144. /// <summary>
  145. /// Reads a BSON Decimal128 element from the reader.
  146. /// </summary>
  147. /// <param name="reader">The reader.</param>
  148. /// <param name="name">The name of the element.</param>
  149. /// <returns>A <see cref="Decimal128"/>.</returns>
  150. public static Decimal128 ReadDecimal128(this IBsonReader reader, string name)
  151. {
  152. VerifyName(reader, name);
  153. return reader.ReadDecimal128();
  154. }
  155. /// <summary>
  156. /// Reads a BSON Double element from the reader.
  157. /// </summary>
  158. /// <param name="reader">The reader.</param>
  159. /// <param name="name">The name of the element.</param>
  160. /// <returns>A Double.</returns>
  161. public static double ReadDouble(this IBsonReader reader, string name)
  162. {
  163. VerifyName(reader, name);
  164. return reader.ReadDouble();
  165. }
  166. /// <summary>
  167. /// Reads a BSON Int32 element from the reader.
  168. /// </summary>
  169. /// <param name="reader">The reader.</param>
  170. /// <param name="name">The name of the element.</param>
  171. /// <returns>An Int32.</returns>
  172. public static int ReadInt32(this IBsonReader reader, string name)
  173. {
  174. VerifyName(reader, name);
  175. return reader.ReadInt32();
  176. }
  177. /// <summary>
  178. /// Reads a BSON Int64 element from the reader.
  179. /// </summary>
  180. /// <param name="reader">The reader.</param>
  181. /// <param name="name">The name of the element.</param>
  182. /// <returns>An Int64.</returns>
  183. public static long ReadInt64(this IBsonReader reader, string name)
  184. {
  185. VerifyName(reader, name);
  186. return reader.ReadInt64();
  187. }
  188. /// <summary>
  189. /// Reads a BSON JavaScript element from the reader.
  190. /// </summary>
  191. /// <param name="reader">The reader.</param>
  192. /// <param name="name">The name of the element.</param>
  193. /// <returns>A string.</returns>
  194. public static string ReadJavaScript(this IBsonReader reader, string name)
  195. {
  196. VerifyName(reader, name);
  197. return reader.ReadJavaScript();
  198. }
  199. /// <summary>
  200. /// Reads a BSON JavaScript with scope element from the reader (call ReadStartDocument next to read the scope).
  201. /// </summary>
  202. /// <param name="reader">The reader.</param>
  203. /// <param name="name">The name of the element.</param>
  204. /// <returns>A string.</returns>
  205. public static string ReadJavaScriptWithScope(this IBsonReader reader, string name)
  206. {
  207. VerifyName(reader, name);
  208. return reader.ReadJavaScriptWithScope();
  209. }
  210. /// <summary>
  211. /// Reads a BSON MaxKey element from the reader.
  212. /// </summary>
  213. /// <param name="reader">The reader.</param>
  214. /// <param name="name">The name of the element.</param>
  215. public static void ReadMaxKey(this IBsonReader reader, string name)
  216. {
  217. VerifyName(reader, name);
  218. reader.ReadMaxKey();
  219. }
  220. /// <summary>
  221. /// Reads a BSON MinKey element from the reader.
  222. /// </summary>
  223. /// <param name="reader">The reader.</param>
  224. /// <param name="name">The name of the element.</param>
  225. public static void ReadMinKey(this IBsonReader reader, string name)
  226. {
  227. VerifyName(reader, name);
  228. reader.ReadMinKey();
  229. }
  230. /// <summary>
  231. /// Reads the name of an element from the reader.
  232. /// </summary>
  233. /// <param name="reader">The reader.</param>
  234. /// <returns>The name of the element.</returns>
  235. public static string ReadName(this IBsonReader reader)
  236. {
  237. return reader.ReadName(Utf8NameDecoder.Instance);
  238. }
  239. /// <summary>
  240. /// Reads the name of an element from the reader.
  241. /// </summary>
  242. /// <param name="reader">The reader.</param>
  243. /// <param name="name">The name of the element.</param>
  244. public static void ReadName(this IBsonReader reader, string name)
  245. {
  246. VerifyName(reader, name);
  247. }
  248. /// <summary>
  249. /// Reads a BSON null element from the reader.
  250. /// </summary>
  251. /// <param name="reader">The reader.</param>
  252. /// <param name="name">The name of the element.</param>
  253. public static void ReadNull(this IBsonReader reader, string name)
  254. {
  255. VerifyName(reader, name);
  256. reader.ReadNull();
  257. }
  258. /// <summary>
  259. /// Reads a BSON ObjectId element from the reader.
  260. /// </summary>
  261. /// <param name="reader">The reader.</param>
  262. /// <param name="name">The name of the element.</param>
  263. /// <returns>An ObjectId.</returns>
  264. public static ObjectId ReadObjectId(this IBsonReader reader, string name)
  265. {
  266. VerifyName(reader, name);
  267. return reader.ReadObjectId();
  268. }
  269. /// <summary>
  270. /// Reads a raw BSON array.
  271. /// </summary>
  272. /// <param name="reader">The reader.</param>
  273. /// <param name="name">The name.</param>
  274. /// <returns>
  275. /// The raw BSON array.
  276. /// </returns>
  277. public static IByteBuffer ReadRawBsonArray(this IBsonReader reader, string name)
  278. {
  279. VerifyName(reader, name);
  280. return reader.ReadRawBsonArray();
  281. }
  282. /// <summary>
  283. /// Reads a raw BSON document.
  284. /// </summary>
  285. /// <param name="reader">The reader.</param>
  286. /// <param name="name">The name.</param>
  287. /// <returns>The raw BSON document.</returns>
  288. public static IByteBuffer ReadRawBsonDocument(this IBsonReader reader, string name)
  289. {
  290. VerifyName(reader, name);
  291. return reader.ReadRawBsonDocument();
  292. }
  293. /// <summary>
  294. /// Reads a BSON regular expression element from the reader.
  295. /// </summary>
  296. /// <param name="reader">The reader.</param>
  297. /// <param name="name">The name of the element.</param>
  298. /// <returns>A BsonRegularExpression.</returns>
  299. public static BsonRegularExpression ReadRegularExpression(this IBsonReader reader, string name)
  300. {
  301. VerifyName(reader, name);
  302. return reader.ReadRegularExpression();
  303. }
  304. /// <summary>
  305. /// Reads a BSON string element from the reader.
  306. /// </summary>
  307. /// <param name="reader">The reader.</param>
  308. /// <param name="name">The name of the element.</param>
  309. /// <returns>A String.</returns>
  310. public static string ReadString(this IBsonReader reader, string name)
  311. {
  312. VerifyName(reader, name);
  313. return reader.ReadString();
  314. }
  315. /// <summary>
  316. /// Reads a BSON symbol element from the reader.
  317. /// </summary>
  318. /// <param name="reader">The reader.</param>
  319. /// <param name="name">The name of the element.</param>
  320. /// <returns>A string.</returns>
  321. public static string ReadSymbol(this IBsonReader reader, string name)
  322. {
  323. VerifyName(reader, name);
  324. return reader.ReadSymbol();
  325. }
  326. /// <summary>
  327. /// Reads a BSON timestamp element from the reader.
  328. /// </summary>
  329. /// <returns>The combined timestamp/increment.</returns>
  330. /// <param name="reader">The reader.</param>
  331. /// <param name="name">The name of the element.</param>
  332. public static long ReadTimestamp(this IBsonReader reader, string name)
  333. {
  334. VerifyName(reader, name);
  335. return reader.ReadTimestamp();
  336. }
  337. /// <summary>
  338. /// Reads a BSON undefined element from the reader.
  339. /// </summary>
  340. /// <param name="reader">The reader.</param>
  341. /// <param name="name">The name of the element.</param>
  342. public static void ReadUndefined(this IBsonReader reader, string name)
  343. {
  344. VerifyName(reader, name);
  345. reader.ReadUndefined();
  346. }
  347. private static void VerifyName(IBsonReader reader, string expectedName)
  348. {
  349. var actualName = reader.ReadName();
  350. if (actualName != expectedName)
  351. {
  352. var message = string.Format(
  353. "Expected element name to be '{0}', not '{1}'.",
  354. expectedName, actualName);
  355. throw new FormatException(message);
  356. }
  357. }
  358. }
  359. }