docs: def _get_object(data, position, obj_end, opts):
      """Decode a BSON subdocument to opts.document_class or bson.dbref.DBRef."""
      obj_size = _UNPACK_INT(data[position:position + 4])[0]
      end = position + obj_size - 1
      if data[end:position + obj_size] != b"\x00":
          raise InvalidBSON("bad eoo")
      if end >= obj_end:
          raise InvalidBSON("invalid object length")
      obj = _elements_to_dict(data, position + 4, end, opts, subdocument=True)
  
      position += obj_size
      if "$ref" in obj:
          return (DBRef(obj.pop("$ref"), obj.pop("$id", None),
                        obj.pop("$db", None), obj), position)
      return obj, position