bson 4.7.0 → 4.9.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (159) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +6 -2
  3. data/ext/bson/bson-endian.h +1 -1
  4. data/ext/bson/bson-native.h +16 -5
  5. data/ext/bson/bytebuf.c +1 -1
  6. data/ext/bson/endian.c +2 -1
  7. data/ext/bson/init.c +26 -3
  8. data/ext/bson/read.c +127 -33
  9. data/ext/bson/util.c +41 -1
  10. data/ext/bson/write.c +18 -14
  11. data/lib/bson.rb +4 -1
  12. data/lib/bson/active_support.rb +1 -1
  13. data/lib/bson/array.rb +34 -5
  14. data/lib/bson/binary.rb +42 -4
  15. data/lib/bson/boolean.rb +12 -3
  16. data/lib/bson/code.rb +16 -2
  17. data/lib/bson/code_with_scope.rb +32 -5
  18. data/lib/bson/config.rb +1 -1
  19. data/lib/bson/date.rb +1 -1
  20. data/lib/bson/date_time.rb +1 -1
  21. data/lib/bson/db_pointer.rb +110 -0
  22. data/lib/bson/decimal128.rb +16 -2
  23. data/lib/bson/decimal128/builder.rb +1 -1
  24. data/lib/bson/document.rb +1 -1
  25. data/lib/bson/environment.rb +2 -1
  26. data/lib/bson/error.rb +21 -0
  27. data/lib/bson/ext_json.rb +375 -0
  28. data/lib/bson/false_class.rb +1 -1
  29. data/lib/bson/float.rb +48 -2
  30. data/lib/bson/hash.rb +36 -5
  31. data/lib/bson/int32.rb +22 -2
  32. data/lib/bson/int64.rb +29 -4
  33. data/lib/bson/integer.rb +35 -1
  34. data/lib/bson/json.rb +1 -1
  35. data/lib/bson/max_key.rb +13 -1
  36. data/lib/bson/min_key.rb +13 -1
  37. data/lib/bson/nil_class.rb +4 -2
  38. data/lib/bson/object.rb +28 -1
  39. data/lib/bson/object_id.rb +16 -2
  40. data/lib/bson/open_struct.rb +1 -1
  41. data/lib/bson/regexp.rb +20 -3
  42. data/lib/bson/registry.rb +1 -1
  43. data/lib/bson/specialized.rb +4 -2
  44. data/lib/bson/string.rb +4 -2
  45. data/lib/bson/symbol.rb +93 -4
  46. data/lib/bson/time.rb +63 -4
  47. data/lib/bson/time_with_zone.rb +1 -1
  48. data/lib/bson/timestamp.rb +16 -2
  49. data/lib/bson/true_class.rb +1 -1
  50. data/lib/bson/undefined.rb +12 -1
  51. data/lib/bson/version.rb +2 -2
  52. data/spec/bson/array_spec.rb +1 -1
  53. data/spec/bson/binary_spec.rb +34 -4
  54. data/spec/bson/binary_uuid_spec.rb +1 -1
  55. data/spec/bson/boolean_spec.rb +1 -1
  56. data/spec/bson/code_spec.rb +1 -1
  57. data/spec/bson/code_with_scope_spec.rb +1 -1
  58. data/spec/bson/date_spec.rb +1 -1
  59. data/spec/bson/date_time_spec.rb +1 -1
  60. data/spec/bson/decimal128_spec.rb +1 -1
  61. data/spec/bson/document_spec.rb +1 -1
  62. data/spec/bson/ext_json_parse_spec.rb +308 -0
  63. data/spec/bson/false_class_spec.rb +1 -1
  64. data/spec/bson/float_spec.rb +37 -1
  65. data/spec/bson/hash_spec.rb +71 -1
  66. data/spec/bson/int32_spec.rb +21 -1
  67. data/spec/bson/int64_spec.rb +39 -1
  68. data/spec/bson/integer_spec.rb +27 -1
  69. data/spec/bson/json_spec.rb +1 -1
  70. data/spec/bson/max_key_spec.rb +1 -1
  71. data/spec/bson/min_key_spec.rb +1 -1
  72. data/spec/bson/nil_class_spec.rb +1 -1
  73. data/spec/bson/object_id_spec.rb +1 -1
  74. data/spec/bson/object_spec.rb +1 -1
  75. data/spec/bson/open_struct_spec.rb +1 -1
  76. data/spec/bson/raw_spec.rb +22 -1
  77. data/spec/bson/regexp_spec.rb +1 -1
  78. data/spec/bson/registry_spec.rb +1 -1
  79. data/spec/bson/string_spec.rb +1 -1
  80. data/spec/bson/symbol_raw_spec.rb +45 -0
  81. data/spec/bson/symbol_spec.rb +61 -1
  82. data/spec/bson/time_spec.rb +205 -2
  83. data/spec/bson/time_with_zone_spec.rb +1 -1
  84. data/spec/bson/timestamp_spec.rb +1 -1
  85. data/spec/bson/true_class_spec.rb +1 -1
  86. data/spec/bson/undefined_spec.rb +1 -1
  87. data/spec/bson_spec.rb +1 -1
  88. data/spec/{support → runners}/common_driver.rb +1 -1
  89. data/spec/runners/corpus.rb +185 -0
  90. data/spec/{support/corpus.rb → runners/corpus_legacy.rb} +41 -59
  91. data/spec/spec_helper.rb +10 -3
  92. data/spec/{bson/driver_bson_spec.rb → spec_tests/common_driver_spec.rb} +1 -0
  93. data/spec/{bson/corpus_spec.rb → spec_tests/corpus_legacy_spec.rb} +4 -4
  94. data/spec/spec_tests/corpus_spec.rb +124 -0
  95. data/spec/spec_tests/data/corpus/README.md +15 -0
  96. data/spec/spec_tests/data/corpus/array.json +49 -0
  97. data/spec/spec_tests/data/corpus/binary.json +85 -0
  98. data/spec/spec_tests/data/corpus/boolean.json +27 -0
  99. data/spec/spec_tests/data/corpus/code.json +67 -0
  100. data/spec/spec_tests/data/corpus/code_w_scope.json +78 -0
  101. data/spec/spec_tests/data/corpus/datetime.json +42 -0
  102. data/spec/spec_tests/data/corpus/dbpointer.json +56 -0
  103. data/spec/spec_tests/data/corpus/dbref.json +31 -0
  104. data/spec/spec_tests/data/corpus/decimal128-1.json +317 -0
  105. data/spec/spec_tests/data/corpus/decimal128-2.json +793 -0
  106. data/spec/spec_tests/data/corpus/decimal128-3.json +1771 -0
  107. data/spec/spec_tests/data/corpus/decimal128-4.json +117 -0
  108. data/spec/spec_tests/data/corpus/decimal128-5.json +402 -0
  109. data/spec/spec_tests/data/corpus/decimal128-6.json +119 -0
  110. data/spec/spec_tests/data/corpus/decimal128-7.json +323 -0
  111. data/spec/spec_tests/data/corpus/document.json +36 -0
  112. data/spec/spec_tests/data/corpus/double.json +87 -0
  113. data/spec/spec_tests/data/corpus/int32.json +43 -0
  114. data/spec/spec_tests/data/corpus/int64.json +43 -0
  115. data/spec/spec_tests/data/corpus/maxkey.json +12 -0
  116. data/spec/spec_tests/data/corpus/minkey.json +12 -0
  117. data/spec/spec_tests/data/corpus/multi-type-deprecated.json +15 -0
  118. data/spec/spec_tests/data/corpus/multi-type.json +11 -0
  119. data/spec/spec_tests/data/corpus/null.json +12 -0
  120. data/spec/spec_tests/data/corpus/oid.json +28 -0
  121. data/spec/spec_tests/data/corpus/regex.json +65 -0
  122. data/spec/spec_tests/data/corpus/string.json +72 -0
  123. data/spec/spec_tests/data/corpus/symbol.json +80 -0
  124. data/spec/spec_tests/data/corpus/timestamp.json +24 -0
  125. data/spec/spec_tests/data/corpus/top.json +236 -0
  126. data/spec/spec_tests/data/corpus/undefined.json +15 -0
  127. data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/array.json +0 -0
  128. data/spec/{support/corpus-tests/failures → spec_tests/data/corpus_legacy}/binary.json +0 -0
  129. data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/boolean.json +0 -0
  130. data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/code.json +1 -1
  131. data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/code_w_scope.json +1 -1
  132. data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/document.json +1 -1
  133. data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/double.json +1 -1
  134. data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/failures/datetime.json +0 -0
  135. data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/failures/dbpointer.json +0 -0
  136. data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/failures/int64.json +0 -0
  137. data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/failures/symbol.json +0 -0
  138. data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/int32.json +1 -1
  139. data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/maxkey.json +1 -1
  140. data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/minkey.json +1 -1
  141. data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/null.json +1 -1
  142. data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/oid.json +0 -0
  143. data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/regex.json +1 -1
  144. data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/string.json +0 -0
  145. data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/timestamp.json +1 -1
  146. data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/top.json +0 -0
  147. data/spec/{support/corpus-tests/failures → spec_tests/data/corpus_legacy}/undefined.json +0 -0
  148. data/spec/{support/driver-spec-tests → spec_tests/data}/decimal128/decimal128-1.json +0 -0
  149. data/spec/{support/driver-spec-tests → spec_tests/data}/decimal128/decimal128-2.json +0 -0
  150. data/spec/{support/driver-spec-tests → spec_tests/data}/decimal128/decimal128-3.json +0 -0
  151. data/spec/{support/driver-spec-tests → spec_tests/data}/decimal128/decimal128-4.json +0 -0
  152. data/spec/{support/driver-spec-tests → spec_tests/data}/decimal128/decimal128-5.json +0 -0
  153. data/spec/{support/driver-spec-tests → spec_tests/data}/decimal128/decimal128-6.json +0 -0
  154. data/spec/{support/driver-spec-tests → spec_tests/data}/decimal128/decimal128-7.json +0 -0
  155. data/spec/support/shared_examples.rb +1 -1
  156. metadata +172 -120
  157. checksums.yaml.gz.sig +0 -0
  158. data.tar.gz.sig +0 -1
  159. metadata.gz.sig +0 -1
@@ -1,4 +1,4 @@
1
- # Copyright (C) 2009-2019 MongoDB Inc.
1
+ # Copyright (C) 2009-2020 MongoDB Inc.
2
2
  #
3
3
  # Licensed under the Apache License, Version 2.0 (the "License");
4
4
  # you may not use this file except in compliance with the License.
@@ -1,4 +1,4 @@
1
- # Copyright (C) 2009-2019 MongoDB Inc.
1
+ # Copyright (C) 2009-2020 MongoDB Inc.
2
2
  #
3
3
  # Licensed under the Apache License, Version 2.0 (the "License");
4
4
  # you may not use this file except in compliance with the License.
@@ -46,18 +46,64 @@ module BSON
46
46
  buffer.put_double(self)
47
47
  end
48
48
 
49
+ # Converts this object to a representation directly serializable to
50
+ # Extended JSON (https://github.com/mongodb/specifications/blob/master/source/extended-json.rst).
51
+ #
52
+ # This method returns the float itself if relaxed representation is
53
+ # requested and the value is finite, otherwise a $numberDouble hash.
54
+ #
55
+ # @option opts [ nil | :relaxed | :legacy ] :mode Serialization mode
56
+ # (default is canonical extended JSON)
57
+ #
58
+ # @return [ Hash | Float ] The extended json representation.
59
+ def as_extended_json(**options)
60
+ case infinite?
61
+ when 1
62
+ {'$numberDouble' => 'Infinity'}
63
+ when -1
64
+ {'$numberDouble' => '-Infinity'}
65
+ else
66
+ if nan?
67
+ {'$numberDouble' => 'NaN'}
68
+ else
69
+ if options[:mode] == :relaxed || options[:mode] == :legacy
70
+ self
71
+ else
72
+ value = if BSON::Environment.jruby?
73
+ # Hack to make bson corpus spec tests pass.
74
+ # JRuby serializes -1.2345678901234568e+18 as
75
+ # -1234567890123456770.0, which is valid but differs from MRI
76
+ # serialization. Extended JSON spec does not define precise
77
+ # stringification of floats.
78
+ # https://jira.mongodb.org/browse/SPEC-1536
79
+ if abs > 1e15
80
+ '%.17g' % to_s
81
+ else
82
+ to_s
83
+ end
84
+ else
85
+ to_s
86
+ end
87
+ {'$numberDouble' => value.upcase}
88
+ end
89
+ end
90
+ end
91
+ end
92
+
49
93
  module ClassMethods
50
94
 
51
95
  # Deserialize an instance of a Float from a BSON double.
52
96
  #
53
97
  # @param [ ByteBuffer ] buffer The byte buffer.
54
98
  #
99
+ # @option options [ nil | :bson ] :mode Decoding mode to use.
100
+ #
55
101
  # @return [ Float ] The decoded Float.
56
102
  #
57
103
  # @see http://bsonspec.org/#/specification
58
104
  #
59
105
  # @since 2.0.0
60
- def from_bson(buffer)
106
+ def from_bson(buffer, **options)
61
107
  buffer.get_double
62
108
  end
63
109
  end
@@ -1,4 +1,4 @@
1
- # Copyright (C) 2009-2019 MongoDB Inc.
1
+ # Copyright (C) 2009-2020 MongoDB Inc.
2
2
  #
3
3
  # Licensed under the Apache License, Version 2.0 (the "License");
4
4
  # you may not use this file except in compliance with the License.
@@ -74,26 +74,57 @@ module BSON
74
74
  Document.new(self)
75
75
  end
76
76
 
77
+ # Converts this object to a representation directly serializable to
78
+ # Extended JSON (https://github.com/mongodb/specifications/blob/master/source/extended-json.rst).
79
+ #
80
+ # This method recursively invokes +as_extended_json+ with the provided
81
+ # options on each hash value.
82
+ #
83
+ # @option opts [ nil | :relaxed | :legacy ] :mode Serialization mode
84
+ # (default is canonical extended JSON)
85
+ #
86
+ # @return [ Hash ] This hash converted to extended json representation.
87
+ def as_extended_json(**options)
88
+ ::Hash[map do |key, value|
89
+ [key, value.as_extended_json(**options)]
90
+ end]
91
+ end
92
+
77
93
  module ClassMethods
78
94
 
79
95
  # Deserialize the hash from BSON.
80
96
  #
81
97
  # @param [ ByteBuffer ] buffer The byte buffer.
82
98
  #
99
+ # @option options [ nil | :bson ] :mode Decoding mode to use.
100
+ #
83
101
  # @return [ Array ] The decoded hash.
84
102
  #
85
103
  # @see http://bsonspec.org/#/specification
86
104
  #
87
105
  # @since 2.0.0
88
- def from_bson(buffer)
106
+ def from_bson(buffer, **options)
89
107
  if buffer.respond_to?(:get_hash)
90
- buffer.get_hash
108
+ buffer.get_hash(**options)
91
109
  else
92
110
  hash = Document.allocate
93
- buffer.get_int32 # Throw away the size.
111
+ start_position = buffer.read_position
112
+ expected_byte_size = buffer.get_int32
94
113
  while (type = buffer.get_byte) != NULL_BYTE
95
114
  field = buffer.get_cstring
96
- hash.store(field, BSON::Registry.get(type, field).from_bson(buffer))
115
+ cls = BSON::Registry.get(type, field)
116
+ value = if options.empty?
117
+ # Compatibility with the older Ruby driver versions which define
118
+ # a DBRef class with from_bson accepting a single argument.
119
+ cls.from_bson(buffer)
120
+ else
121
+ cls.from_bson(buffer, **options)
122
+ end
123
+ hash.store(field, value)
124
+ end
125
+ actual_byte_size = buffer.read_position - start_position
126
+ if actual_byte_size != expected_byte_size
127
+ raise Error::BSONDecodeError, "Expected hash to take #{expected_byte_size} bytes but it took #{actual_byte_size} bytes"
97
128
  end
98
129
  hash
99
130
  end
@@ -1,4 +1,4 @@
1
- # Copyright (C) 2009-2019 MongoDB Inc.
1
+ # Copyright (C) 2009-2020 MongoDB Inc.
2
2
  #
3
3
  # Licensed under the Apache License, Version 2.0 (the "License");
4
4
  # you may not use this file except in compliance with the License.
@@ -40,12 +40,14 @@ module BSON
40
40
  #
41
41
  # @param [ ByteBuffer ] buffer The byte buffer.
42
42
  #
43
+ # @option options [ nil | :bson ] :mode Decoding mode to use.
44
+ #
43
45
  # @return [ Integer ] The decoded Integer.
44
46
  #
45
47
  # @see http://bsonspec.org/#/specification
46
48
  #
47
49
  # @since 2.0.0
48
- def self.from_bson(buffer)
50
+ def self.from_bson(buffer, **options)
49
51
  buffer.get_int32
50
52
  end
51
53
 
@@ -115,6 +117,24 @@ module BSON
115
117
  alias :eql? :==
116
118
  alias :=== :==
117
119
 
120
+ # Converts this object to a representation directly serializable to
121
+ # Extended JSON (https://github.com/mongodb/specifications/blob/master/source/extended-json.rst).
122
+ #
123
+ # This method returns the integer value if relaxed representation is
124
+ # requested, otherwise a $numberInt hash.
125
+ #
126
+ # @option opts [ nil | :relaxed | :legacy ] :mode Serialization mode
127
+ # (default is canonical extended JSON)
128
+ #
129
+ # @return [ Hash | Integer ] The extended json representation.
130
+ def as_extended_json(**options)
131
+ if options[:mode] == :relaxed || options[:mode] == :legacy
132
+ value
133
+ else
134
+ {'$numberInt' => value.to_s}
135
+ end
136
+ end
137
+
118
138
  # Register this type when the module is loaded.
119
139
  #
120
140
  # @since 2.0.0
@@ -1,4 +1,4 @@
1
- # Copyright (C) 2009-2019 MongoDB Inc.
1
+ # Copyright (C) 2009-2020 MongoDB Inc.
2
2
  #
3
3
  # Licensed under the Apache License, Version 2.0 (the "License");
4
4
  # you may not use this file except in compliance with the License.
@@ -35,13 +35,20 @@ module BSON
35
35
  #
36
36
  # @param [ ByteBuffer ] buffer The byte buffer.
37
37
  #
38
- # @return [ Integer ] The decoded Integer.
38
+ # @option options [ nil | :bson ] :mode Decoding mode to use.
39
+ #
40
+ # @return [ Integer | BSON::Int64 ] The decoded Integer.
39
41
  #
40
42
  # @see http://bsonspec.org/#/specification
41
43
  #
42
44
  # @since 2.0.0
43
- def self.from_bson(buffer)
44
- buffer.get_int64
45
+ def self.from_bson(buffer, **options)
46
+ value = buffer.get_int64
47
+ if options[:mode] == :bson
48
+ new(value)
49
+ else
50
+ value
51
+ end
45
52
  end
46
53
 
47
54
  # Instantiate a BSON Int64.
@@ -110,6 +117,24 @@ module BSON
110
117
  alias :eql? :==
111
118
  alias :=== :==
112
119
 
120
+ # Converts this object to a representation directly serializable to
121
+ # Extended JSON (https://github.com/mongodb/specifications/blob/master/source/extended-json.rst).
122
+ #
123
+ # This method returns the integer value if relaxed representation is
124
+ # requested, otherwise a $numberLong hash.
125
+ #
126
+ # @option opts [ nil | :relaxed | :legacy ] :mode Serialization mode
127
+ # (default is canonical extended JSON)
128
+ #
129
+ # @return [ Hash | Integer ] The extended json representation.
130
+ def as_extended_json(**options)
131
+ if options[:mode] == :relaxed || options[:mode] == :legacy
132
+ value
133
+ else
134
+ {'$numberLong' => value.to_s}
135
+ end
136
+ end
137
+
113
138
  # Register this type when the module is loaded.
114
139
  #
115
140
  # @since 2.0.0
@@ -1,4 +1,4 @@
1
- # Copyright (C) 2009-2019 MongoDB Inc.
1
+ # Copyright (C) 2009-2020 MongoDB Inc.
2
2
  #
3
3
  # Licensed under the Apache License, Version 2.0 (the "License");
4
4
  # you may not use this file except in compliance with the License.
@@ -159,6 +159,40 @@ module BSON
159
159
  self
160
160
  end
161
161
 
162
+ # Converts this object to a representation directly serializable to
163
+ # Extended JSON (https://github.com/mongodb/specifications/blob/master/source/extended-json.rst).
164
+ #
165
+ # This method returns the integer itself if relaxed representation is
166
+ # requested, otherwise a $numberInt hash if the value fits in 32 bits
167
+ # and a $numberLong otherwise. Regardless of which representation is
168
+ # requested, a value that does not fit in 64 bits raises RangeError.
169
+ #
170
+ # @option opts [ nil | :relaxed | :legacy ] :mode Serialization mode
171
+ # (default is canonical extended JSON)
172
+ #
173
+ # @return [ Hash | Integer ] The extended json representation.
174
+ def as_extended_json(**options)
175
+ # The behavior of native integers' serialization to extended json is
176
+ # not specified. Following our bson serialization logic in this file,
177
+ # produce explicit $numberInt or $numberLong, choosing $numberInt if
178
+ # the integer fits in 32 bits. In Ruby integers can be arbitrarily
179
+ # big; integers that do not fit into 64 bits raise an error as we do not
180
+ # want to silently perform an effective type conversion of integer ->
181
+ # decimal.
182
+
183
+ unless bson_int64?
184
+ raise RangeError, "Integer #{self} is too big to be represented as a MongoDB integer"
185
+ end
186
+
187
+ if options[:mode] == :relaxed || options[:mode] == :legacy
188
+ self
189
+ elsif bson_int32?
190
+ {'$numberInt' => to_s}
191
+ else
192
+ {'$numberLong' => to_s}
193
+ end
194
+ end
195
+
162
196
  private
163
197
 
164
198
  def append_bson_int32(encoded)
@@ -1,4 +1,4 @@
1
- # Copyright (C) 2009-2019 MongoDB Inc.
1
+ # Copyright (C) 2009-2020 MongoDB Inc.
2
2
  #
3
3
  # Licensed under the Apache License, Version 2.0 (the "License");
4
4
  # you may not use this file except in compliance with the License.
@@ -1,4 +1,4 @@
1
- # Copyright (C) 2009-2019 MongoDB Inc.
1
+ # Copyright (C) 2009-2020 MongoDB Inc.
2
2
  #
3
3
  # Licensed under the Apache License, Version 2.0 (the "License");
4
4
  # you may not use this file except in compliance with the License.
@@ -58,7 +58,19 @@ module BSON
58
58
  # @return [ Hash ] The max key as a JSON hash.
59
59
  #
60
60
  # @since 2.0.0
61
+ # @deprecated Use as_extended_json instead.
61
62
  def as_json(*args)
63
+ as_extended_json
64
+ end
65
+
66
+ # Converts this object to a representation directly serializable to
67
+ # Extended JSON (https://github.com/mongodb/specifications/blob/master/source/extended-json.rst).
68
+ #
69
+ # @option opts [ nil | :relaxed | :legacy ] :mode Serialization mode
70
+ # (default is canonical extended JSON)
71
+ #
72
+ # @return [ Hash ] The extended json representation.
73
+ def as_extended_json(**options)
62
74
  { "$maxKey" => 1 }
63
75
  end
64
76
 
@@ -1,4 +1,4 @@
1
- # Copyright (C) 2009-2019 MongoDB Inc.
1
+ # Copyright (C) 2009-2020 MongoDB Inc.
2
2
  #
3
3
  # Licensed under the Apache License, Version 2.0 (the "License");
4
4
  # you may not use this file except in compliance with the License.
@@ -58,7 +58,19 @@ module BSON
58
58
  # @return [ Hash ] The min key as a JSON hash.
59
59
  #
60
60
  # @since 2.0.0
61
+ # @deprecated Use as_extended_json instead.
61
62
  def as_json(*args)
63
+ as_extended_json
64
+ end
65
+
66
+ # Converts this object to a representation directly serializable to
67
+ # Extended JSON (https://github.com/mongodb/specifications/blob/master/source/extended-json.rst).
68
+ #
69
+ # @option opts [ nil | :relaxed | :legacy ] :mode Serialization mode
70
+ # (default is canonical extended JSON)
71
+ #
72
+ # @return [ Hash ] The extended json representation.
73
+ def as_extended_json(**options)
62
74
  { "$minKey" => 1 }
63
75
  end
64
76
 
@@ -1,4 +1,4 @@
1
- # Copyright (C) 2009-2019 MongoDB Inc.
1
+ # Copyright (C) 2009-2020 MongoDB Inc.
2
2
  #
3
3
  # Licensed under the Apache License, Version 2.0 (the "License");
4
4
  # you may not use this file except in compliance with the License.
@@ -34,12 +34,14 @@ module BSON
34
34
  #
35
35
  # @param [ ByteBuffer ] buffer The byte buffer.
36
36
  #
37
+ # @option options [ nil | :bson ] :mode Decoding mode to use.
38
+ #
37
39
  # @return [ nil ] The decoded nil value.
38
40
  #
39
41
  # @see http://bsonspec.org/#/specification
40
42
  #
41
43
  # @since 2.0.0
42
- def from_bson(buffer)
44
+ def from_bson(buffer, **options)
43
45
  nil
44
46
  end
45
47
  end
@@ -1,4 +1,4 @@
1
- # Copyright (C) 2009-2019 MongoDB Inc.
1
+ # Copyright (C) 2009-2020 MongoDB Inc.
2
2
  #
3
3
  # Licensed under the Apache License, Version 2.0 (the "License");
4
4
  # you may not use this file except in compliance with the License.
@@ -58,6 +58,33 @@ module BSON
58
58
  def to_bson_normalized_value
59
59
  self
60
60
  end
61
+
62
+ # Serializes this object to Extended JSON
63
+ # (https://github.com/mongodb/specifications/blob/master/source/extended-json.rst).
64
+ #
65
+ # Subclasses should override +as_extended_json+ rather than this method.
66
+ #
67
+ # @option opts [ nil | :relaxed | :legacy ] :mode Serialization mode
68
+ # (default is canonical extended JSON)
69
+ #
70
+ # @return [ String ] The extended json serialization.
71
+ def to_extended_json(**options)
72
+ as_extended_json(**options).to_json
73
+ end
74
+
75
+ # Converts this object to a representation directly serializable to
76
+ # Extended JSON (https://github.com/mongodb/specifications/blob/master/source/extended-json.rst).
77
+ #
78
+ # Subclasses should override this method to provide custom serialization
79
+ # to Extended JSON.
80
+ #
81
+ # @option options [ true | false ] :relaxed Whether to produce relaxed
82
+ # extended JSON representation.
83
+ #
84
+ # @return [ Object ] The extended json representation.
85
+ def as_extended_json(**options)
86
+ self
87
+ end
61
88
  end
62
89
 
63
90
  # Raised when trying to serialize an object into a key.
@@ -1,4 +1,4 @@
1
- # Copyright (C) 2009-2019 MongoDB Inc.
1
+ # Copyright (C) 2009-2020 MongoDB Inc.
2
2
  #
3
3
  # Licensed under the Apache License, Version 2.0 (the "License");
4
4
  # you may not use this file except in compliance with the License.
@@ -71,7 +71,19 @@ module BSON
71
71
  # @return [ Hash ] The object id as a JSON hash.
72
72
  #
73
73
  # @since 2.0.0
74
+ # @deprecated Use as_extended_json instead.
74
75
  def as_json(*args)
76
+ as_extended_json
77
+ end
78
+
79
+ # Converts this object to a representation directly serializable to
80
+ # Extended JSON (https://github.com/mongodb/specifications/blob/master/source/extended-json.rst).
81
+ #
82
+ # @option opts [ nil | :relaxed | :legacy ] :mode Serialization mode
83
+ # (default is canonical extended JSON)
84
+ #
85
+ # @return [ Hash ] The extended json representation.
86
+ def as_extended_json(**options)
75
87
  { "$oid" => to_s }
76
88
  end
77
89
 
@@ -217,10 +229,12 @@ module BSON
217
229
  #
218
230
  # @param [ ByteBuffer ] buffer The byte buffer.
219
231
  #
232
+ # @option options [ nil | :bson ] :mode Decoding mode to use.
233
+ #
220
234
  # @return [ BSON::ObjectId ] The object id.
221
235
  #
222
236
  # @since 2.0.0
223
- def from_bson(buffer)
237
+ def from_bson(buffer, **options)
224
238
  from_data(buffer.get_bytes(12))
225
239
  end
226
240