Module: BSON

Defined in:
lib/bson.rb,
lib/bson/code.rb,
lib/bson/date.rb,
lib/bson/hash.rb,
lib/bson/json.rb,
lib/bson/time.rb,
lib/bson/array.rb,
lib/bson/dbref.rb,
lib/bson/error.rb,
lib/bson/float.rb,
lib/bson/int32.rb,
lib/bson/int64.rb,
lib/bson/binary.rb,
lib/bson/config.rb,
lib/bson/object.rb,
lib/bson/regexp.rb,
lib/bson/string.rb,
lib/bson/symbol.rb,
lib/bson/vector.rb,
lib/bson/boolean.rb,
lib/bson/integer.rb,
lib/bson/max_key.rb,
lib/bson/min_key.rb,
lib/bson/version.rb,
lib/bson/document.rb,
lib/bson/ext_json.rb,
lib/bson/registry.rb,
lib/bson/date_time.rb,
lib/bson/nil_class.rb,
lib/bson/object_id.rb,
lib/bson/timestamp.rb,
lib/bson/undefined.rb,
lib/bson/db_pointer.rb,
lib/bson/decimal128.rb,
lib/bson/true_class.rb,
lib/bson/big_decimal.rb,
lib/bson/environment.rb,
lib/bson/false_class.rb,
lib/bson/open_struct.rb,
lib/bson/specialized.rb,
lib/bson/time_with_zone.rb,
lib/bson/code_with_scope.rb,
lib/bson/error/invalid_key.rb,
lib/bson/decimal128/builder.rb,
lib/bson/error/unsupported_type.rb,
lib/bson/error/bson_decode_error.rb,
lib/bson/error/invalid_object_id.rb,
lib/bson/error/invalid_binary_type.rb,
lib/bson/error/ext_json_parse_error.rb,
lib/bson/error/unserializable_class.rb,
lib/bson/error/invalid_dbref_argument.rb,
lib/bson/error/invalid_regexp_pattern.rb,
lib/bson/error/invalid_decimal128_range.rb,
lib/bson/error/invalid_decimal128_string.rb,
lib/bson/error/unrepresentable_precision.rb,
lib/bson/error/unsupported_binary_subtype.rb,
lib/bson/error/invalid_decimal128_argument.rb,
ext/bson/init.c

Overview

rubocop:todo all

Defined Under Namespace

Modules: Array, BigDecimal, Config, Date, DateTime, Environment, ExtJSON, FalseClass, Float, Hash, Integer, JSON, NilClass, Object, OpenStruct, Regexp, Registry, Specialized, String, Symbol, Time, TimeWithZone, TrueClass Classes: Binary, Boolean, ByteBuffer, Code, CodeWithScope, DBRef, DbPointer, Decimal128, Document, Error, Int32, Int64, MaxKey, MinKey, ObjectId, Timestamp, Undefined, Vector

Constant Summary collapse

BINARY =

Constant for binary string encoding.

Since:

  • 2.0.0

"BINARY"
NO_VALUE =

Constant for bson types that don't actually serialize a value.

Since:

  • 2.0.0

::String.new(encoding: BINARY).freeze
NULL_BYTE =

Constant for a null byte (0x00).

Since:

  • 2.0.0

::String.new(0.chr, encoding: BINARY).freeze
UTF8 =

Constant for UTF-8 string encoding.

Since:

  • 2.0.0

"UTF-8"
MAX_NESTING_DEPTH =

Maximum number of nested BSON documents or arrays the decoder will accept. Prevents stack-overflow DoS on adversarial input. Matches libbson and the Go driver's ExtJSON parser.

Since:

  • 0.0.0

200
DATE_REFERENCE =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Julian day of Date 1970-01-01 - UNIX timestamp reference.

::Date.new(1970, 1, 1).jd
MILLISECONDS_IN_DAY =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Number of miliseconds in a day.

60 * 60 * 24 * 1_000
VERSION =

The current version of the library.

NOTE: this file is automatically updated via rake candidate:create. Manual changes to this file may be overwritten by that rake task.

'5.2.1'

Class Method Summary collapse

Class Method Details

.enter_nesting_depth ⇒ Object

Bump the per-thread BSON decode nesting counter and raise if it exceeds MAX_NESTING_DEPTH. Pair every call with leave_nesting_depth in an ensure block. The bump is inlined at each callsite (rather than a block-yielding helper) to keep JRuby JVM stack frame counts low enough that the check fires before the JVM stack overflows on adversarial input.

Since:

  • 0.0.0



75
76
77
78
79
80
81
82
# File 'lib/bson.rb', line 75

def self.enter_nesting_depth
  depth = (Thread.current[:_bson_nesting_depth] ||= 0) + 1
  if depth > MAX_NESTING_DEPTH
    raise Error::BSONDecodeError,
          "BSON document nesting depth exceeds maximum of #{MAX_NESTING_DEPTH}"
  end
  Thread.current[:_bson_nesting_depth] = depth
end

.leave_nesting_depth ⇒ Object

Since:

  • 0.0.0



84
85
86
# File 'lib/bson.rb', line 84

def self.leave_nesting_depth
  Thread.current[:_bson_nesting_depth] -= 1
end

.ObjectId(string_or_object_id) ⇒ BSON::ObjectId

Interprets the argument as an ObjectId. If given a BSON::ObjectId, this method returns it directly without creating a new one. Otherwise, the argument is interpreted as a string and parsed accordingly.

Examples:

Create an object id from the string.

BSON::ObjectId('...')

Parameters:

  • string_or_object_id (String | BSON::ObjectId) —

    The string to create the id from, or the existing object id to return.

Returns:

Raises:

See Also:

Since:

  • 0.0.0



39
40
41
42
43
# File 'lib/bson.rb', line 39

def self.ObjectId(string_or_object_id)
  return string_or_object_id if string_or_object_id.is_a?(self::ObjectId)

  self::ObjectId.from_string(string_or_object_id)
end