Module: BSON::Symbol

Defined in:
lib/bson/symbol.rb

Overview

Note:

Symbols are deprecated in the BSON spec, but they are still currently supported here for backwards compatibility.

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

See Also:

Since:

  • 2.0.0

Defined Under Namespace

Modules: ClassMethods Classes: Raw

Constant Summary collapse

BSON_TYPE =

A symbol is type 0x0E in the BSON spec.

Since:

  • 2.0.0

::String.new(14.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).

Returns:

  • (Hash)

    The extended json representation.

Since:

  • 2.0.0



95
96
97
# File 'lib/bson/symbol.rb', line 95

def as_extended_json(**_options)
  { "$symbol" => to_s }
end

#bson_typeString

Symbols are serialized as strings as symbols are now removed from the BSON specification. Therefore the bson_type when serializing must be a string.

Examples:

Get the BSON type for the symbol.

:test.bson_type

Returns:

  • (String)

    The single byte BSON type.

See Also:

Since:

  • 4.0.0



47
48
49
# File 'lib/bson/symbol.rb', line 47

def bson_type
  String::BSON_TYPE
end

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

Get the symbol as encoded BSON.

Examples:

Get the symbol as encoded BSON.

:test.to_bson

Returns:

See Also:

Since:

  • 2.0.0



61
62
63
# File 'lib/bson/symbol.rb', line 61

def to_bson(buffer = ByteBuffer.new)
  buffer.put_symbol(self)
end

#to_bson_keyString

Get the symbol as a BSON key name encoded C symbol.

Examples:

Get the symbol as a key name.

:test.to_bson_key

Returns:

  • (String)

    The encoded symbol as a BSON key.

See Also:

Since:

  • 2.0.0



75
76
77
# File 'lib/bson/symbol.rb', line 75

def to_bson_key
  self
end

#to_bson_normalized_keyString

Converts the symbol to a normalized key in a BSON document.

Examples:

Convert the symbol to a normalized key.

:test.to_bson_normalized_key

Returns:

  • (String)

    The symbol as a non interned string.

Since:

  • 3.0.0



87
88
89
# File 'lib/bson/symbol.rb', line 87

def to_bson_normalized_key
  to_s
end