Module: BSON::Hash

Defined in:
lib/bson/hash.rb

Overview

Injects behaviour for encoding and decoding hashes to and from raw bytes as specified by the BSON spec.

Defined Under Namespace

Modules: ClassMethods

Constant Summary collapse

BSON_TYPE =

A hash, also called an embedded document, is type 0x03 in the BSON spec.

::String.new(3.chr, encoding: BINARY).freeze

Instance Method Summary collapse

Instance Method Details

#as_extended_json(**options) ⇒ Hash

Converts this object to a representation directly serializable to Extended JSON (github.com/mongodb/specifications/blob/master/source/extended-json.rst).

This method recursively invokes as_extended_json with the provided options on each hash value.

Parameters:

  • opts (Hash)

    a customizable set of options

Returns:

  • (Hash)

    This hash converted to extended json representation.



65
66
67
# File 'lib/bson/hash.rb', line 65

def as_extended_json(**options)
  transform_values { |value| value.as_extended_json(**options) }
end

#to_bson(buffer = ByteBuffer.new) ⇒ BSON::ByteBuffer

Get the hash as encoded BSON.

Examples:

Get the hash as encoded BSON.

{ "field" => "value" }.to_bson

Returns:

See Also:



35
36
37
38
39
40
41
42
43
# File 'lib/bson/hash.rb', line 35

def to_bson(buffer = ByteBuffer.new)
  # If the native buffer version has an optimized version, we'll call
  # it directly. Otherwise, we'll serialize the hash the hard way.
  if buffer.respond_to?(:put_hash)
    buffer.put_hash(self)
  else
    serialize_to_buffer(buffer)
  end
end

#to_bson_normalized_valueBSON::Document

Converts the hash to a normalized value in a BSON document.

Examples:

Convert the hash to a normalized value.

hash.to_bson_normalized_value

Returns:



51
52
53
# File 'lib/bson/hash.rb', line 51

def to_bson_normalized_value
  Document.new(self)
end