BsonValue.cs 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657
  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. using System.Collections.Generic;
  17. using System.Text.RegularExpressions;
  18. namespace MongoDB.Bson
  19. {
  20. /// <summary>
  21. /// Represents a BSON value (this is an abstract class, see the various subclasses).
  22. /// </summary>
  23. #if NET452
  24. [Serializable]
  25. #endif
  26. public abstract class BsonValue : IComparable<BsonValue>, IConvertible, IEquatable<BsonValue>
  27. {
  28. // private static fields
  29. private static Dictionary<BsonType, int> __bsonTypeSortOrder = new Dictionary<BsonType, int>
  30. {
  31. { BsonType.MinKey, 1 },
  32. { BsonType.Undefined, 2 },
  33. { BsonType.Null, 3 },
  34. { BsonType.Decimal128, 4 },
  35. { BsonType.Double, 4 },
  36. { BsonType.Int32, 4 },
  37. { BsonType.Int64, 4 },
  38. { BsonType.String, 5 },
  39. { BsonType.Symbol, 5 },
  40. { BsonType.Document, 6 },
  41. { BsonType.Array, 7 },
  42. { BsonType.Binary, 8 },
  43. { BsonType.ObjectId, 9 },
  44. { BsonType.Boolean, 10 },
  45. { BsonType.DateTime, 11 },
  46. { BsonType.Timestamp, 11 },
  47. { BsonType.RegularExpression, 12 },
  48. { BsonType.JavaScript, 13 }, // TODO: confirm where JavaScript and JavaScriptWithScope are in the sort order
  49. { BsonType.JavaScriptWithScope, 14 },
  50. { BsonType.MaxKey, 15 }
  51. };
  52. // public properties
  53. /// <summary>
  54. /// Casts the BsonValue to a Boolean (throws an InvalidCastException if the cast is not valid).
  55. /// </summary>
  56. public bool AsBoolean
  57. {
  58. get { return ((BsonBoolean)this).Value; }
  59. }
  60. /// <summary>
  61. /// Casts the BsonValue to a BsonArray (throws an InvalidCastException if the cast is not valid).
  62. /// </summary>
  63. public BsonArray AsBsonArray
  64. {
  65. get { return (BsonArray)this; }
  66. }
  67. /// <summary>
  68. /// Casts the BsonValue to a BsonBinaryData (throws an InvalidCastException if the cast is not valid).
  69. /// </summary>
  70. public BsonBinaryData AsBsonBinaryData
  71. {
  72. get { return (BsonBinaryData)this; }
  73. }
  74. /// <summary>
  75. /// Casts the BsonValue to a BsonDateTime (throws an InvalidCastException if the cast is not valid).
  76. /// </summary>
  77. public BsonDateTime AsBsonDateTime
  78. {
  79. get { return (BsonDateTime)this; }
  80. }
  81. /// <summary>
  82. /// Casts the BsonValue to a BsonDocument (throws an InvalidCastException if the cast is not valid).
  83. /// </summary>
  84. public BsonDocument AsBsonDocument
  85. {
  86. get { return (BsonDocument)this; }
  87. }
  88. /// <summary>
  89. /// Casts the BsonValue to a BsonJavaScript (throws an InvalidCastException if the cast is not valid).
  90. /// </summary>
  91. public BsonJavaScript AsBsonJavaScript
  92. {
  93. get { return (BsonJavaScript)this; }
  94. }
  95. /// <summary>
  96. /// Casts the BsonValue to a BsonJavaScriptWithScope (throws an InvalidCastException if the cast is not valid).
  97. /// </summary>
  98. public BsonJavaScriptWithScope AsBsonJavaScriptWithScope
  99. {
  100. get { return (BsonJavaScriptWithScope)this; }
  101. }
  102. /// <summary>
  103. /// Casts the BsonValue to a BsonMaxKey (throws an InvalidCastException if the cast is not valid).
  104. /// </summary>
  105. public BsonMaxKey AsBsonMaxKey
  106. {
  107. get { return (BsonMaxKey)this; }
  108. }
  109. /// <summary>
  110. /// Casts the BsonValue to a BsonMinKey (throws an InvalidCastException if the cast is not valid).
  111. /// </summary>
  112. public BsonMinKey AsBsonMinKey
  113. {
  114. get { return (BsonMinKey)this; }
  115. }
  116. /// <summary>
  117. /// Casts the BsonValue to a BsonNull (throws an InvalidCastException if the cast is not valid).
  118. /// </summary>
  119. public BsonNull AsBsonNull
  120. {
  121. get { return (BsonNull)this; }
  122. }
  123. /// <summary>
  124. /// Casts the BsonValue to a BsonRegularExpression (throws an InvalidCastException if the cast is not valid).
  125. /// </summary>
  126. public BsonRegularExpression AsBsonRegularExpression
  127. {
  128. get { return (BsonRegularExpression)this; }
  129. }
  130. /// <summary>
  131. /// Casts the BsonValue to a BsonSymbol (throws an InvalidCastException if the cast is not valid).
  132. /// </summary>
  133. public BsonSymbol AsBsonSymbol
  134. {
  135. get { return (BsonSymbol)this; }
  136. }
  137. /// <summary>
  138. /// Casts the BsonValue to a BsonTimestamp (throws an InvalidCastException if the cast is not valid).
  139. /// </summary>
  140. public BsonTimestamp AsBsonTimestamp
  141. {
  142. get { return (BsonTimestamp)this; }
  143. }
  144. /// <summary>
  145. /// Casts the BsonValue to a BsonUndefined (throws an InvalidCastException if the cast is not valid).
  146. /// </summary>
  147. public BsonUndefined AsBsonUndefined
  148. {
  149. get { return (BsonUndefined)this; }
  150. }
  151. /// <summary>
  152. /// Casts the BsonValue to a BsonValue (a way of upcasting subclasses of BsonValue to BsonValue at compile time).
  153. /// </summary>
  154. public BsonValue AsBsonValue
  155. {
  156. get { return this; }
  157. }
  158. /// <summary>
  159. /// Casts the BsonValue to a Byte[] (throws an InvalidCastException if the cast is not valid).
  160. /// </summary>
  161. public byte[] AsByteArray
  162. {
  163. get { return ((BsonBinaryData)this).Bytes; }
  164. }
  165. /// <summary>
  166. /// Casts the BsonValue to a DateTime in UTC (throws an InvalidCastException if the cast is not valid).
  167. /// </summary>
  168. [Obsolete("Use ToUniversalTime instead.")]
  169. public DateTime AsDateTime
  170. {
  171. get { return AsUniversalTime; }
  172. }
  173. /// <summary>
  174. /// Casts the BsonValue to a <see cref="decimal"/> (throws an InvalidCastException if the cast is not valid).
  175. /// </summary>
  176. public decimal AsDecimal
  177. {
  178. get { return (decimal)((BsonDecimal128)this).Value; }
  179. }
  180. /// <summary>
  181. /// Casts the BsonValue to a <see cref="Decimal128"/> (throws an InvalidCastException if the cast is not valid).
  182. /// </summary>
  183. public Decimal128 AsDecimal128
  184. {
  185. get { return ((BsonDecimal128)this).Value; }
  186. }
  187. /// <summary>
  188. /// Casts the BsonValue to a Double (throws an InvalidCastException if the cast is not valid).
  189. /// </summary>
  190. public double AsDouble
  191. {
  192. get { return ((BsonDouble)this).Value; }
  193. }
  194. /// <summary>
  195. /// Casts the BsonValue to a Guid (throws an InvalidCastException if the cast is not valid).
  196. /// </summary>
  197. public Guid AsGuid
  198. {
  199. get { return ((BsonBinaryData)this).ToGuid(); }
  200. }
  201. /// <summary>
  202. /// Casts the BsonValue to an Int32 (throws an InvalidCastException if the cast is not valid).
  203. /// </summary>
  204. public int AsInt32
  205. {
  206. get { return ((BsonInt32)this).Value; }
  207. }
  208. /// <summary>
  209. /// Casts the BsonValue to a DateTime in the local timezone (throws an InvalidCastException if the cast is not valid).
  210. /// </summary>
  211. [Obsolete("Use ToLocalTime instead.")]
  212. public DateTime AsLocalTime
  213. {
  214. get { return ((BsonDateTime)this).ToLocalTime(); }
  215. }
  216. /// <summary>
  217. /// Casts the BsonValue to a Int64 (throws an InvalidCastException if the cast is not valid).
  218. /// </summary>
  219. public long AsInt64
  220. {
  221. get { return ((BsonInt64)this).Value; }
  222. }
  223. /// <summary>
  224. /// Casts the BsonValue to a Nullable{Boolean} (throws an InvalidCastException if the cast is not valid).
  225. /// </summary>
  226. public bool? AsNullableBoolean
  227. {
  228. get { return (BsonType == BsonType.Null) ? null : (bool?)AsBoolean; }
  229. }
  230. /// <summary>
  231. /// Casts the BsonValue to a Nullable{DateTime} (throws an InvalidCastException if the cast is not valid).
  232. /// </summary>
  233. [Obsolete("Use ToNullableUniversalTime instead.")]
  234. public DateTime? AsNullableDateTime
  235. {
  236. get { return (BsonType == BsonType.Null) ? null : (DateTime?)AsDateTime; }
  237. }
  238. /// <summary>
  239. /// Casts the BsonValue to a Nullable{Decimal} (throws an InvalidCastException if the cast is not valid).
  240. /// </summary>
  241. public decimal? AsNullableDecimal
  242. {
  243. get { return (BsonType == BsonType.Null) ? null : (decimal?)AsDecimal128; }
  244. }
  245. /// <summary>
  246. /// Casts the BsonValue to a Nullable{Decimal128} (throws an InvalidCastException if the cast is not valid).
  247. /// </summary>
  248. public Decimal128? AsNullableDecimal128
  249. {
  250. get { return (BsonType == BsonType.Null) ? null : (Decimal128?)AsDecimal128; }
  251. }
  252. /// <summary>
  253. /// Casts the BsonValue to a Nullable{Double} (throws an InvalidCastException if the cast is not valid).
  254. /// </summary>
  255. public double? AsNullableDouble
  256. {
  257. get { return (BsonType == BsonType.Null) ? null : (double?)AsDouble; }
  258. }
  259. /// <summary>
  260. /// Casts the BsonValue to a Nullable{Guid} (throws an InvalidCastException if the cast is not valid).
  261. /// </summary>
  262. public Guid? AsNullableGuid
  263. {
  264. get { return (BsonType == BsonType.Null) ? null : (Guid?)AsGuid; }
  265. }
  266. /// <summary>
  267. /// Casts the BsonValue to a Nullable{Int32} (throws an InvalidCastException if the cast is not valid).
  268. /// </summary>
  269. public int? AsNullableInt32
  270. {
  271. get { return (BsonType == BsonType.Null) ? null : (int?)AsInt32; }
  272. }
  273. /// <summary>
  274. /// Casts the BsonValue to a Nullable{Int64} (throws an InvalidCastException if the cast is not valid).
  275. /// </summary>
  276. public long? AsNullableInt64
  277. {
  278. get { return (BsonType == BsonType.Null) ? null : (long?)AsInt64; }
  279. }
  280. /// <summary>
  281. /// Casts the BsonValue to a Nullable{ObjectId} (throws an InvalidCastException if the cast is not valid).
  282. /// </summary>
  283. public ObjectId? AsNullableObjectId
  284. {
  285. get { return (BsonType == BsonType.Null) ? null : (ObjectId?)AsObjectId; }
  286. }
  287. /// <summary>
  288. /// Casts the BsonValue to an ObjectId (throws an InvalidCastException if the cast is not valid).
  289. /// </summary>
  290. public ObjectId AsObjectId
  291. {
  292. get { return ((BsonObjectId)this).Value; }
  293. }
  294. /// <summary>
  295. /// Casts the BsonValue to a Regex (throws an InvalidCastException if the cast is not valid).
  296. /// </summary>
  297. public Regex AsRegex
  298. {
  299. get { return ((BsonRegularExpression)this).ToRegex(); }
  300. }
  301. /// <summary>
  302. /// Casts the BsonValue to a String (throws an InvalidCastException if the cast is not valid).
  303. /// </summary>
  304. public string AsString
  305. {
  306. get { return ((BsonString)this).Value; }
  307. }
  308. /// <summary>
  309. /// Casts the BsonValue to a DateTime in UTC (throws an InvalidCastException if the cast is not valid).
  310. /// </summary>
  311. [Obsolete("Use ToUniversalTime instead.")]
  312. public DateTime AsUniversalTime
  313. {
  314. get { return ((BsonDateTime)this).ToUniversalTime(); }
  315. }
  316. /// <summary>
  317. /// Gets the BsonType of this BsonValue.
  318. /// </summary>
  319. public abstract BsonType BsonType { get; }
  320. /// <summary>
  321. /// Tests whether this BsonValue is a Boolean.
  322. /// </summary>
  323. public bool IsBoolean
  324. {
  325. get { return BsonType == BsonType.Boolean; }
  326. }
  327. /// <summary>
  328. /// Tests whether this BsonValue is a BsonArray.
  329. /// </summary>
  330. public bool IsBsonArray
  331. {
  332. get { return BsonType == BsonType.Array; }
  333. }
  334. /// <summary>
  335. /// Tests whether this BsonValue is a BsonBinaryData.
  336. /// </summary>
  337. public bool IsBsonBinaryData
  338. {
  339. get { return BsonType == BsonType.Binary; }
  340. }
  341. /// <summary>
  342. /// Tests whether this BsonValue is a BsonDateTime.
  343. /// </summary>
  344. public bool IsBsonDateTime
  345. {
  346. get { return BsonType == BsonType.DateTime; }
  347. }
  348. /// <summary>
  349. /// Tests whether this BsonValue is a BsonDocument.
  350. /// </summary>
  351. public bool IsBsonDocument
  352. {
  353. get { return BsonType == BsonType.Document; }
  354. }
  355. /// <summary>
  356. /// Tests whether this BsonValue is a BsonJavaScript.
  357. /// </summary>
  358. public bool IsBsonJavaScript
  359. {
  360. get { return BsonType == BsonType.JavaScript || BsonType == BsonType.JavaScriptWithScope; }
  361. }
  362. /// <summary>
  363. /// Tests whether this BsonValue is a BsonJavaScriptWithScope.
  364. /// </summary>
  365. public bool IsBsonJavaScriptWithScope
  366. {
  367. get { return BsonType == BsonType.JavaScriptWithScope; }
  368. }
  369. /// <summary>
  370. /// Tests whether this BsonValue is a BsonMaxKey.
  371. /// </summary>
  372. public bool IsBsonMaxKey
  373. {
  374. get { return BsonType == BsonType.MaxKey; }
  375. }
  376. /// <summary>
  377. /// Tests whether this BsonValue is a BsonMinKey.
  378. /// </summary>
  379. public bool IsBsonMinKey
  380. {
  381. get { return BsonType == BsonType.MinKey; }
  382. }
  383. /// <summary>
  384. /// Tests whether this BsonValue is a BsonNull.
  385. /// </summary>
  386. public bool IsBsonNull
  387. {
  388. get { return BsonType == BsonType.Null; }
  389. }
  390. /// <summary>
  391. /// Tests whether this BsonValue is a BsonRegularExpression.
  392. /// </summary>
  393. public bool IsBsonRegularExpression
  394. {
  395. get { return BsonType == BsonType.RegularExpression; }
  396. }
  397. /// <summary>
  398. /// Tests whether this BsonValue is a BsonSymbol .
  399. /// </summary>
  400. public bool IsBsonSymbol
  401. {
  402. get { return BsonType == BsonType.Symbol; }
  403. }
  404. /// <summary>
  405. /// Tests whether this BsonValue is a BsonTimestamp.
  406. /// </summary>
  407. public bool IsBsonTimestamp
  408. {
  409. get { return BsonType == BsonType.Timestamp; }
  410. }
  411. /// <summary>
  412. /// Tests whether this BsonValue is a BsonUndefined.
  413. /// </summary>
  414. public bool IsBsonUndefined
  415. {
  416. get { return BsonType == BsonType.Undefined; }
  417. }
  418. /// <summary>
  419. /// Tests whether this BsonValue is a DateTime.
  420. /// </summary>
  421. [Obsolete("Use IsValidDateTime instead.")]
  422. public bool IsDateTime
  423. {
  424. get { return IsValidDateTime; }
  425. }
  426. /// <summary>
  427. /// Tests whether this BsonValue is a Decimal128.
  428. /// </summary>
  429. public bool IsDecimal128
  430. {
  431. get { return BsonType == BsonType.Decimal128; }
  432. }
  433. /// <summary>
  434. /// Tests whether this BsonValue is a Double.
  435. /// </summary>
  436. public bool IsDouble
  437. {
  438. get { return BsonType == BsonType.Double; }
  439. }
  440. /// <summary>
  441. /// Tests whether this BsonValue is a Guid.
  442. /// </summary>
  443. public bool IsGuid
  444. {
  445. get
  446. {
  447. if (BsonType == BsonType.Binary)
  448. {
  449. var subType = ((BsonBinaryData)this).SubType;
  450. return subType == BsonBinarySubType.UuidStandard || subType == BsonBinarySubType.UuidLegacy;
  451. }
  452. else
  453. {
  454. return false;
  455. }
  456. }
  457. }
  458. /// <summary>
  459. /// Tests whether this BsonValue is an Int32.
  460. /// </summary>
  461. public bool IsInt32
  462. {
  463. get { return BsonType == BsonType.Int32; }
  464. }
  465. /// <summary>
  466. /// Tests whether this BsonValue is an Int64.
  467. /// </summary>
  468. public bool IsInt64
  469. {
  470. get { return BsonType == BsonType.Int64; }
  471. }
  472. /// <summary>
  473. /// Tests whether this BsonValue is a numeric value.
  474. /// </summary>
  475. public bool IsNumeric
  476. {
  477. get
  478. {
  479. return
  480. BsonType == BsonType.Decimal128 ||
  481. BsonType == BsonType.Double ||
  482. BsonType == BsonType.Int32 ||
  483. BsonType == BsonType.Int64;
  484. }
  485. }
  486. /// <summary>
  487. /// Tests whether this BsonValue is an ObjectId .
  488. /// </summary>
  489. public bool IsObjectId
  490. {
  491. get { return BsonType == BsonType.ObjectId; }
  492. }
  493. /// <summary>
  494. /// Tests whether this BsonValue is a String.
  495. /// </summary>
  496. public bool IsString
  497. {
  498. get { return BsonType == BsonType.String; }
  499. }
  500. /// <summary>
  501. /// Tests whether this BsonValue is a valid DateTime.
  502. /// </summary>
  503. public virtual bool IsValidDateTime
  504. {
  505. get { return false; }
  506. }
  507. /// <summary>
  508. /// Gets the raw value of this BsonValue (or null if this BsonValue doesn't have a single scalar value).
  509. /// </summary>
  510. // note: don't change return value to "this" or lots of things will break
  511. [Obsolete("Use Value property of subclasses or BsonTypeMapper.MapToDotNetValue instead.")]
  512. public virtual object RawValue
  513. {
  514. get { return null; } // subclasses that have a single value (e.g. Int32) override this
  515. }
  516. // public operators
  517. /// <summary>
  518. /// Casts a BsonValue to a bool.
  519. /// </summary>
  520. /// <param name="value">The BsonValue.</param>
  521. /// <returns>A bool.</returns>
  522. public static explicit operator bool(BsonValue value)
  523. {
  524. if (value == null)
  525. {
  526. throw new ArgumentNullException("value");
  527. }
  528. return value.AsBoolean;
  529. }
  530. /// <summary>
  531. /// Casts a BsonValue to a bool?.
  532. /// </summary>
  533. /// <param name="value">The BsonValue.</param>
  534. /// <returns>A bool?.</returns>
  535. public static explicit operator bool?(BsonValue value)
  536. {
  537. return (value == null) ? null : value.AsNullableBoolean;
  538. }
  539. /// <summary>
  540. /// Converts a bool to a BsonValue.
  541. /// </summary>
  542. /// <param name="value">A bool.</param>
  543. /// <returns>A BsonValue.</returns>
  544. public static implicit operator BsonValue(bool value)
  545. {
  546. return (BsonBoolean)value;
  547. }
  548. /// <summary>
  549. /// Converts a bool? to a BsonValue.
  550. /// </summary>
  551. /// <param name="value">A bool?.</param>
  552. /// <returns>A BsonValue.</returns>
  553. public static implicit operator BsonValue(bool? value)
  554. {
  555. return value.HasValue ? (BsonValue)(BsonBoolean)value.Value : BsonNull.Value;
  556. }
  557. /// <summary>
  558. /// Converts a byte[] to a BsonValue.
  559. /// </summary>
  560. /// <param name="value">A byte[].</param>
  561. /// <returns>A BsonValue.</returns>
  562. public static implicit operator BsonValue(byte[] value)
  563. {
  564. return (value != null) ? (BsonValue)new BsonBinaryData(value) : null;
  565. }
  566. /// <summary>
  567. /// Converts a DateTime to a BsonValue.
  568. /// </summary>
  569. /// <param name="value">A DateTime.</param>
  570. /// <returns>A BsonValue.</returns>
  571. public static implicit operator BsonValue(DateTime value)
  572. {
  573. return new BsonDateTime(value);
  574. }
  575. /// <summary>
  576. /// Converts a DateTime? to a BsonValue.
  577. /// </summary>
  578. /// <param name="value">A DateTime?.</param>
  579. /// <returns>A BsonValue.</returns>
  580. public static implicit operator BsonValue(DateTime? value)
  581. {
  582. return value.HasValue ? (BsonValue)new BsonDateTime(value.Value) : BsonNull.Value;
  583. }
  584. /// <summary>
  585. /// Converts a decimal to a BsonValue.
  586. /// </summary>
  587. /// <param name="value">A decimal.</param>
  588. /// <returns>A BsonValue.</returns>
  589. public static implicit operator BsonValue(decimal value)
  590. {
  591. return (BsonDecimal128)(Decimal128)value;
  592. }
  593. /// <summary>
  594. /// Converts a decimal? to a BsonValue.
  595. /// </summary>
  596. /// <param name="value">A decimal?.</param>
  597. /// <returns>A BsonValue.</returns>
  598. public static implicit operator BsonValue(decimal? value)
  599. {
  600. return value.HasValue ? (BsonValue)(BsonDecimal128)(Decimal128)value.Value : BsonNull.Value;
  601. }
  602. /// <summary>
  603. /// Converts a <see cref="Decimal128"/> to a BsonValue.
  604. /// </summary>
  605. /// <param name="value">A Decimal128.</param>
  606. /// <returns>A BsonValue.</returns>
  607. public static implicit operator BsonValue(Decimal128 value)
  608. {
  609. return (BsonDecimal128)value;
  610. }
  611. /// <summary>
  612. /// Converts a nullable <see cref="Decimal128"/> to a BsonValue.
  613. /// </summary>
  614. /// <param name="value">A Decimal128?.</param>
  615. /// <returns>A BsonValue.</returns>
  616. public static implicit operator BsonValue(Decimal128? value)
  617. {
  618. return value.HasValue ? (BsonValue)(BsonDecimal128)value.Value : BsonNull.Value;
  619. }
  620. /// <summary>
  621. /// Converts a double to a BsonValue.
  622. /// </summary>
  623. /// <param name="value">A double.</param>
  624. /// <returns>A BsonValue.</returns>
  625. public static implicit operator BsonValue(double value)
  626. {
  627. return (BsonDouble)value;
  628. }
  629. /// <summary>
  630. /// Converts a double? to a BsonValue.
  631. /// </summary>
  632. /// <param name="value">A double?.</param>
  633. /// <returns>A BsonValue.</returns>
  634. public static implicit operator BsonValue(double? value)
  635. {
  636. return value.HasValue ? (BsonValue)(BsonDouble)value.Value : BsonNull.Value;
  637. }
  638. /// <summary>
  639. /// Converts an Enum to a BsonValue.
  640. /// </summary>
  641. /// <param name="value">An Enum.</param>
  642. /// <returns>A BsonValue.</returns>
  643. public static implicit operator BsonValue(Enum value)
  644. {
  645. return BsonTypeMapper.MapToBsonValue(value);
  646. }
  647. /// <summary>
  648. /// Converts a Guid to a BsonValue.
  649. /// </summary>
  650. /// <param name="value">A Guid.</param>
  651. /// <returns>A BsonValue.</returns>
  652. public static implicit operator BsonValue(Guid value)
  653. {
  654. return new BsonBinaryData(value);
  655. }
  656. /// <summary>
  657. /// Converts a Guid? to a BsonValue.
  658. /// </summary>
  659. /// <param name="value">A Guid?.</param>
  660. /// <returns>A BsonValue.</returns>
  661. public static implicit operator BsonValue(Guid? value)
  662. {
  663. return value.HasValue ? (BsonValue)new BsonBinaryData(value.Value) : BsonNull.Value;
  664. }
  665. /// <summary>
  666. /// Converts an int to a BsonValue.
  667. /// </summary>
  668. /// <param name="value">An int.</param>
  669. /// <returns>A BsonValue.</returns>
  670. public static implicit operator BsonValue(int value)
  671. {
  672. return (BsonInt32)value;
  673. }
  674. /// <summary>
  675. /// Converts an int? to a BsonValue.
  676. /// </summary>
  677. /// <param name="value">An int?.</param>
  678. /// <returns>A BsonValue.</returns>
  679. public static implicit operator BsonValue(int? value)
  680. {
  681. return value.HasValue ? (BsonValue)(BsonInt32)value.Value : BsonNull.Value;
  682. }
  683. /// <summary>
  684. /// Converts a long to a BsonValue.
  685. /// </summary>
  686. /// <param name="value">A long.</param>
  687. /// <returns>A BsonValue.</returns>
  688. public static implicit operator BsonValue(long value)
  689. {
  690. return (BsonInt64)value;
  691. }
  692. /// <summary>
  693. /// Converts a long? to a BsonValue.
  694. /// </summary>
  695. /// <param name="value">A long?.</param>
  696. /// <returns>A BsonValue.</returns>
  697. public static implicit operator BsonValue(long? value)
  698. {
  699. return value.HasValue ? (BsonValue)(BsonInt64)value.Value : BsonNull.Value;
  700. }
  701. /// <summary>
  702. /// Converts an ObjectId to a BsonValue.
  703. /// </summary>
  704. /// <param name="value">An ObjectId.</param>
  705. /// <returns>A BsonValue.</returns>
  706. public static implicit operator BsonValue(ObjectId value)
  707. {
  708. return new BsonObjectId(value);
  709. }
  710. /// <summary>
  711. /// Converts an ObjectId? to a BsonValue.
  712. /// </summary>
  713. /// <param name="value">An ObjectId?.</param>
  714. /// <returns>A BsonValue.</returns>
  715. public static implicit operator BsonValue(ObjectId? value)
  716. {
  717. return value.HasValue ? (BsonValue)new BsonObjectId(value.Value) : BsonNull.Value;
  718. }
  719. /// <summary>
  720. /// Converts a Regex to a BsonValue.
  721. /// </summary>
  722. /// <param name="value">A Regex.</param>
  723. /// <returns>A BsonValue.</returns>
  724. public static implicit operator BsonValue(Regex value)
  725. {
  726. return (value != null) ? (BsonValue)new BsonRegularExpression(value) : null;
  727. }
  728. /// <summary>
  729. /// Converts a string to a BsonValue.
  730. /// </summary>
  731. /// <param name="value">A string.</param>
  732. /// <returns>A BsonValue.</returns>
  733. public static implicit operator BsonValue(string value)
  734. {
  735. return (value != null) ? (BsonValue)(BsonString)value : null;
  736. }
  737. /// <summary>
  738. /// Casts a BsonValue to a byte[].
  739. /// </summary>
  740. /// <param name="value">The BsonValue.</param>
  741. /// <returns>A byte[].</returns>
  742. public static explicit operator byte[](BsonValue value)
  743. {
  744. return (value == null) ? null : value.AsByteArray;
  745. }
  746. /// <summary>
  747. /// Casts a BsonValue to a DateTime.
  748. /// </summary>
  749. /// <param name="value">The BsonValue.</param>
  750. /// <returns>A DateTime.</returns>
  751. public static explicit operator DateTime(BsonValue value)
  752. {
  753. if (value == null)
  754. {
  755. throw new ArgumentNullException("value");
  756. }
  757. if (!(value is BsonDateTime))
  758. {
  759. throw new InvalidCastException();
  760. }
  761. return value.ToUniversalTime();
  762. }
  763. /// <summary>
  764. /// Casts a BsonValue to a DateTime?.
  765. /// </summary>
  766. /// <param name="value">The BsonValue.</param>
  767. /// <returns>A DateTime?.</returns>
  768. public static explicit operator DateTime?(BsonValue value)
  769. {
  770. if (value != null && !((value is BsonDateTime) || (value is BsonNull)))
  771. {
  772. throw new InvalidCastException();
  773. }
  774. return (value == null) ? null : value.ToNullableUniversalTime();
  775. }
  776. /// <summary>
  777. /// Casts a BsonValue to a decimal.
  778. /// </summary>
  779. /// <param name="value">The BsonValue.</param>
  780. /// <returns>A decimal.</returns>
  781. public static explicit operator decimal(BsonValue value)
  782. {
  783. if (value == null)
  784. {
  785. throw new ArgumentNullException("value");
  786. }
  787. return value.AsDecimal;
  788. }
  789. /// <summary>
  790. /// Casts a BsonValue to a decimal?.
  791. /// </summary>
  792. /// <param name="value">The BsonValue.</param>
  793. /// <returns>A decimal?.</returns>
  794. public static explicit operator decimal?(BsonValue value)
  795. {
  796. return (value == null) ? null : value.AsNullableDecimal;
  797. }
  798. /// <summary>
  799. /// Casts a BsonValue to a <see cref="Decimal128"/>.
  800. /// </summary>
  801. /// <param name="value">The BsonValue.</param>
  802. /// <returns>A <see cref="Decimal128"/>.</returns>
  803. public static explicit operator Decimal128(BsonValue value)
  804. {
  805. if (value == null)
  806. {
  807. throw new ArgumentNullException("value");
  808. }
  809. return value.AsDecimal128;
  810. }
  811. /// <summary>
  812. /// Casts a BsonValue to a nullable <see cref="Decimal128"/>?.
  813. /// </summary>
  814. /// <param name="value">The BsonValue.</param>
  815. /// <returns>A nullable <see cref="Decimal128"/>.</returns>
  816. public static explicit operator Decimal128?(BsonValue value)
  817. {
  818. return (value == null) ? null : value.AsNullableDecimal128;
  819. }
  820. /// <summary>
  821. /// Casts a BsonValue to a double.
  822. /// </summary>
  823. /// <param name="value">The BsonValue.</param>
  824. /// <returns>A double.</returns>
  825. public static explicit operator double(BsonValue value)
  826. {
  827. if (value == null)
  828. {
  829. throw new ArgumentNullException("value");
  830. }
  831. return value.AsDouble;
  832. }
  833. /// <summary>
  834. /// Casts a BsonValue to a double?.
  835. /// </summary>
  836. /// <param name="value">The BsonValue.</param>
  837. /// <returns>A double?.</returns>
  838. public static explicit operator double?(BsonValue value)
  839. {
  840. return (value == null) ? null : value.AsNullableDouble;
  841. }
  842. /// <summary>
  843. /// Casts a BsonValue to a Guid.
  844. /// </summary>
  845. /// <param name="value">The BsonValue.</param>
  846. /// <returns>A Guid.</returns>
  847. public static explicit operator Guid(BsonValue value)
  848. {
  849. if (value == null)
  850. {
  851. throw new ArgumentNullException("value");
  852. }
  853. return value.AsGuid;
  854. }
  855. /// <summary>
  856. /// Casts a BsonValue to a Guid?.
  857. /// </summary>
  858. /// <param name="value">The BsonValue.</param>
  859. /// <returns>A Guid?.</returns>
  860. public static explicit operator Guid?(BsonValue value)
  861. {
  862. return (value == null) ? null : value.AsNullableGuid;
  863. }
  864. /// <summary>
  865. /// Casts a BsonValue to an int.
  866. /// </summary>
  867. /// <param name="value">The BsonValue.</param>
  868. /// <returns>An int.</returns>
  869. public static explicit operator int(BsonValue value)
  870. {
  871. if (value == null)
  872. {
  873. throw new ArgumentNullException("value");
  874. }
  875. return value.AsInt32;
  876. }
  877. /// <summary>
  878. /// Casts a BsonValue to an int?.
  879. /// </summary>
  880. /// <param name="value">The BsonValue.</param>
  881. /// <returns>An int?.</returns>
  882. public static explicit operator int?(BsonValue value)
  883. {
  884. return value == null ? null : value.AsNullableInt32;
  885. }
  886. /// <summary>
  887. /// Casts a BsonValue to a long.
  888. /// </summary>
  889. /// <param name="value">The BsonValue.</param>
  890. /// <returns>A long.</returns>
  891. public static explicit operator long(BsonValue value)
  892. {
  893. if (value == null)
  894. {
  895. throw new ArgumentNullException("value");
  896. }
  897. return value.AsInt64;
  898. }
  899. /// <summary>
  900. /// Casts a BsonValue to a long?.
  901. /// </summary>
  902. /// <param name="value">The BsonValue.</param>
  903. /// <returns>A long?.</returns>
  904. public static explicit operator long?(BsonValue value)
  905. {
  906. return (value == null) ? null : value.AsNullableInt64;
  907. }
  908. /// <summary>
  909. /// Casts a BsonValue to an ObjectId.
  910. /// </summary>
  911. /// <param name="value">The BsonValue.</param>
  912. /// <returns>An ObjectId.</returns>
  913. public static explicit operator ObjectId(BsonValue value)
  914. {
  915. if (value == null)
  916. {
  917. throw new ArgumentNullException("value");
  918. }
  919. return value.AsObjectId;
  920. }
  921. /// <summary>
  922. /// Casts a BsonValue to an ObjectId?.
  923. /// </summary>
  924. /// <param name="value">The BsonValue.</param>
  925. /// <returns>An ObjectId?.</returns>
  926. public static explicit operator ObjectId?(BsonValue value)
  927. {
  928. return (value == null) ? null : value.AsNullableObjectId;
  929. }
  930. /// <summary>
  931. /// Casts a BsonValue to a Regex.
  932. /// </summary>
  933. /// <param name="value">The BsonValue.</param>
  934. /// <returns>A Regex.</returns>
  935. public static explicit operator Regex(BsonValue value)
  936. {
  937. return (value == null) ? null : value.AsRegex;
  938. }
  939. /// <summary>
  940. /// Casts a BsonValue to a string.
  941. /// </summary>
  942. /// <param name="value">The BsonValue.</param>
  943. /// <returns>A string.</returns>
  944. public static explicit operator string(BsonValue value)
  945. {
  946. return (value == null) ? null : value.AsString;
  947. }
  948. /// <summary>
  949. /// Compares two BsonValues.
  950. /// </summary>
  951. /// <param name="lhs">The first BsonValue.</param>
  952. /// <param name="rhs">The other BsonValue.</param>
  953. /// <returns>True if the first BsonValue is less than the other one.</returns>
  954. public static bool operator <(BsonValue lhs, BsonValue rhs)
  955. {
  956. if (object.ReferenceEquals(lhs, null) && object.ReferenceEquals(rhs, null)) { return false; }
  957. if (object.ReferenceEquals(lhs, null)) { return true; }
  958. if (object.ReferenceEquals(rhs, null)) { return false; }
  959. return lhs.CompareTo(rhs) < 0;
  960. }
  961. /// <summary>
  962. /// Compares two BsonValues.
  963. /// </summary>
  964. /// <param name="lhs">The first BsonValue.</param>
  965. /// <param name="rhs">The other BsonValue.</param>
  966. /// <returns>True if the first BsonValue is less than or equal to the other one.</returns>
  967. public static bool operator <=(BsonValue lhs, BsonValue rhs)
  968. {
  969. if (object.ReferenceEquals(lhs, null) && object.ReferenceEquals(rhs, null)) { return true; }
  970. if (object.ReferenceEquals(lhs, null)) { return true; }
  971. if (object.ReferenceEquals(rhs, null)) { return false; }
  972. return lhs.CompareTo(rhs) <= 0;
  973. }
  974. /// <summary>
  975. /// Compares two BsonValues.
  976. /// </summary>
  977. /// <param name="lhs">The first BsonValue.</param>
  978. /// <param name="rhs">The other BsonValue.</param>
  979. /// <returns>True if the two BsonValues are not equal according to ==.</returns>
  980. public static bool operator !=(BsonValue lhs, BsonValue rhs)
  981. {
  982. return !(lhs == rhs);
  983. }
  984. /// <summary>
  985. /// Compares two BsonValues.
  986. /// </summary>
  987. /// <param name="lhs">The first BsonValue.</param>
  988. /// <param name="rhs">The other BsonValue.</param>
  989. /// <returns>True if the two BsonValues are equal according to ==.</returns>
  990. public static bool operator ==(BsonValue lhs, BsonValue rhs)
  991. {
  992. if (object.ReferenceEquals(lhs, null)) { return object.ReferenceEquals(rhs, null); }
  993. if (object.ReferenceEquals(rhs, null)) { return false; } // don't check type because sometimes different types can be ==
  994. return lhs.OperatorEqualsImplementation(rhs); // some subclasses override OperatorEqualsImplementation
  995. }
  996. /// <summary>
  997. /// Compares two BsonValues.
  998. /// </summary>
  999. /// <param name="lhs">The first BsonValue.</param>
  1000. /// <param name="rhs">The other BsonValue.</param>
  1001. /// <returns>True if the first BsonValue is greater than the other one.</returns>
  1002. public static bool operator >(BsonValue lhs, BsonValue rhs)
  1003. {
  1004. return !(lhs <= rhs);
  1005. }
  1006. /// <summary>
  1007. /// Compares two BsonValues.
  1008. /// </summary>
  1009. /// <param name="lhs">The first BsonValue.</param>
  1010. /// <param name="rhs">The other BsonValue.</param>
  1011. /// <returns>True if the first BsonValue is greater than or equal to the other one.</returns>
  1012. public static bool operator >=(BsonValue lhs, BsonValue rhs)
  1013. {
  1014. return !(lhs < rhs);
  1015. }
  1016. // public indexers
  1017. /// <summary>
  1018. /// Gets or sets a value by position (only applies to BsonDocument and BsonArray).
  1019. /// </summary>
  1020. /// <param name="index">The position.</param>
  1021. /// <returns>The value.</returns>
  1022. public virtual BsonValue this[int index]
  1023. {
  1024. get
  1025. {
  1026. var message = string.Format("{0} does not support indexing by position (only BsonDocument and BsonArray do).", this.GetType().Name);
  1027. throw new NotSupportedException(message);
  1028. }
  1029. set
  1030. {
  1031. var message = string.Format("{0} does not support indexing by position (only BsonDocument and BsonArray do).", this.GetType().Name);
  1032. throw new NotSupportedException(message);
  1033. }
  1034. }
  1035. /// <summary>
  1036. /// Gets or sets a value by name (only applies to BsonDocument).
  1037. /// </summary>
  1038. /// <param name="name">The name.</param>
  1039. /// <returns>The value.</returns>
  1040. public virtual BsonValue this[string name]
  1041. {
  1042. get
  1043. {
  1044. var message = string.Format("{0} does not support indexing by name (only BsonDocument does).", this.GetType().Name);
  1045. throw new NotSupportedException(message);
  1046. }
  1047. set
  1048. {
  1049. var message = string.Format("{0} does not support indexing by name (only BsonDocument does).", this.GetType().Name);
  1050. throw new NotSupportedException(message);
  1051. }
  1052. }
  1053. // public static methods
  1054. /// <summary>
  1055. /// Creates a new instance of the BsonValue class.
  1056. /// </summary>
  1057. /// <param name="value">A value to be mapped to a BsonValue.</param>
  1058. /// <returns>A BsonValue.</returns>
  1059. public static BsonValue Create(object value)
  1060. {
  1061. // optimize away the call to MapToBsonValue for the most common cases
  1062. if (value == null)
  1063. {
  1064. return BsonNull.Value;
  1065. }
  1066. else if (value is BsonValue)
  1067. {
  1068. return (BsonValue)value;
  1069. }
  1070. else if (value is int)
  1071. {
  1072. return (BsonInt32)(int)value;
  1073. }
  1074. else if (value is string)
  1075. {
  1076. return (BsonString)(string)value;
  1077. }
  1078. else if (value is bool)
  1079. {
  1080. return (BsonBoolean)((bool)value);
  1081. }
  1082. else if (value is DateTime)
  1083. {
  1084. return new BsonDateTime((DateTime)value);
  1085. }
  1086. else if (value is long)
  1087. {
  1088. return (BsonInt64)(long)value;
  1089. }
  1090. else if (value is double)
  1091. {
  1092. return (BsonDouble)(double)value;
  1093. }
  1094. else
  1095. {
  1096. return BsonTypeMapper.MapToBsonValue(value);
  1097. }
  1098. }
  1099. // public methods
  1100. /// <summary>
  1101. /// Creates a shallow clone of the BsonValue (see also DeepClone).
  1102. /// </summary>
  1103. /// <returns>A shallow clone of the BsonValue.</returns>
  1104. public virtual BsonValue Clone()
  1105. {
  1106. return this; // subclasses override Clone if necessary
  1107. }
  1108. /// <summary>
  1109. /// Compares this BsonValue to another BsonValue.
  1110. /// </summary>
  1111. /// <param name="other">The other BsonValue.</param>
  1112. /// <returns>A 32-bit signed integer that indicates whether this BsonValue is less than, equal to, or greather than the other BsonValue.</returns>
  1113. public abstract int CompareTo(BsonValue other);
  1114. /// <summary>
  1115. /// Compares the type of this BsonValue to the type of another BsonValue.
  1116. /// </summary>
  1117. /// <param name="other">The other BsonValue.</param>
  1118. /// <returns>A 32-bit signed integer that indicates whether the type of this BsonValue is less than, equal to, or greather than the type of the other BsonValue.</returns>
  1119. public int CompareTypeTo(BsonValue other)
  1120. {
  1121. if (object.ReferenceEquals(other, null)) { return 1; }
  1122. return __bsonTypeSortOrder[BsonType].CompareTo(__bsonTypeSortOrder[other.BsonType]);
  1123. }
  1124. /// <summary>
  1125. /// Creates a deep clone of the BsonValue (see also Clone).
  1126. /// </summary>
  1127. /// <returns>A deep clone of the BsonValue.</returns>
  1128. public virtual BsonValue DeepClone()
  1129. {
  1130. return this; // subclasses override DeepClone if necessary
  1131. }
  1132. /// <summary>
  1133. /// Compares this BsonValue to another BsonValue.
  1134. /// </summary>
  1135. /// <param name="rhs">The other BsonValue.</param>
  1136. /// <returns>True if the two BsonValue values are equal.</returns>
  1137. public bool Equals(BsonValue rhs)
  1138. {
  1139. return Equals((object)rhs);
  1140. }
  1141. /// <summary>
  1142. /// Compares this BsonValue to another object.
  1143. /// </summary>
  1144. /// <param name="obj">The other object.</param>
  1145. /// <returns>True if the other object is a BsonValue and equal to this one.</returns>
  1146. public abstract override bool Equals(object obj);
  1147. /// <summary>
  1148. /// Gets the hash code.
  1149. /// </summary>
  1150. /// <returns>The hash code.</returns>
  1151. public abstract override int GetHashCode();
  1152. /// <summary>
  1153. /// Converts this BsonValue to a Boolean (using the JavaScript definition of truthiness).
  1154. /// </summary>
  1155. /// <returns>A Boolean.</returns>
  1156. public virtual bool ToBoolean()
  1157. {
  1158. // some subclasses override as appropriate
  1159. return true; // everything else is true
  1160. }
  1161. /// <summary>
  1162. /// Converts this BsonValue to a Decimal.
  1163. /// </summary>
  1164. /// <returns>A Decimal.</returns>
  1165. public virtual decimal ToDecimal()
  1166. {
  1167. var message = string.Format("{0} does not support ToDecimal.", this.GetType().Name);
  1168. throw new NotSupportedException(message);
  1169. }
  1170. /// <summary>
  1171. /// Converts this BsonValue to a Decimal128.
  1172. /// </summary>
  1173. /// <returns>A Decimal128.</returns>
  1174. public virtual Decimal128 ToDecimal128()
  1175. {
  1176. var message = string.Format("{0} does not support ToDecimal128.", this.GetType().Name);
  1177. throw new NotSupportedException(message);
  1178. }
  1179. /// <summary>
  1180. /// Converts this BsonValue to a Double.
  1181. /// </summary>
  1182. /// <returns>A Double.</returns>
  1183. public virtual double ToDouble()
  1184. {
  1185. var message = string.Format("{0} does not support ToDouble.", this.GetType().Name);
  1186. throw new NotSupportedException(message);
  1187. }
  1188. /// <summary>
  1189. /// Converts this BsonValue to an Int32.
  1190. /// </summary>
  1191. /// <returns>An Int32.</returns>
  1192. public virtual int ToInt32()
  1193. {
  1194. var message = string.Format("{0} does not support ToInt32.", this.GetType().Name);
  1195. throw new NotSupportedException(message);
  1196. }
  1197. /// <summary>
  1198. /// Converts this BsonValue to an Int64.
  1199. /// </summary>
  1200. /// <returns>An Int64.</returns>
  1201. public virtual long ToInt64()
  1202. {
  1203. var message = string.Format("{0} does not support ToInt64.", this.GetType().Name);
  1204. throw new NotSupportedException(message);
  1205. }
  1206. /// <summary>
  1207. /// Converts this BsonValue to a DateTime in local time.
  1208. /// </summary>
  1209. /// <returns>A DateTime.</returns>
  1210. public virtual DateTime ToLocalTime()
  1211. {
  1212. var message = string.Format("{0} does not support ToLocalTime.", this.GetType().Name);
  1213. throw new NotSupportedException(message);
  1214. }
  1215. /// <summary>
  1216. /// Converts this BsonValue to a DateTime? in local time.
  1217. /// </summary>
  1218. /// <returns>A DateTime?.</returns>
  1219. public virtual DateTime? ToNullableLocalTime()
  1220. {
  1221. var message = string.Format("{0} does not support ToNullableLocalTime.", this.GetType().Name);
  1222. throw new NotSupportedException(message);
  1223. }
  1224. /// <summary>
  1225. /// Converts this BsonValue to a DateTime? in UTC.
  1226. /// </summary>
  1227. /// <returns>A DateTime?.</returns>
  1228. public virtual DateTime? ToNullableUniversalTime()
  1229. {
  1230. var message = string.Format("{0} does not support ToNullableUniversalTime.", this.GetType().Name);
  1231. throw new NotSupportedException(message);
  1232. }
  1233. /// <summary>
  1234. /// Converts this BsonValue to a DateTime in UTC.
  1235. /// </summary>
  1236. /// <returns>A DateTime.</returns>
  1237. public virtual DateTime ToUniversalTime()
  1238. {
  1239. var message = string.Format("{0} does not support ToUniversalTime.", this.GetType().Name);
  1240. throw new NotSupportedException(message);
  1241. }
  1242. // protected methods
  1243. /// <summary>
  1244. /// Implementation of the IConvertible GetTypeCode method.
  1245. /// </summary>
  1246. /// <returns>The TypeCode.</returns>
  1247. protected virtual TypeCode IConvertibleGetTypeCodeImplementation()
  1248. {
  1249. return TypeCode.Object;
  1250. }
  1251. /// <summary>
  1252. /// Implementation of the IConvertible ToBoolean method.
  1253. /// </summary>
  1254. /// <param name="provider">The format provider.</param>
  1255. /// <returns>A bool.</returns>
  1256. protected virtual bool IConvertibleToBooleanImplementation(IFormatProvider provider)
  1257. {
  1258. throw new InvalidCastException();
  1259. }
  1260. /// <summary>
  1261. /// Implementation of the IConvertible ToByte method.
  1262. /// </summary>
  1263. /// <param name="provider">The format provider.</param>
  1264. /// <returns>A byte.</returns>
  1265. protected virtual byte IConvertibleToByteImplementation(IFormatProvider provider)
  1266. {
  1267. throw new InvalidCastException();
  1268. }
  1269. /// <summary>
  1270. /// Implementation of the IConvertible ToChar method.
  1271. /// </summary>
  1272. /// <param name="provider">The format provider.</param>
  1273. /// <returns>A char.</returns>
  1274. protected virtual char IConvertibleToCharImplementation(IFormatProvider provider)
  1275. {
  1276. throw new InvalidCastException();
  1277. }
  1278. /// <summary>
  1279. /// Implementation of the IConvertible ToDateTime method.
  1280. /// </summary>
  1281. /// <param name="provider">The format provider.</param>
  1282. /// <returns>A DateTime.</returns>
  1283. protected virtual DateTime IConvertibleToDateTimeImplementation(IFormatProvider provider)
  1284. {
  1285. throw new InvalidCastException();
  1286. }
  1287. /// <summary>
  1288. /// Implementation of the IConvertible ToDecimal method.
  1289. /// </summary>
  1290. /// <param name="provider">The format provider.</param>
  1291. /// <returns>A decimal.</returns>
  1292. protected virtual decimal IConvertibleToDecimalImplementation(IFormatProvider provider)
  1293. {
  1294. throw new InvalidCastException();
  1295. }
  1296. /// <summary>
  1297. /// Implementation of the IConvertible ToDouble method.
  1298. /// </summary>
  1299. /// <param name="provider">The format provider.</param>
  1300. /// <returns>A double.</returns>
  1301. protected virtual double IConvertibleToDoubleImplementation(IFormatProvider provider)
  1302. {
  1303. throw new InvalidCastException();
  1304. }
  1305. /// <summary>
  1306. /// Implementation of the IConvertible ToInt16 method.
  1307. /// </summary>
  1308. /// <param name="provider">The format provider.</param>
  1309. /// <returns>A short.</returns>
  1310. protected virtual short IConvertibleToInt16Implementation(IFormatProvider provider)
  1311. {
  1312. throw new InvalidCastException();
  1313. }
  1314. /// <summary>
  1315. /// Implementation of the IConvertible ToInt32 method.
  1316. /// </summary>
  1317. /// <param name="provider">The format provider.</param>
  1318. /// <returns>An int.</returns>
  1319. protected virtual int IConvertibleToInt32Implementation(IFormatProvider provider)
  1320. {
  1321. throw new InvalidCastException();
  1322. }
  1323. /// <summary>
  1324. /// Implementation of the IConvertible ToInt64 method.
  1325. /// </summary>
  1326. /// <param name="provider">The format provider.</param>
  1327. /// <returns>A long.</returns>
  1328. protected virtual long IConvertibleToInt64Implementation(IFormatProvider provider)
  1329. {
  1330. throw new InvalidCastException();
  1331. }
  1332. /// <summary>
  1333. /// Implementation of the IConvertible ToSByte method.
  1334. /// </summary>
  1335. /// <param name="provider">The format provider.</param>
  1336. /// <returns>An sbyte.</returns>
  1337. #pragma warning disable 3002
  1338. protected virtual sbyte IConvertibleToSByteImplementation(IFormatProvider provider)
  1339. {
  1340. throw new InvalidCastException();
  1341. }
  1342. #pragma warning restore
  1343. /// <summary>
  1344. /// Implementation of the IConvertible ToSingle method.
  1345. /// </summary>
  1346. /// <param name="provider">The format provider.</param>
  1347. /// <returns>A float.</returns>
  1348. protected virtual float IConvertibleToSingleImplementation(IFormatProvider provider)
  1349. {
  1350. throw new InvalidCastException();
  1351. }
  1352. /// <summary>
  1353. /// Implementation of the IConvertible ToString method.
  1354. /// </summary>
  1355. /// <param name="provider">The format provider.</param>
  1356. /// <returns>A string.</returns>
  1357. protected virtual string IConvertibleToStringImplementation(IFormatProvider provider)
  1358. {
  1359. throw new InvalidCastException();
  1360. }
  1361. /// <summary>
  1362. /// Implementation of the IConvertible ToUInt16 method.
  1363. /// </summary>
  1364. /// <param name="provider">The format provider.</param>
  1365. /// <returns>A ushort.</returns>
  1366. #pragma warning disable 3002
  1367. protected virtual ushort IConvertibleToUInt16Implementation(IFormatProvider provider)
  1368. {
  1369. throw new InvalidCastException();
  1370. }
  1371. #pragma warning restore
  1372. /// <summary>
  1373. /// Implementation of the IConvertible ToUInt32 method.
  1374. /// </summary>
  1375. /// <param name="provider">The format provider.</param>
  1376. /// <returns>A uint.</returns>
  1377. #pragma warning disable 3002
  1378. protected virtual uint IConvertibleToUInt32Implementation(IFormatProvider provider)
  1379. {
  1380. throw new InvalidCastException();
  1381. }
  1382. #pragma warning restore
  1383. /// <summary>
  1384. /// Implementation of the IConvertible ToUInt64 method.
  1385. /// </summary>
  1386. /// <param name="provider">The format provider.</param>
  1387. /// <returns>A ulong.</returns>
  1388. #pragma warning disable 3002
  1389. protected virtual ulong IConvertibleToUInt64Implementation(IFormatProvider provider)
  1390. {
  1391. throw new InvalidCastException();
  1392. }
  1393. #pragma warning restore
  1394. /// <summary>
  1395. /// Implementation of operator ==.
  1396. /// </summary>
  1397. /// <param name="rhs">The other BsonValue.</param>
  1398. /// <returns>True if the two BsonValues are equal according to ==.</returns>
  1399. protected virtual bool OperatorEqualsImplementation(BsonValue rhs)
  1400. {
  1401. return Equals(rhs); // default implementation of == is to call Equals
  1402. }
  1403. // explicit IConvertible implementation
  1404. TypeCode IConvertible.GetTypeCode()
  1405. {
  1406. return IConvertibleGetTypeCodeImplementation();
  1407. }
  1408. bool IConvertible.ToBoolean(IFormatProvider provider)
  1409. {
  1410. return IConvertibleToBooleanImplementation(provider);
  1411. }
  1412. byte IConvertible.ToByte(IFormatProvider provider)
  1413. {
  1414. return IConvertibleToByteImplementation(provider);
  1415. }
  1416. char IConvertible.ToChar(IFormatProvider provider)
  1417. {
  1418. return IConvertibleToCharImplementation(provider);
  1419. }
  1420. DateTime IConvertible.ToDateTime(IFormatProvider provider)
  1421. {
  1422. return IConvertibleToDateTimeImplementation(provider);
  1423. }
  1424. decimal IConvertible.ToDecimal(IFormatProvider provider)
  1425. {
  1426. return IConvertibleToDecimalImplementation(provider);
  1427. }
  1428. double IConvertible.ToDouble(IFormatProvider provider)
  1429. {
  1430. return IConvertibleToDoubleImplementation(provider);
  1431. }
  1432. short IConvertible.ToInt16(IFormatProvider provider)
  1433. {
  1434. return IConvertibleToInt16Implementation(provider);
  1435. }
  1436. int IConvertible.ToInt32(IFormatProvider provider)
  1437. {
  1438. return IConvertibleToInt32Implementation(provider);
  1439. }
  1440. long IConvertible.ToInt64(IFormatProvider provider)
  1441. {
  1442. return IConvertibleToInt64Implementation(provider);
  1443. }
  1444. sbyte IConvertible.ToSByte(IFormatProvider provider)
  1445. {
  1446. return IConvertibleToSByteImplementation(provider);
  1447. }
  1448. float IConvertible.ToSingle(IFormatProvider provider)
  1449. {
  1450. return IConvertibleToSingleImplementation(provider);
  1451. }
  1452. string IConvertible.ToString(IFormatProvider provider)
  1453. {
  1454. return IConvertibleToStringImplementation(provider);
  1455. }
  1456. object IConvertible.ToType(Type conversionType, IFormatProvider provider)
  1457. {
  1458. if (conversionType == typeof(object))
  1459. {
  1460. return this;
  1461. }
  1462. switch (BsonType)
  1463. {
  1464. case BsonType.Boolean: return Convert.ChangeType(this.AsBoolean, conversionType, provider);
  1465. case BsonType.DateTime: return Convert.ChangeType(this.ToUniversalTime(), conversionType, provider);
  1466. case BsonType.Decimal128: return Convert.ChangeType(this.AsDecimal128, conversionType, provider);
  1467. case BsonType.Double: return Convert.ChangeType(this.AsDouble, conversionType, provider);
  1468. case BsonType.Int32: return Convert.ChangeType(this.AsInt32, conversionType, provider);
  1469. case BsonType.Int64: return Convert.ChangeType(this.AsInt64, conversionType, provider);
  1470. case BsonType.ObjectId: return Convert.ChangeType(this.AsObjectId, conversionType, provider);
  1471. case BsonType.String: return Convert.ChangeType(this.AsString, conversionType, provider);
  1472. default: throw new InvalidCastException();
  1473. }
  1474. }
  1475. ushort IConvertible.ToUInt16(IFormatProvider provider)
  1476. {
  1477. return IConvertibleToUInt16Implementation(provider);
  1478. }
  1479. uint IConvertible.ToUInt32(IFormatProvider provider)
  1480. {
  1481. return IConvertibleToUInt32Implementation(provider);
  1482. }
  1483. ulong IConvertible.ToUInt64(IFormatProvider provider)
  1484. {
  1485. return IConvertibleToUInt64Implementation(provider);
  1486. }
  1487. }
  1488. }