module BSON::Array::ClassMethods

Public Instance Methods

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

Deserialize the array from BSON.

@param [ ByteBuffer ] buffer The byte buffer.

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

@return [ Array ] The decoded array.

@see bsonspec.org/#/specification

@since 2.0.0

# File lib/bson/array.rb, line 121
def from_bson(buffer, **options)
  if buffer.respond_to?(:get_array)
    buffer.get_array(**options)
  else
    array = new
    start_position = buffer.read_position
    expected_byte_size = buffer.get_int32
    while (type = buffer.get_byte) != NULL_BYTE
      buffer.get_cstring
      cls = BSON::Registry.get(type)
      value = if options.empty?
        cls.from_bson(buffer)
      else
        cls.from_bson(buffer, **options)
      end
      array << value
    end
    actual_byte_size = buffer.read_position - start_position
    if actual_byte_size != expected_byte_size
      raise Error::BSONDecodeError, "Expected array to take #{expected_byte_size} bytes but it took #{actual_byte_size} bytes"
    end
    array
  end
end