module BSON::Hash::ClassMethods

Public Instance Methods

from_bson(buffer, **options) click to toggle source

Deserialize the hash from BSON.

@param [ ByteBuffer ] buffer The byte buffer.

@option options [ nil | :bson ] :mode Decoding mode to use.

@return [ Array ] The decoded hash.

@see bsonspec.org/#/specification

@since 2.0.0

# File lib/bson/hash.rb, line 106
def from_bson(buffer, **options)
  if buffer.respond_to?(:get_hash)
    buffer.get_hash(**options)
  else
    hash = Document.allocate
    buffer.get_int32 # Throw away the size.
    while (type = buffer.get_byte) != NULL_BYTE
      field = buffer.get_cstring
      cls = BSON::Registry.get(type, field)
      value = if options.empty?
        # Compatibility with the older Ruby driver versions which define
        # a DBRef class with from_bson accepting a single argument.
        cls.from_bson(buffer)
      else
        cls.from_bson(buffer, **options)
      end
      hash.store(field, value)
    end
    hash
  end
end