module BSON::ExtJSON
def create_binary(encoded_value, encoded_subtype)
def create_binary(encoded_value, encoded_subtype) ded_subtype.hex :TYPES[subtype.chr] ttps://jira.mongodb.org/browse/RUBY-2056 lementedError, "Binary subtype #{encoded_subtype} is not currently supported" e64.decode64(encoded_value), type)
def create_regexp(pattern, options)
def create_regexp(pattern, options) w(pattern, options)
def dbref?(hash)
def dbref?(hash) ey?('$db') _a?(String) se ref']&.is_a?(String) && hash.key?('$id')
def map_hash(hash, **options)
def map_hash(hash, **options) p do |key, value| ?(String) || key.is_a?(Symbol)) && key.to_s.include?(NULL_BYTE) r::ExtJSONParseError, "Hash key cannot contain a null byte: #{key}" obj(value, **options)]
def parse(str, **options)
-
(Object)
- Parsed object tree.
Options Hash:
(**options)
-
:mode
(nil | :bson
) -- Which types to emit
Parameters:
-
str
(String
) -- The string to parse.
Other tags:
- Note: - This method uses Ruby standard library's JSON.parse method to
def parse(str, **options) ON.parse(str), **options)
def parse_hash(hash, **options)
def parse_hash(hash, **options) ) ef handling. according to extended json spec, only hash values (but p-level BSON document itself) may be of type "dbref". applies to both hash values and the hash overall; however, o not have DBRef as a distinct type, applying the below op level hashes doesn't cause harm. dup elete('$ref') esent, can be anything lete('$id') Hash) _hash(id) id value as it was, do not convert either to ObjectId ring. But if the value was in {'$oid' => ...} format, is converted to an ObjectId instance so that ion to BSON later on works correctly. ' => ref, '$id' => id} ('$db') always be a string, if provided = hash.delete('$db') pdate(parse_hash(hash)) == 1 hash.first key rom_string(value) l' w.new(value) rInt' ue.is_a?(String) ror::ExtJSONParseError, "$numberInt value is of an incorrect type: #{value}" rLong' ue.is_a?(String) ror::ExtJSONParseError, "$numberLong value is of an incorrect type: #{value}" lue.to_i [:mode] != :bson w(value) rDouble' dles string to double conversion as well as inf/-inf/nan ue.is_a?(String) ror::ExtJSONParseError, "Invalid $numberDouble value: #{value}" (value).to_f rDecimal' sider returning BigDecimal here instead of Decimal128 .new(value) y' ue.is_a?(Hash) ror::ExtJSONParseError, "Invalid $binary value: #{value}" ue.keys.sort == %w(base64 subType) ror::ExtJSONParseError, "Invalid $binary value: #{value}" lue = value['base64'] oded_value.is_a?(String) ror::ExtJSONParseError, "Invalid base64 value in $binary: #{value}" value['subType'] type.is_a?(String) ror::ExtJSONParseError, "Invalid subType value in $binary: #{value}" ary(encoded_value, subtype) [a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}\z/.match(value) ror::ExtJSONParseError, "Invalid $uuid value: #{value}" ary.from_uuid(value) ue.is_a?(String) ror::ExtJSONParseError, "Invalid $code value: #{value}" alue) tamp' ue.keys.sort == %w(i t) ror::ExtJSONParseError, "Invalid $timestamp value: #{value}" 't'] s_a?(Integer) ror::ExtJSONParseError, "Invalid t value: #{value}" 'i'] s_a?(Integer) ror::ExtJSONParseError, "Invalid i value: #{value}" new(t, i) arExpression' ue.keys.sort == %w(options pattern) ror::ExtJSONParseError, "Invalid $regularExpression value: #{value}" sider returning Ruby regular expression object here exp(value['pattern'], value['options']) nter' ue.keys.sort == %w($id $ref) ror::ExtJSONParseError, "Invalid $dbPointer value: #{value}" new(value['$ref'], parse_hash(value['$id'])) g arse(value).utc alue.keys.sort == %w($numberLong) Error::ExtJSONParseError, "Invalid value for $date: #{value}" c = value.values.first.to_i.divmod(1000) t(sec, msec*1000).utc ror::ExtJSONParseError, "Invalid value for $date: #{value}" y' ue == 1 ror::ExtJSONParseError, "Invalid $minKey value: #{value}" y' ue == 1 ror::ExtJSONParseError, "Invalid $maxKey value: #{value}" ined' ue == true ror::ExtJSONParseError, "Invalid $undefined value: #{value}" new ash, **options) == 2 = hash.keys.sort sorted_keys.first orted_keys.last == '$code' ted_keys == %w($code $scope) ror::ExtJSONParseError, "Invalid $code value: #{hash}" h['$code'].is_a?(String) ror::ExtJSONParseError, "Invalid $code value: #{value}" eWithScope.new(hash['$code'], map_hash(hash['$scope'])) == '$binary' ted_keys == %w($binary $type) ror::ExtJSONParseError, "Invalid $binary value: #{hash}" h['$binary'].is_a?(String) ror::ExtJSONParseError, "Invalid $binary value: #{value}" h['$type'].is_a?(String) ror::ExtJSONParseError, "Invalid $binary subtype: #{hash['$type']}" ate_binary(hash['$binary'], hash['$type']) == '$regex' ted_keys == %w($options $regex) ror::ExtJSONParseError, "Invalid $regex value: #{hash}" regex'].is_a?(Hash) x' => parse_hash(hash['$regex']), ons' => hash['$options'] h['$regex'].is_a?(String) ror::ExtJSONParseError, "Invalid $regex pattern: #{hash['$regex']}" h['$options'].is_a?(String) ror::ExtJSONParseError, "Invalid $regex options: #{hash['$options']}" ate_regexp(hash['$regex'], hash['$options']) served_keys(hash, **options) rved_keys(hash, **options)
def parse_obj(value, **options)
-
(Object)
- Converted object tree.
Options Hash:
(**options)
-
:mode
(nil | :bson
) -- Which types to emit
Parameters:
-
value
(Object
) -- The object tree to convert.
Other tags:
- Note: - This method accepts any types as input, not just Hash instances.
Other tags:
- Example: Convert a non-hash value: -
Example: Convert extended JSON type hashes: -
def parse_obj(value, **options) nt :ruby and :ruby! modes bson].include?(options[:mode]) ntError, "Invalid value for :mode option: #{options[:mode].inspect}" rueClass, FalseClass, NilClass, Numeric alue, **options) |item| item, **options) :ExtJSONParseError, "Unknown value type: #{value}"
def verify_no_reserved_keys(hash, **options)
def verify_no_reserved_keys(hash, **options) > RESERVED_KEYS.length KEYS.any? { |key| hash.key?(key) } r::ExtJSONParseError, "Hash uses reserved keys but does not match a known type: #{hash}" .any? { |key| RESERVED_KEYS_HASH.key?(key) } r::ExtJSONParseError, "Hash uses reserved keys but does not match a known type: #{hash}" **options)