bson 4.7.1 → 4.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- checksums.yaml.gz.sig +2 -2
- data.tar.gz.sig +0 -0
- data/README.md +5 -1
- data/ext/bson/bson-native.h +14 -4
- data/ext/bson/init.c +25 -2
- data/ext/bson/read.c +61 -15
- data/ext/bson/util.c +40 -0
- data/ext/bson/write.c +17 -13
- data/lib/bson.rb +3 -0
- data/lib/bson/array.rb +21 -3
- data/lib/bson/binary.rb +41 -3
- data/lib/bson/boolean.rb +3 -1
- data/lib/bson/code.rb +15 -1
- data/lib/bson/code_with_scope.rb +17 -3
- data/lib/bson/db_pointer.rb +104 -0
- data/lib/bson/decimal128.rb +15 -1
- data/lib/bson/error.rb +17 -0
- data/lib/bson/ext_json.rb +374 -0
- data/lib/bson/float.rb +47 -1
- data/lib/bson/hash.rb +23 -3
- data/lib/bson/int32.rb +21 -1
- data/lib/bson/int64.rb +28 -3
- data/lib/bson/integer.rb +34 -0
- data/lib/bson/max_key.rb +12 -0
- data/lib/bson/min_key.rb +12 -0
- data/lib/bson/nil_class.rb +3 -1
- data/lib/bson/object.rb +27 -0
- data/lib/bson/object_id.rb +15 -1
- data/lib/bson/regexp.rb +19 -2
- data/lib/bson/specialized.rb +3 -1
- data/lib/bson/string.rb +3 -1
- data/lib/bson/symbol.rb +92 -3
- data/lib/bson/time.rb +28 -3
- data/lib/bson/timestamp.rb +15 -1
- data/lib/bson/undefined.rb +11 -0
- data/lib/bson/version.rb +1 -1
- data/spec/bson/binary_spec.rb +33 -3
- data/spec/bson/ext_json_parse_spec.rb +276 -0
- data/spec/bson/float_spec.rb +36 -0
- data/spec/bson/hash_spec.rb +70 -0
- data/spec/bson/int32_spec.rb +20 -0
- data/spec/bson/int64_spec.rb +38 -0
- data/spec/bson/integer_spec.rb +26 -0
- data/spec/bson/raw_spec.rb +22 -1
- data/spec/bson/symbol_raw_spec.rb +45 -0
- data/spec/bson/symbol_spec.rb +60 -0
- data/spec/{support → runners}/common_driver.rb +0 -0
- data/spec/runners/corpus.rb +182 -0
- data/spec/{support/corpus.rb → runners/corpus_legacy.rb} +40 -58
- data/spec/spec_helper.rb +9 -2
- data/spec/{bson/driver_bson_spec.rb → spec_tests/common_driver_spec.rb} +1 -0
- data/spec/{bson/corpus_spec.rb → spec_tests/corpus_legacy_spec.rb} +4 -4
- data/spec/spec_tests/corpus_spec.rb +124 -0
- data/spec/spec_tests/data/corpus/README.md +15 -0
- data/spec/spec_tests/data/corpus/array.json +49 -0
- data/spec/spec_tests/data/corpus/binary.json +85 -0
- data/spec/spec_tests/data/corpus/boolean.json +27 -0
- data/spec/spec_tests/data/corpus/code.json +67 -0
- data/spec/spec_tests/data/corpus/code_w_scope.json +78 -0
- data/spec/spec_tests/data/corpus/datetime.json +42 -0
- data/spec/spec_tests/data/corpus/dbpointer.json +56 -0
- data/spec/spec_tests/data/corpus/dbref.json +31 -0
- data/spec/spec_tests/data/corpus/decimal128-1.json +317 -0
- data/spec/spec_tests/data/corpus/decimal128-2.json +793 -0
- data/spec/spec_tests/data/corpus/decimal128-3.json +1771 -0
- data/spec/spec_tests/data/corpus/decimal128-4.json +117 -0
- data/spec/spec_tests/data/corpus/decimal128-5.json +402 -0
- data/spec/spec_tests/data/corpus/decimal128-6.json +119 -0
- data/spec/spec_tests/data/corpus/decimal128-7.json +323 -0
- data/spec/spec_tests/data/corpus/document.json +36 -0
- data/spec/spec_tests/data/corpus/double.json +87 -0
- data/spec/spec_tests/data/corpus/int32.json +43 -0
- data/spec/spec_tests/data/corpus/int64.json +43 -0
- data/spec/spec_tests/data/corpus/maxkey.json +12 -0
- data/spec/spec_tests/data/corpus/minkey.json +12 -0
- data/spec/spec_tests/data/corpus/multi-type-deprecated.json +15 -0
- data/spec/spec_tests/data/corpus/multi-type.json +11 -0
- data/spec/spec_tests/data/corpus/null.json +12 -0
- data/spec/spec_tests/data/corpus/oid.json +28 -0
- data/spec/spec_tests/data/corpus/regex.json +65 -0
- data/spec/spec_tests/data/corpus/string.json +72 -0
- data/spec/spec_tests/data/corpus/symbol.json +80 -0
- data/spec/spec_tests/data/corpus/timestamp.json +24 -0
- data/spec/spec_tests/data/corpus/top.json +240 -0
- data/spec/spec_tests/data/corpus/undefined.json +15 -0
- data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/array.json +0 -0
- data/spec/{support/corpus-tests/failures → spec_tests/data/corpus_legacy}/binary.json +0 -0
- data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/boolean.json +0 -0
- data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/code.json +1 -1
- data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/code_w_scope.json +1 -1
- data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/document.json +1 -1
- data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/double.json +1 -1
- data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/failures/datetime.json +0 -0
- data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/failures/dbpointer.json +0 -0
- data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/failures/int64.json +0 -0
- data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/failures/symbol.json +0 -0
- data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/int32.json +1 -1
- data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/maxkey.json +1 -1
- data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/minkey.json +1 -1
- data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/null.json +1 -1
- data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/oid.json +0 -0
- data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/regex.json +1 -1
- data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/string.json +0 -0
- data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/timestamp.json +1 -1
- data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/top.json +0 -0
- data/spec/{support/corpus-tests/failures → spec_tests/data/corpus_legacy}/undefined.json +0 -0
- data/spec/{support/driver-spec-tests → spec_tests/data}/decimal128/decimal128-1.json +0 -0
- data/spec/{support/driver-spec-tests → spec_tests/data}/decimal128/decimal128-2.json +0 -0
- data/spec/{support/driver-spec-tests → spec_tests/data}/decimal128/decimal128-3.json +0 -0
- data/spec/{support/driver-spec-tests → spec_tests/data}/decimal128/decimal128-4.json +0 -0
- data/spec/{support/driver-spec-tests → spec_tests/data}/decimal128/decimal128-5.json +0 -0
- data/spec/{support/driver-spec-tests → spec_tests/data}/decimal128/decimal128-6.json +0 -0
- data/spec/{support/driver-spec-tests → spec_tests/data}/decimal128/decimal128-7.json +0 -0
- metadata +170 -95
- metadata.gz.sig +2 -4
data/lib/bson/float.rb
CHANGED
@@ -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
|
data/lib/bson/hash.rb
CHANGED
@@ -74,26 +74,46 @@ 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
111
|
buffer.get_int32 # Throw away the size.
|
94
112
|
while (type = buffer.get_byte) != NULL_BYTE
|
95
113
|
field = buffer.get_cstring
|
96
|
-
|
114
|
+
cls = BSON::Registry.get(type, field)
|
115
|
+
value = cls.from_bson(buffer, **options)
|
116
|
+
hash.store(field, value)
|
97
117
|
end
|
98
118
|
hash
|
99
119
|
end
|
data/lib/bson/int32.rb
CHANGED
@@ -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
|
data/lib/bson/int64.rb
CHANGED
@@ -35,13 +35,20 @@ module BSON
|
|
35
35
|
#
|
36
36
|
# @param [ ByteBuffer ] buffer The byte buffer.
|
37
37
|
#
|
38
|
-
# @
|
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
|
data/lib/bson/integer.rb
CHANGED
@@ -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)
|
data/lib/bson/max_key.rb
CHANGED
@@ -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
|
|
data/lib/bson/min_key.rb
CHANGED
@@ -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
|
|
data/lib/bson/nil_class.rb
CHANGED
@@ -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
|
data/lib/bson/object.rb
CHANGED
@@ -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.
|
data/lib/bson/object_id.rb
CHANGED
@@ -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
|
|
data/lib/bson/regexp.rb
CHANGED
@@ -205,7 +205,22 @@ module BSON
|
|
205
205
|
#
|
206
206
|
# @since 4.2.0
|
207
207
|
def as_json(*args)
|
208
|
-
|
208
|
+
as_extended_json(mode: :legacy)
|
209
|
+
end
|
210
|
+
|
211
|
+
# Converts this object to a representation directly serializable to
|
212
|
+
# Extended JSON (https://github.com/mongodb/specifications/blob/master/source/extended-json.rst).
|
213
|
+
#
|
214
|
+
# @option opts [ nil | :relaxed | :legacy ] :mode Serialization mode
|
215
|
+
# (default is canonical extended JSON)
|
216
|
+
#
|
217
|
+
# @return [ Hash ] The extended json representation.
|
218
|
+
def as_extended_json(**opts)
|
219
|
+
if opts[:mode] == :legacy
|
220
|
+
{ "$regex" => source, "$options" => options }
|
221
|
+
else
|
222
|
+
{"$regularExpression" => {'pattern' => source, "options" => options}}
|
223
|
+
end
|
209
224
|
end
|
210
225
|
|
211
226
|
# Check equality of the raw bson regexp against another.
|
@@ -248,12 +263,14 @@ module BSON
|
|
248
263
|
#
|
249
264
|
# @param [ ByteBuffer ] buffer The byte buffer.
|
250
265
|
#
|
266
|
+
# @option opts [ nil | :bson ] :mode Decoding mode to use.
|
267
|
+
#
|
251
268
|
# @return [ Regexp ] The decoded regular expression.
|
252
269
|
#
|
253
270
|
# @see http://bsonspec.org/#/specification
|
254
271
|
#
|
255
272
|
# @since 2.0.0
|
256
|
-
def from_bson(buffer)
|
273
|
+
def from_bson(buffer, **opts)
|
257
274
|
pattern = buffer.get_cstring
|
258
275
|
options = buffer.get_cstring
|
259
276
|
Raw.new(pattern, options)
|
data/lib/bson/specialized.rb
CHANGED
@@ -61,12 +61,14 @@ module BSON
|
|
61
61
|
#
|
62
62
|
# @param [ ByteBuffer ] buffer The byte buffer.
|
63
63
|
#
|
64
|
+
# @option options [ nil | :bson ] :mode Decoding mode to use.
|
65
|
+
#
|
64
66
|
# @return [ Specialized ] The decoded specialized class.
|
65
67
|
#
|
66
68
|
# @see http://bsonspec.org/#/specification
|
67
69
|
#
|
68
70
|
# @since 2.0.0
|
69
|
-
def from_bson(buffer)
|
71
|
+
def from_bson(buffer, **options)
|
70
72
|
new
|
71
73
|
end
|
72
74
|
end
|
data/lib/bson/string.rb
CHANGED
@@ -124,12 +124,14 @@ module BSON
|
|
124
124
|
#
|
125
125
|
# @param [ ByteBuffer ] buffer The byte buffer.
|
126
126
|
#
|
127
|
+
# @option options [ nil | :bson ] :mode Decoding mode to use.
|
128
|
+
#
|
127
129
|
# @return [ Regexp ] The decoded string.
|
128
130
|
#
|
129
131
|
# @see http://bsonspec.org/#/specification
|
130
132
|
#
|
131
133
|
# @since 2.0.0
|
132
|
-
def from_bson(buffer)
|
134
|
+
def from_bson(buffer, **options)
|
133
135
|
buffer.get_string
|
134
136
|
end
|
135
137
|
end
|