You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

165 lines
4.7 KiB

  1. #region License
  2. // Copyright (c) 2007 James Newton-King
  3. //
  4. // Permission is hereby granted, free of charge, to any person
  5. // obtaining a copy of this software and associated documentation
  6. // files (the "Software"), to deal in the Software without
  7. // restriction, including without limitation the rights to use,
  8. // copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the
  10. // Software is furnished to do so, subject to the following
  11. // conditions:
  12. //
  13. // The above copyright notice and this permission notice shall be
  14. // included in all copies or substantial portions of the Software.
  15. //
  16. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  17. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  18. // OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  19. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  20. // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  21. // WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  22. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  23. // OTHER DEALINGS IN THE SOFTWARE.
  24. #endregion
  25. using System.Collections;
  26. using System.Collections.Generic;
  27. namespace Newtonsoft.Json.Bson
  28. {
  29. internal abstract class BsonToken
  30. {
  31. public abstract BsonType Type { get; }
  32. public BsonToken Parent { get; set; }
  33. public int CalculatedSize { get; set; }
  34. }
  35. internal class BsonObject : BsonToken, IEnumerable<BsonProperty>
  36. {
  37. private readonly List<BsonProperty> _children = new List<BsonProperty>();
  38. public void Add(string name, BsonToken token)
  39. {
  40. _children.Add(new BsonProperty { Name = new BsonString(name, false), Value = token });
  41. token.Parent = this;
  42. }
  43. public override BsonType Type => BsonType.Object;
  44. public IEnumerator<BsonProperty> GetEnumerator()
  45. {
  46. return _children.GetEnumerator();
  47. }
  48. IEnumerator IEnumerable.GetEnumerator()
  49. {
  50. return GetEnumerator();
  51. }
  52. }
  53. internal class BsonArray : BsonToken, IEnumerable<BsonToken>
  54. {
  55. private readonly List<BsonToken> _children = new List<BsonToken>();
  56. public void Add(BsonToken token)
  57. {
  58. _children.Add(token);
  59. token.Parent = this;
  60. }
  61. public override BsonType Type => BsonType.Array;
  62. public IEnumerator<BsonToken> GetEnumerator()
  63. {
  64. return _children.GetEnumerator();
  65. }
  66. IEnumerator IEnumerable.GetEnumerator()
  67. {
  68. return GetEnumerator();
  69. }
  70. }
  71. internal class BsonEmpty : BsonToken
  72. {
  73. public static readonly BsonToken Null = new BsonEmpty(BsonType.Null);
  74. public static readonly BsonToken Undefined = new BsonEmpty(BsonType.Undefined);
  75. private BsonEmpty(BsonType type)
  76. {
  77. Type = type;
  78. }
  79. public override BsonType Type { get; }
  80. }
  81. internal class BsonValue : BsonToken
  82. {
  83. private readonly object _value;
  84. private readonly BsonType _type;
  85. public BsonValue(object value, BsonType type)
  86. {
  87. _value = value;
  88. _type = type;
  89. }
  90. public object Value => _value;
  91. public override BsonType Type => _type;
  92. }
  93. internal class BsonBoolean : BsonValue
  94. {
  95. public static readonly BsonBoolean False = new BsonBoolean(false);
  96. public static readonly BsonBoolean True = new BsonBoolean(true);
  97. private BsonBoolean(bool value)
  98. : base(value, BsonType.Boolean)
  99. {
  100. }
  101. }
  102. internal class BsonString : BsonValue
  103. {
  104. public int ByteCount { get; set; }
  105. public bool IncludeLength { get; }
  106. public BsonString(object value, bool includeLength)
  107. : base(value, BsonType.String)
  108. {
  109. IncludeLength = includeLength;
  110. }
  111. }
  112. internal class BsonBinary : BsonValue
  113. {
  114. public BsonBinaryType BinaryType { get; set; }
  115. public BsonBinary(byte[] value, BsonBinaryType binaryType)
  116. : base(value, BsonType.Binary)
  117. {
  118. BinaryType = binaryType;
  119. }
  120. }
  121. internal class BsonRegex : BsonToken
  122. {
  123. public BsonString Pattern { get; set; }
  124. public BsonString Options { get; set; }
  125. public BsonRegex(string pattern, string options)
  126. {
  127. Pattern = new BsonString(pattern, false);
  128. Options = new BsonString(options, false);
  129. }
  130. public override BsonType Type => BsonType.Regex;
  131. }
  132. internal class BsonProperty
  133. {
  134. public BsonString Name { get; set; }
  135. public BsonToken Value { get; set; }
  136. }
  137. }