module BSON::Hash
def as_extended_json(**options)
-
(Hash)
- This hash converted to extended json representation.
Options Hash:
(**opts)
-
:mode
(nil | :relaxed | :legacy
) -- Serialization mode
def as_extended_json(**options) transform_values { |value| value.as_extended_json(**options) } end
def serialize_key(buffer, key)
-
(EncodingError)
- if the string is not a valid encoding -
(ArgumentError)
- if the string cannot be serialized
Parameters:
-
buffer
(ByteBuf
) -- The buffer to received the serialized
def serialize_key(buffer, key) buffer.put_cstring(key) rescue ArgumentError => e raise ArgumentError, "Error serializing key #{key}: #{e.class}: #{e}" rescue EncodingError => e # Note this may convert exception class from a subclass of # EncodingError to EncodingError itself raise EncodingError, "Error serializing key #{key}: #{e.class}: #{e}" end
def serialize_key_value_pairs(buffer)
-
(Error::UnserializableClass)
- if a value cannot be serialized
Parameters:
-
buffer
(ByteBuf
) -- The buffer to received the serialized
def serialize_key_value_pairs(buffer) each do |field, value| unless value.respond_to?(:bson_type) raise Error::UnserializableClass, "Hash value for key '#{field}' does not define its BSON serialized type: #{value}" end buffer.put_byte(value.bson_type) key = field.to_bson_key serialize_key(buffer, key) value.to_bson(buffer) end end
def serialize_to_buffer(buffer)
-
buffer
(ByteBuf
) -- The buffer to receive the serialized hash.
def serialize_to_buffer(buffer) position = buffer.length buffer.put_int32(0) serialize_key_value_pairs(buffer) buffer.put_byte(NULL_BYTE) buffer.replace_int32(position, buffer.length - position) end
def to_bson(buffer = ByteBuffer.new)
- See: http://bsonspec.org/#/specification -
Returns:
-
(BSON::ByteBuffer)
- The buffer with the encoded object.
Other tags:
- Example: Get the hash as encoded BSON. -
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
def to_bson_normalized_value
-
(BSON::Document)
- The normalized hash.
Other tags:
- Example: Convert the hash to a normalized value. -
def to_bson_normalized_value Document.new(self) end