JsonToken.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  1. /* Copyright 2010-2014 MongoDB Inc.
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. using System;
  16. namespace MongoDB.Bson.IO
  17. {
  18. /// <summary>
  19. /// Represents a JSON token type.
  20. /// </summary>
  21. public enum JsonTokenType
  22. {
  23. /// <summary>
  24. /// An invalid token.
  25. /// </summary>
  26. Invalid,
  27. /// <summary>
  28. /// A begin array token (a '[').
  29. /// </summary>
  30. BeginArray,
  31. /// <summary>
  32. /// A begin object token (a '{').
  33. /// </summary>
  34. BeginObject,
  35. /// <summary>
  36. /// An end array token (a ']').
  37. /// </summary>
  38. EndArray,
  39. /// <summary>
  40. /// A left parenthesis (a '(').
  41. /// </summary>
  42. LeftParen,
  43. /// <summary>
  44. /// A right parenthesis (a ')').
  45. /// </summary>
  46. RightParen,
  47. /// <summary>
  48. /// An end object token (a '}').
  49. /// </summary>
  50. EndObject,
  51. /// <summary>
  52. /// A colon token (a ':').
  53. /// </summary>
  54. Colon,
  55. /// <summary>
  56. /// A comma token (a ',').
  57. /// </summary>
  58. Comma,
  59. /// <summary>
  60. /// A DateTime token.
  61. /// </summary>
  62. DateTime,
  63. /// <summary>
  64. /// A Double token.
  65. /// </summary>
  66. Double,
  67. /// <summary>
  68. /// An Int32 token.
  69. /// </summary>
  70. Int32,
  71. /// <summary>
  72. /// And Int64 token.
  73. /// </summary>
  74. Int64,
  75. /// <summary>
  76. /// An ObjectId token.
  77. /// </summary>
  78. ObjectId,
  79. /// <summary>
  80. /// A regular expression token.
  81. /// </summary>
  82. RegularExpression,
  83. /// <summary>
  84. /// A string token.
  85. /// </summary>
  86. String,
  87. /// <summary>
  88. /// An unquoted string token.
  89. /// </summary>
  90. UnquotedString,
  91. /// <summary>
  92. /// An end of file token.
  93. /// </summary>
  94. EndOfFile
  95. }
  96. /// <summary>
  97. /// Represents a JSON token.
  98. /// </summary>
  99. public class JsonToken
  100. {
  101. // private fields
  102. private JsonTokenType _type;
  103. private string _lexeme;
  104. // constructors
  105. /// <summary>
  106. /// Initializes a new instance of the JsonToken class.
  107. /// </summary>
  108. /// <param name="type">The token type.</param>
  109. /// <param name="lexeme">The lexeme.</param>
  110. public JsonToken(JsonTokenType type, string lexeme)
  111. {
  112. _type = type;
  113. _lexeme = lexeme;
  114. }
  115. // public properties
  116. /// <summary>
  117. /// Gets the token type.
  118. /// </summary>
  119. public JsonTokenType Type
  120. {
  121. get { return _type; }
  122. }
  123. /// <summary>
  124. /// Gets the lexeme.
  125. /// </summary>
  126. public string Lexeme
  127. {
  128. get { return _lexeme; }
  129. }
  130. /// <summary>
  131. /// Gets the value of a DateTime token.
  132. /// </summary>
  133. public virtual BsonDateTime DateTimeValue
  134. {
  135. get { throw new NotSupportedException(); }
  136. }
  137. /// <summary>
  138. /// Gets the value of a Double token.
  139. /// </summary>
  140. public virtual double DoubleValue
  141. {
  142. get { throw new NotSupportedException(); }
  143. }
  144. /// <summary>
  145. /// Gets the value of an Int32 token.
  146. /// </summary>
  147. public virtual int Int32Value
  148. {
  149. get { throw new NotSupportedException(); }
  150. }
  151. /// <summary>
  152. /// Gets the value of an Int64 token.
  153. /// </summary>
  154. public virtual long Int64Value
  155. {
  156. get { throw new NotSupportedException(); }
  157. }
  158. /// <summary>
  159. /// Gets a value indicating whether this token is number.
  160. /// </summary>
  161. /// <value>
  162. /// <c>true</c> if this token is number; otherwise, <c>false</c>.
  163. /// </value>
  164. public virtual bool IsNumber
  165. {
  166. get { return false; }
  167. }
  168. /// <summary>
  169. /// Gets the value of an ObjectId token.
  170. /// </summary>
  171. public virtual ObjectId ObjectIdValue
  172. {
  173. get { throw new NotSupportedException(); }
  174. }
  175. /// <summary>
  176. /// Gets the value of a regular expression token.
  177. /// </summary>
  178. public virtual BsonRegularExpression RegularExpressionValue
  179. {
  180. get { throw new NotSupportedException(); }
  181. }
  182. /// <summary>
  183. /// Gets the value of a string token.
  184. /// </summary>
  185. public virtual string StringValue
  186. {
  187. get { throw new NotSupportedException(); }
  188. }
  189. }
  190. /// <summary>
  191. /// Represents a DateTime JSON token.
  192. /// </summary>
  193. public class DateTimeJsonToken : JsonToken
  194. {
  195. // private fields
  196. private BsonDateTime _value;
  197. // constructors
  198. /// <summary>
  199. /// Initializes a new instance of the DateTimeJsonToken class.
  200. /// </summary>
  201. /// <param name="lexeme">The lexeme.</param>
  202. /// <param name="value">The DateTime value.</param>
  203. public DateTimeJsonToken(string lexeme, BsonDateTime value)
  204. : base(JsonTokenType.DateTime, lexeme)
  205. {
  206. _value = value;
  207. }
  208. // public properties
  209. /// <summary>
  210. /// Gets the value of a DateTime token.
  211. /// </summary>
  212. public override BsonDateTime DateTimeValue
  213. {
  214. get { return _value; }
  215. }
  216. }
  217. /// <summary>
  218. /// Represents a Double JSON token.
  219. /// </summary>
  220. public class DoubleJsonToken : JsonToken
  221. {
  222. // private fields
  223. private double _value;
  224. // constructors
  225. /// <summary>
  226. /// Initializes a new instance of the DoubleJsonToken class.
  227. /// </summary>
  228. /// <param name="lexeme">The lexeme.</param>
  229. /// <param name="value">The Double value.</param>
  230. public DoubleJsonToken(string lexeme, double value)
  231. : base(JsonTokenType.Double, lexeme)
  232. {
  233. _value = value;
  234. }
  235. // public properties
  236. /// <summary>
  237. /// Gets the value of a Double token.
  238. /// </summary>
  239. public override double DoubleValue
  240. {
  241. get { return _value; }
  242. }
  243. /// <summary>
  244. /// Gets the value of an Int32 token.
  245. /// </summary>
  246. public override int Int32Value
  247. {
  248. get { return (int)_value; }
  249. }
  250. /// <summary>
  251. /// Gets the value of an Int64 token.
  252. /// </summary>
  253. public override long Int64Value
  254. {
  255. get { return (long)_value; }
  256. }
  257. /// <summary>
  258. /// Gets a value indicating whether this token is number.
  259. /// </summary>
  260. /// <value>
  261. /// <c>true</c> if this token is number; otherwise, <c>false</c>.
  262. /// </value>
  263. public override bool IsNumber
  264. {
  265. get { return true; }
  266. }
  267. }
  268. /// <summary>
  269. /// Represents an Int32 JSON token.
  270. /// </summary>
  271. public class Int32JsonToken : JsonToken
  272. {
  273. // private fields
  274. private int _value;
  275. // constructors
  276. /// <summary>
  277. /// Initializes a new instance of the Int32JsonToken class.
  278. /// </summary>
  279. /// <param name="lexeme">The lexeme.</param>
  280. /// <param name="value">The Int32 value.</param>
  281. public Int32JsonToken(string lexeme, int value)
  282. : base(JsonTokenType.Int32, lexeme)
  283. {
  284. _value = value;
  285. }
  286. // public properties
  287. /// <summary>
  288. /// Gets the value of a Double token.
  289. /// </summary>
  290. public override double DoubleValue
  291. {
  292. get { return _value; }
  293. }
  294. /// <summary>
  295. /// Gets the value of an Int32 token.
  296. /// </summary>
  297. public override int Int32Value
  298. {
  299. get { return _value; }
  300. }
  301. /// <summary>
  302. /// Gets the value of an Int32 token as an Int64.
  303. /// </summary>
  304. public override long Int64Value
  305. {
  306. get { return _value; }
  307. }
  308. /// <summary>
  309. /// Gets a value indicating whether this token is number.
  310. /// </summary>
  311. /// <value>
  312. /// <c>true</c> if this token is number; otherwise, <c>false</c>.
  313. /// </value>
  314. public override bool IsNumber
  315. {
  316. get { return true; }
  317. }
  318. }
  319. /// <summary>
  320. /// Represents an Int64 JSON token.
  321. /// </summary>
  322. public class Int64JsonToken : JsonToken
  323. {
  324. // private fields
  325. private long _value;
  326. // constructors
  327. /// <summary>
  328. /// Initializes a new instance of the Int64JsonToken class.
  329. /// </summary>
  330. /// <param name="lexeme">The lexeme.</param>
  331. /// <param name="value">The Int64 value.</param>
  332. public Int64JsonToken(string lexeme, long value)
  333. : base(JsonTokenType.Int64, lexeme)
  334. {
  335. _value = value;
  336. }
  337. // public properties
  338. /// <summary>
  339. /// Gets the value of a Double token.
  340. /// </summary>
  341. public override double DoubleValue
  342. {
  343. get { return _value; }
  344. }
  345. /// <summary>
  346. /// Gets the value of an Int32 token.
  347. /// </summary>
  348. public override int Int32Value
  349. {
  350. get { return (int)_value; }
  351. }
  352. /// <summary>
  353. /// Gets the value of an Int64 token.
  354. /// </summary>
  355. public override long Int64Value
  356. {
  357. get { return _value; }
  358. }
  359. /// <summary>
  360. /// Gets a value indicating whether this token is number.
  361. /// </summary>
  362. /// <value>
  363. /// <c>true</c> if this token is number; otherwise, <c>false</c>.
  364. /// </value>
  365. public override bool IsNumber
  366. {
  367. get { return true; }
  368. }
  369. }
  370. /// <summary>
  371. /// Represents an ObjectId JSON token.
  372. /// </summary>
  373. public class ObjectIdJsonToken : JsonToken
  374. {
  375. // private fields
  376. private ObjectId _value;
  377. // constructors
  378. /// <summary>
  379. /// Initializes a new instance of the ObjectIdJsonToken class.
  380. /// </summary>
  381. /// <param name="lexeme">The lexeme.</param>
  382. /// <param name="value">The ObjectId value.</param>
  383. public ObjectIdJsonToken(string lexeme, ObjectId value)
  384. : base(JsonTokenType.ObjectId, lexeme)
  385. {
  386. _value = value;
  387. }
  388. // public properties
  389. /// <summary>
  390. /// Gets the value of an ObjectId token.
  391. /// </summary>
  392. public override ObjectId ObjectIdValue
  393. {
  394. get { return _value; }
  395. }
  396. }
  397. /// <summary>
  398. /// Represents a regular expression JSON token.
  399. /// </summary>
  400. public class RegularExpressionJsonToken : JsonToken
  401. {
  402. // private fields
  403. private BsonRegularExpression _value;
  404. // constructors
  405. /// <summary>
  406. /// Initializes a new instance of the RegularExpressionJsonToken class.
  407. /// </summary>
  408. /// <param name="lexeme">The lexeme.</param>
  409. /// <param name="value">The BsonRegularExpression value.</param>
  410. public RegularExpressionJsonToken(string lexeme, BsonRegularExpression value)
  411. : base(JsonTokenType.RegularExpression, lexeme)
  412. {
  413. _value = value;
  414. }
  415. // public properties
  416. /// <summary>
  417. /// Gets the value of a regular expression token.
  418. /// </summary>
  419. public override BsonRegularExpression RegularExpressionValue
  420. {
  421. get { return _value; }
  422. }
  423. }
  424. /// <summary>
  425. /// Represents a String JSON token.
  426. /// </summary>
  427. public class StringJsonToken : JsonToken
  428. {
  429. // private fields
  430. private string _value;
  431. // constructors
  432. /// <summary>
  433. /// Initializes a new instance of the StringJsonToken class.
  434. /// </summary>
  435. /// <param name="type">The token type.</param>
  436. /// <param name="lexeme">The lexeme.</param>
  437. /// <param name="value">The String value.</param>
  438. public StringJsonToken(JsonTokenType type, string lexeme, string value)
  439. : base(type, lexeme)
  440. {
  441. _value = value;
  442. }
  443. // public properties
  444. /// <summary>
  445. /// Gets the value of an String token.
  446. /// </summary>
  447. public override string StringValue
  448. {
  449. get { return _value; }
  450. }
  451. }
  452. }