bson 4.7.1 → 4.9.1

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. checksums.yaml.gz.sig +0 -0
  3. data.tar.gz.sig +2 -1
  4. data/README.md +6 -2
  5. data/ext/bson/bson-endian.h +1 -1
  6. data/ext/bson/bson-native.h +15 -5
  7. data/ext/bson/bytebuf.c +1 -1
  8. data/ext/bson/endian.c +1 -1
  9. data/ext/bson/init.c +26 -3
  10. data/ext/bson/read.c +127 -33
  11. data/ext/bson/util.c +41 -1
  12. data/ext/bson/write.c +18 -14
  13. data/lib/bson.rb +4 -1
  14. data/lib/bson/active_support.rb +1 -1
  15. data/lib/bson/array.rb +34 -5
  16. data/lib/bson/binary.rb +42 -4
  17. data/lib/bson/boolean.rb +12 -3
  18. data/lib/bson/code.rb +16 -2
  19. data/lib/bson/code_with_scope.rb +32 -5
  20. data/lib/bson/config.rb +1 -1
  21. data/lib/bson/date.rb +1 -1
  22. data/lib/bson/date_time.rb +1 -1
  23. data/lib/bson/db_pointer.rb +110 -0
  24. data/lib/bson/decimal128.rb +16 -2
  25. data/lib/bson/decimal128/builder.rb +1 -1
  26. data/lib/bson/document.rb +1 -1
  27. data/lib/bson/environment.rb +2 -1
  28. data/lib/bson/error.rb +21 -0
  29. data/lib/bson/ext_json.rb +375 -0
  30. data/lib/bson/false_class.rb +1 -1
  31. data/lib/bson/float.rb +48 -2
  32. data/lib/bson/hash.rb +36 -5
  33. data/lib/bson/int32.rb +22 -2
  34. data/lib/bson/int64.rb +29 -4
  35. data/lib/bson/integer.rb +35 -1
  36. data/lib/bson/json.rb +1 -1
  37. data/lib/bson/max_key.rb +13 -1
  38. data/lib/bson/min_key.rb +13 -1
  39. data/lib/bson/nil_class.rb +4 -2
  40. data/lib/bson/object.rb +28 -1
  41. data/lib/bson/object_id.rb +16 -2
  42. data/lib/bson/open_struct.rb +1 -1
  43. data/lib/bson/regexp.rb +20 -3
  44. data/lib/bson/registry.rb +1 -1
  45. data/lib/bson/specialized.rb +4 -2
  46. data/lib/bson/string.rb +4 -2
  47. data/lib/bson/symbol.rb +93 -4
  48. data/lib/bson/time.rb +63 -4
  49. data/lib/bson/time_with_zone.rb +1 -1
  50. data/lib/bson/timestamp.rb +16 -2
  51. data/lib/bson/true_class.rb +1 -1
  52. data/lib/bson/undefined.rb +12 -1
  53. data/lib/bson/version.rb +2 -2
  54. data/spec/bson/array_spec.rb +1 -1
  55. data/spec/bson/binary_spec.rb +34 -4
  56. data/spec/bson/binary_uuid_spec.rb +1 -1
  57. data/spec/bson/boolean_spec.rb +1 -1
  58. data/spec/bson/code_spec.rb +1 -1
  59. data/spec/bson/code_with_scope_spec.rb +1 -1
  60. data/spec/bson/date_spec.rb +1 -1
  61. data/spec/bson/date_time_spec.rb +1 -1
  62. data/spec/bson/decimal128_spec.rb +1 -1
  63. data/spec/bson/document_spec.rb +1 -1
  64. data/spec/bson/ext_json_parse_spec.rb +308 -0
  65. data/spec/bson/false_class_spec.rb +1 -1
  66. data/spec/bson/float_spec.rb +37 -1
  67. data/spec/bson/hash_spec.rb +71 -1
  68. data/spec/bson/int32_spec.rb +21 -1
  69. data/spec/bson/int64_spec.rb +39 -1
  70. data/spec/bson/integer_spec.rb +27 -1
  71. data/spec/bson/json_spec.rb +1 -1
  72. data/spec/bson/max_key_spec.rb +1 -1
  73. data/spec/bson/min_key_spec.rb +1 -1
  74. data/spec/bson/nil_class_spec.rb +1 -1
  75. data/spec/bson/object_id_spec.rb +1 -1
  76. data/spec/bson/object_spec.rb +1 -1
  77. data/spec/bson/open_struct_spec.rb +1 -1
  78. data/spec/bson/raw_spec.rb +22 -1
  79. data/spec/bson/regexp_spec.rb +1 -1
  80. data/spec/bson/registry_spec.rb +1 -1
  81. data/spec/bson/string_spec.rb +1 -1
  82. data/spec/bson/symbol_raw_spec.rb +45 -0
  83. data/spec/bson/symbol_spec.rb +61 -1
  84. data/spec/bson/time_spec.rb +205 -2
  85. data/spec/bson/time_with_zone_spec.rb +1 -1
  86. data/spec/bson/timestamp_spec.rb +1 -1
  87. data/spec/bson/true_class_spec.rb +1 -1
  88. data/spec/bson/undefined_spec.rb +1 -1
  89. data/spec/bson_spec.rb +1 -1
  90. data/spec/{support → runners}/common_driver.rb +1 -1
  91. data/spec/runners/corpus.rb +185 -0
  92. data/spec/{support/corpus.rb → runners/corpus_legacy.rb} +41 -59
  93. data/spec/spec_helper.rb +10 -3
  94. data/spec/{bson/driver_bson_spec.rb → spec_tests/common_driver_spec.rb} +1 -0
  95. data/spec/{bson/corpus_spec.rb → spec_tests/corpus_legacy_spec.rb} +4 -4
  96. data/spec/spec_tests/corpus_spec.rb +124 -0
  97. data/spec/spec_tests/data/corpus/README.md +15 -0
  98. data/spec/spec_tests/data/corpus/array.json +49 -0
  99. data/spec/spec_tests/data/corpus/binary.json +85 -0
  100. data/spec/spec_tests/data/corpus/boolean.json +27 -0
  101. data/spec/spec_tests/data/corpus/code.json +67 -0
  102. data/spec/spec_tests/data/corpus/code_w_scope.json +78 -0
  103. data/spec/spec_tests/data/corpus/datetime.json +42 -0
  104. data/spec/spec_tests/data/corpus/dbpointer.json +56 -0
  105. data/spec/spec_tests/data/corpus/dbref.json +31 -0
  106. data/spec/spec_tests/data/corpus/decimal128-1.json +317 -0
  107. data/spec/spec_tests/data/corpus/decimal128-2.json +793 -0
  108. data/spec/spec_tests/data/corpus/decimal128-3.json +1771 -0
  109. data/spec/spec_tests/data/corpus/decimal128-4.json +117 -0
  110. data/spec/spec_tests/data/corpus/decimal128-5.json +402 -0
  111. data/spec/spec_tests/data/corpus/decimal128-6.json +119 -0
  112. data/spec/spec_tests/data/corpus/decimal128-7.json +323 -0
  113. data/spec/spec_tests/data/corpus/document.json +36 -0
  114. data/spec/spec_tests/data/corpus/double.json +87 -0
  115. data/spec/spec_tests/data/corpus/int32.json +43 -0
  116. data/spec/spec_tests/data/corpus/int64.json +43 -0
  117. data/spec/spec_tests/data/corpus/maxkey.json +12 -0
  118. data/spec/spec_tests/data/corpus/minkey.json +12 -0
  119. data/spec/spec_tests/data/corpus/multi-type-deprecated.json +15 -0
  120. data/spec/spec_tests/data/corpus/multi-type.json +11 -0
  121. data/spec/spec_tests/data/corpus/null.json +12 -0
  122. data/spec/spec_tests/data/corpus/oid.json +28 -0
  123. data/spec/spec_tests/data/corpus/regex.json +65 -0
  124. data/spec/spec_tests/data/corpus/string.json +72 -0
  125. data/spec/spec_tests/data/corpus/symbol.json +80 -0
  126. data/spec/spec_tests/data/corpus/timestamp.json +24 -0
  127. data/spec/spec_tests/data/corpus/top.json +236 -0
  128. data/spec/spec_tests/data/corpus/undefined.json +15 -0
  129. data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/array.json +0 -0
  130. data/spec/{support/corpus-tests/failures → spec_tests/data/corpus_legacy}/binary.json +0 -0
  131. data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/boolean.json +0 -0
  132. data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/code.json +1 -1
  133. data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/code_w_scope.json +1 -1
  134. data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/document.json +1 -1
  135. data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/double.json +1 -1
  136. data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/failures/datetime.json +0 -0
  137. data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/failures/dbpointer.json +0 -0
  138. data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/failures/int64.json +0 -0
  139. data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/failures/symbol.json +0 -0
  140. data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/int32.json +1 -1
  141. data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/maxkey.json +1 -1
  142. data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/minkey.json +1 -1
  143. data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/null.json +1 -1
  144. data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/oid.json +0 -0
  145. data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/regex.json +1 -1
  146. data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/string.json +0 -0
  147. data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/timestamp.json +1 -1
  148. data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/top.json +0 -0
  149. data/spec/{support/corpus-tests/failures → spec_tests/data/corpus_legacy}/undefined.json +0 -0
  150. data/spec/{support/driver-spec-tests → spec_tests/data}/decimal128/decimal128-1.json +0 -0
  151. data/spec/{support/driver-spec-tests → spec_tests/data}/decimal128/decimal128-2.json +0 -0
  152. data/spec/{support/driver-spec-tests → spec_tests/data}/decimal128/decimal128-3.json +0 -0
  153. data/spec/{support/driver-spec-tests → spec_tests/data}/decimal128/decimal128-4.json +0 -0
  154. data/spec/{support/driver-spec-tests → spec_tests/data}/decimal128/decimal128-5.json +0 -0
  155. data/spec/{support/driver-spec-tests → spec_tests/data}/decimal128/decimal128-6.json +0 -0
  156. data/spec/{support/driver-spec-tests → spec_tests/data}/decimal128/decimal128-7.json +0 -0
  157. data/spec/support/shared_examples.rb +1 -1
  158. metadata +176 -102
  159. metadata.gz.sig +0 -0
@@ -1,4 +1,4 @@
1
- # Copyright (C) 2018-2019 MongoDB Inc.
1
+ # Copyright (C) 2018-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.
@@ -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.
@@ -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) 2016-2019 MongoDB, Inc.
1
+ # Copyright (C) 2016-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.
@@ -0,0 +1,185 @@
1
+ # Copyright (C) 2016-2020 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
+ require 'json'
16
+ require 'forwardable'
17
+
18
+ module BSON
19
+ module Corpus
20
+
21
+ # Represents a test from the driver BSON Corpus.
22
+ #
23
+ # @since 4.2.0
24
+ class Spec
25
+
26
+ # The spec description.
27
+ #
28
+ # @return [ String ] The spec description.
29
+ #
30
+ # @since 4.2.0
31
+ attr_reader :description
32
+
33
+ # The document key of the object to test.
34
+ #
35
+ # @return [ String ] The document key.
36
+ #
37
+ # @since 4.2.0
38
+ attr_reader :test_key
39
+
40
+ # Instantiate the new spec.
41
+ #
42
+ # @example Create the spec.
43
+ # Spec.new(file)
44
+ #
45
+ # @param [ String ] file The name of the json file.
46
+ #
47
+ # @since 4.2.0
48
+ def initialize(file)
49
+ @spec = ::JSON.parse(File.read(file).force_encoding('utf-8'))
50
+ end
51
+
52
+ def description
53
+ @spec['description']
54
+ end
55
+
56
+ def test_key
57
+ @spec['test_key']
58
+ end
59
+
60
+ def valid_tests
61
+ @valid_tests ||=
62
+ @spec['valid']&.map do |test_spec|
63
+ ValidTest.new(self, test_spec)
64
+ end
65
+ end
66
+
67
+ def decode_error_tests
68
+ @decode_error_tests ||=
69
+ @spec['decodeErrors']&.map do |test_spec|
70
+ DecodeErrorTest.new(self, test_spec)
71
+ end
72
+ end
73
+
74
+ def parse_error_tests
75
+ @parse_error_tests ||=
76
+ @spec['parseErrors']&.map do |test_spec|
77
+ ParseErrorTest.new(self, test_spec)
78
+ end
79
+ end
80
+
81
+ # The class of the bson object to test.
82
+ #
83
+ # @example Get the class of the object to test.
84
+ # spec.klass
85
+ #
86
+ # @return [ Class ] The object class.
87
+ #
88
+ # @since 4.2.0
89
+ def klass
90
+ @klass ||= BSON.const_get(description)
91
+ end
92
+ end
93
+
94
+ class TestBase
95
+
96
+ private
97
+
98
+ def decode_hex(obj)
99
+ [ obj ].pack('H*')
100
+ end
101
+ end
102
+
103
+ # Represents a single BSON Corpus test.
104
+ #
105
+ # @since 4.2.0
106
+ class ValidTest < TestBase
107
+ extend Forwardable
108
+
109
+ # Instantiate the new Test.
110
+ #
111
+ # @example Create the test.
112
+ # Test.new(test)
113
+ #
114
+ # @param [ Corpus::Spec ] spec The test specification.
115
+ # @param [ Hash ] test The test specification.
116
+ #
117
+ # @since 4.2.0
118
+ def initialize(spec, test_params)
119
+ @spec = spec
120
+ test_params = test_params.dup
121
+ %w(
122
+ description canonical_extjson relaxed_extjson
123
+ degenerate_extjson converted_extjson
124
+ lossy
125
+ ).each do |key|
126
+ instance_variable_set("@#{key}", test_params.delete(key))
127
+ end
128
+ %w(
129
+ canonical_bson degenerate_bson converted_bson
130
+ lossy
131
+ ).each do |key|
132
+ if test_params.key?(key)
133
+ instance_variable_set("@#{key}", decode_hex(test_params.delete(key)))
134
+ end
135
+ end
136
+ unless test_params.empty?
137
+ raise "Test params has unprocessed keys: #{test_params}"
138
+ end
139
+ end
140
+
141
+ def_delegators :@spec, :test_key
142
+
143
+ attr_reader :description,
144
+ :canonical_bson,
145
+ :degenerate_bson,
146
+ :converted_bson,
147
+ :canonical_extjson,
148
+ :relaxed_extjson,
149
+ :degenerate_extjson,
150
+ :converted_extjson
151
+
152
+ def lossy?
153
+ !!@lossy
154
+ end
155
+
156
+ def canonical_extjson_doc
157
+ ::JSON.parse(canonical_extjson)
158
+ end
159
+
160
+ def relaxed_extjson_doc
161
+ relaxed_extjson && ::JSON.parse(relaxed_extjson)
162
+ end
163
+ end
164
+
165
+ class DecodeErrorTest < TestBase
166
+ def initialize(spec, test_params)
167
+ @spec = spec
168
+ @description = test_params['description']
169
+ @bson = decode_hex(test_params['bson'])
170
+ end
171
+
172
+ attr_reader :description, :bson
173
+ end
174
+
175
+ class ParseErrorTest
176
+ def initialize(spec, test_params)
177
+ @spec = spec
178
+ @description = test_params['description']
179
+ @string = test_params['string']
180
+ end
181
+
182
+ attr_reader :description, :string
183
+ end
184
+ end
185
+ end
@@ -1,4 +1,4 @@
1
- # Copyright (C) 2016-2019 MongoDB, Inc.
1
+ # Copyright (C) 2016-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.
@@ -15,25 +15,19 @@
15
15
  require 'json'
16
16
 
17
17
  module BSON
18
- module Corpus
18
+ module CorpusLegacy
19
19
 
20
20
  # Represents a test from the driver BSON Corpus.
21
- #
22
- # @since 4.2.0
23
21
  class Spec
24
22
 
25
23
  # The spec description.
26
24
  #
27
25
  # @return [ String ] The spec description.
28
- #
29
- # @since 4.2.0
30
26
  attr_reader :description
31
27
 
32
28
  # The document key of the object to test.
33
29
  #
34
30
  # @return [ String ] The document key.
35
- #
36
- # @since 4.2.0
37
31
  attr_reader :test_key
38
32
 
39
33
  # Instantiate the new spec.
@@ -42,8 +36,6 @@ module BSON
42
36
  # Spec.new(file)
43
37
  #
44
38
  # @param [ String ] file The name of the json file.
45
- #
46
- # @since 4.2.0
47
39
  def initialize(file)
48
40
  @spec = ::JSON.parse(File.read(file))
49
41
  @valid = @spec['valid'] || []
@@ -57,13 +49,11 @@ module BSON
57
49
  # @example Get the list of valid tests.
58
50
  # spec.valid_tests
59
51
  #
60
- # @return [ Array<BSON::Corpus::Test> ] The list of valid Tests.
61
- #
62
- # @since 4.2.0
52
+ # @return [ Array<BSON::CorpusLegacy::Test> ] The list of valid Tests.
63
53
  def valid_tests
64
54
  @valid_tests ||=
65
55
  @valid.collect do |test|
66
- BSON::Corpus::Test.new(self, test)
56
+ BSON::CorpusLegacy::Test.new(self, test)
67
57
  end
68
58
  end
69
59
 
@@ -72,13 +62,11 @@ module BSON
72
62
  # @example Get the list of invalid tests.
73
63
  # spec.invalid_tests
74
64
  #
75
- # @return [ Array<BSON::Corpus::Test> ] The list of invalid Tests.
76
- #
77
- # @since 4.2.0
65
+ # @return [ Array<BSON::CorpusLegacy::Test> ] The list of invalid Tests.
78
66
  def invalid_tests
79
67
  @invalid_tests ||=
80
68
  @invalid.collect do |test|
81
- BSON::Corpus::Test.new(self, test)
69
+ BSON::CorpusLegacy::Test.new(self, test)
82
70
  end
83
71
  end
84
72
 
@@ -88,31 +76,23 @@ module BSON
88
76
  # spec.klass
89
77
  #
90
78
  # @return [ Class ] The object class.
91
- #
92
- # @since 4.2.0
93
79
  def klass
94
80
  @klass ||= BSON.const_get(description)
95
81
  end
96
82
  end
97
83
 
98
84
  # Represents a single BSON Corpus test.
99
- #
100
- # @since 4.2.0
101
85
  class Test
102
86
 
103
87
  # The test description.
104
88
  #
105
89
  # @return [ String ] The test description.
106
- #
107
- # @since 4.2.0
108
90
  attr_reader :description
109
91
 
110
92
  # Name of a field in a valid test case extjson document that should be
111
93
  # checked against the case's string field.
112
94
  #
113
95
  # @return [ String ] The json representation of the object.
114
- #
115
- # @since 4.2.0
116
96
  attr_reader :test_key
117
97
 
118
98
  # Instantiate the new Test.
@@ -122,8 +102,6 @@ module BSON
122
102
  #
123
103
  # @param [ Corpus::Spec ] spec The test specification.
124
104
  # @param [ Hash ] test The test specification.
125
- #
126
- # @since 4.2.0
127
105
  def initialize(spec, test)
128
106
  @spec = spec
129
107
  @description = test['description']
@@ -139,12 +117,29 @@ module BSON
139
117
  # test.correct_bson
140
118
  #
141
119
  # @return [ String ] The correct bson bytes.
142
- #
143
- # @since 4.2.0
144
120
  def correct_bson
145
121
  @correct_bson ||= decode_hex(@canonical_bson || @bson)
146
122
  end
147
123
 
124
+ # Create a BSON::Document object from the test's bson representation
125
+ #
126
+ # @return [ BSON::Document ] The BSON::Document object
127
+ def document_from_bson
128
+ bson_bytes = decode_hex(@bson)
129
+ buffer = BSON::ByteBuffer.new(bson_bytes)
130
+ BSON::Document.from_bson(buffer)
131
+ end
132
+
133
+ # Create a BSON::Document object from the test's canonical bson
134
+ # representation
135
+ #
136
+ # @return [ BSON::Document ] The BSON::Document object
137
+ def document_from_canonical_bson
138
+ bson_bytes = decode_hex(@canonical_bson)
139
+ buffer = BSON::ByteBuffer.new(bson_bytes)
140
+ BSON::Document.from_bson(buffer)
141
+ end
142
+
148
143
  # Given the hex representation of bson, decode it into a Document,
149
144
  # then reencoded it to bson.
150
145
  #
@@ -152,12 +147,8 @@ module BSON
152
147
  # test.reencoded_bson
153
148
  #
154
149
  # @return [ String ] The reencoded bson bytes.
155
- #
156
- # @since 4.2.0
157
150
  def reencoded_bson
158
- bson_bytes = decode_hex(@bson)
159
- buffer = BSON::ByteBuffer.new(bson_bytes)
160
- BSON::Document.from_bson(buffer).to_bson.to_s
151
+ document_from_bson.to_bson.to_s
161
152
  end
162
153
 
163
154
  # Given the hex representation of the canonical bson, decode it into a Document,
@@ -167,12 +158,8 @@ module BSON
167
158
  # test.reencoded_canonical_bson
168
159
  #
169
160
  # @return [ String ] The reencoded canonical bson bytes.
170
- #
171
- # @since 4.2.0
172
161
  def reencoded_canonical_bson
173
- bson_bytes = decode_hex(@canonical_bson)
174
- buffer = BSON::ByteBuffer.new(bson_bytes)
175
- BSON::Document.from_bson(buffer).to_bson.to_s
162
+ document_from_canonical_bson.to_bson.to_s
176
163
  end
177
164
 
178
165
  # Whether the canonical bson should be tested.
@@ -181,8 +168,6 @@ module BSON
181
168
  # test.test_canonical_bson?
182
169
  #
183
170
  # @return [ true, false ] Whether the canonical bson should be tested.
184
- #
185
- # @since 4.2.0
186
171
  def test_canonical_bson?
187
172
  @canonical_bson && (@bson != @canonical_bson)
188
173
  end
@@ -193,8 +178,6 @@ module BSON
193
178
  # test.correct_extjson
194
179
  #
195
180
  # @return [ String ] The correct extended json representation.
196
- #
197
- # @since 4.2.0
198
181
  def correct_extjson
199
182
  @canonical_extjson || @extjson
200
183
  end
@@ -205,8 +188,6 @@ module BSON
205
188
  # test.test_extjson?
206
189
  #
207
190
  # @return [ true, false ] Whether the extended json should be tested.
208
- #
209
- # @since 4.2.0
210
191
  def test_extjson?
211
192
  !!@extjson
212
193
  end
@@ -218,12 +199,8 @@ module BSON
218
199
  # test.extjson_from_encoded_bson
219
200
  #
220
201
  # @return [ Hash ] The extended json representation.
221
- #
222
- # @since 4.2.0
223
202
  def extjson_from_bson
224
- subject = decode_hex(@bson)
225
- buffer = BSON::ByteBuffer.new(subject)
226
- ::JSON.parse(BSON::Document.from_bson(buffer).to_json)
203
+ as_legacy_extended_json(document_from_bson)
227
204
  end
228
205
 
229
206
  # Get the extended json representation of the decoded doc from the provided
@@ -233,12 +210,8 @@ module BSON
233
210
  # test.extjson_from_canonical_bson
234
211
  #
235
212
  # @return [ Hash ] The extended json representation.
236
- #
237
- # @since 4.2.0
238
213
  def extjson_from_canonical_bson
239
- subject = decode_hex(@canonical_bson)
240
- buffer = BSON::ByteBuffer.new(subject)
241
- ::JSON.parse(BSON::Document.from_bson(buffer).to_json)
214
+ as_legacy_extended_json(document_from_canonical_bson)
242
215
  end
243
216
 
244
217
  # Get the extended json representation of the decoded doc from the provided
@@ -248,15 +221,24 @@ module BSON
248
221
  # test.extjson_from_encoded_extjson
249
222
  #
250
223
  # @return [ Hash ] The extended json representation.
251
- #
252
- # @since 4.2.0
253
224
  def extjson_from_encoded_extjson
254
225
  doc = BSON::Document.new(@extjson)
255
- ::JSON.parse(doc.to_json)
226
+ as_legacy_extended_json(doc)
256
227
  end
257
228
 
258
229
  private
259
230
 
231
+ def as_legacy_extended_json(object)
232
+ result = object.as_extended_json(mode: :legacy)
233
+ if object.respond_to?(:as_json)
234
+ old_result = object.as_json
235
+ unless result == old_result
236
+ raise "Serializing #{object} to legacy extended json did not match between new and old APIs"
237
+ end
238
+ end
239
+ result
240
+ end
241
+
260
242
  def decode_hex(obj)
261
243
  [ obj ].pack('H*')
262
244
  end