bsoncxx::document::value scoped_bson_t::steal() { if (!_is_initialized) { return bsoncxx::document::value{bsoncxx::document::view()}; } std::uint32_t length; std::uint8_t* buff = bson_destroy_with_steal(bson(), true, &length); return bsoncxx::document::value(buff, length, bson_free_deleter); }
bsoncxx::array::value steal_array() { if (!_root_is_array) { throw bsoncxx::exception{error_code::k_cannot_perform_array_operation_on_document}; } uint32_t buf_len; uint8_t* buf_ptr = bson_destroy_with_steal(&_root, true, &buf_len); bson_init(&_root); return bsoncxx::array::value{buf_ptr, buf_len, bson_free_deleter}; }
document::value from_json(stdx::string_view json) { bson_error_t error; bson_t* result = bson_new_from_json(reinterpret_cast<const uint8_t*>(json.data()), json.size(), &error); if (!result) throw exception(error_code::k_json_parse_failure, error.message); std::uint32_t length; std::uint8_t* buf = bson_destroy_with_steal(result, true, &length); return document::value{buf, length, bson_free_deleter}; }
stdx::optional<document::value> from_json(stdx::string_view json) { bson_error_t error; bson_t* result = bson_new_from_json(reinterpret_cast<const uint8_t*>(json.data()), json.size(), &error); if (!result) return stdx::nullopt; std::uint32_t length; std::uint8_t* buf = bson_destroy_with_steal(result, true, &length); return document::value{buf, length, bson_free_deleter}; }