BSON Reading#
- <bson/byte.h> (header file)#
 - <bson/view.h> (header file)#
 Contains APIs for reading BSON object data.
Types#
- 
struct [[zero_initializable]] bson_array_view#
 - 
struct [[zero_initializable]] bson_view#
 These types represents null-able read-only view of a BSON document or array object. The object is pointer-sized, trivial, and zero-initializable. The
bson_array_viewhas the same interface asbson_view, but disambiguates between arrays and documents at compile-time.- Zero-initializable:
 [[zero_initializable]]Represents a null view: A view that does not refer to any document, equivalent tobson_view_null. One can callbson_data()to test for a null view. Using a null view is undefined behavior for most other operations.- Header:
 
Note
This type should not be created manually. It should be created using
bson_view_from(),bson_view_from_data(), orbson_view_null.C++ Members#
- 
using iterator = ::bson_iterator#
 - 
using size_type = std::uint32_t#
 
- 
static bson_view from_data(const bson_byte *b, size_t datalen)#
 - C API:
 - Returns:
 The view returned by this function is guaranteed to be non-null.
- Throws:
 This function will throw an exception if it encounters a decoding error. For a non-throwing equivalent, use
bson_view_from_data().
- 
bool empty() const#
 Determine whether the given document object is empty
- 
iterator find(std::string_view) const#
 Return an iterator referring to the first element in *this with the given key.
- C API:
 
- 
enum bson_view_errc#
 Error conditions that can occur when attempting to decode a BSON document into a
bson_view.- Header:
 
- 
enumerator bson_view_errc_okay#
 There was no decoding error
- 
enumerator bson_view_errc_short_read#
 The data buffer given for decoding is too short to possibly hold the document.
If the data buffer is less that five bytes, it is impossible to be a valid document and this error will occur. If the buffer is more than five bytes, but the header declares a length that is greater than the buffer length, this error will also occur.
- 
enumerator bson_view_errc_invalid_header#
 The BSON document header declares an invalid length.
This will occur if the BSON header size is a negative value when it is decoded.
- 
enumerator bson_view_errc_invalid_terminator#
 Decoding a document expects to find a nul byte at the end of the document data. This error will arise if that null byte is missing.
- 
struct bson_byte#
 A byte-sized plain data type that is used to encapsulate a byte value.
- Header:
 
- 
explicit constexpr operator std::byte() const noexcept#
 - 
explicit constexpr operator std::uint8_t() const noexcept#
 - 
explicit constexpr operator char() const noexcept#
 (C++) explicit conversion operators for BSON byte values.
- 
type __bson_viewable#
 A parameter annotated as
__bson_viewableaccepts any type that can be converted tobson_view. This includes:
Constants#
- 
const bson_view bson_view_null#
 - 
const bson_array_view bson_array_view_null#
 Null views of BSON documents. They refer to no object. These are equivalent to zero-initialized objects. A null view can be detected by checking if
bson_data()returns a null pointer.Note
This is implemented as a C preprocessor macro
Functions & Macros#
View Inspection#
- 
bson_view bson_view_from(__bson_viewable [[nullable]] B)#
 Obtain a
bson_viewfor the given document-like object. This is also used by other function-like macros to coercebson_mutandbson_doctobson_viewautomatically.- Parameters:
 - Returns:
 A new
bson_viewthat views the document associated withB.
Note
This is implemented as a C preprocessor macro
- bson_view bson_view_from_data(
 - const bson_byte *const data,
 - const size_t data_len,
 - enum bson_view_errc *error,
 Obtain a new
bson_viewthat views a document that exists atdatawhich is at mostdata_lenbytes long.- Parameters:
 data – A pointer to the beginning of a BSON document. This sould point exactly at the BSON object header.
data_len – The length of the array of bytes pointed-to by
data. This function will validate the document header to ensure that it will not attempt to overrun thedatabuffer.error – An optional output parameter that will describe the error encountered while decoding a BSON document from
data.
- Returns:
 A
bson_viewthat views the document atdata, or a null view if an error occured. Checking for null can be done withbson_data().- Header:
 
The returned view is valid until:
Dereferencing
datawould be undefined behavior, including if the underlying buffer is reallocated during mutation.OR any data accessible via
datais modified outside of a BSON mutator API.
Important
This function does not consider it an error if
data_lenis larger than the actual document size. This is a useful behavior for decoding data from an input stream.The actual resulting document size can be obtained with
bson_size()Important
This function does not validate the content of elements within the document. The document elements are validated on-the-fly during iteration. Refer: Errant Iterators
- 
const bson_byte *bson_data(__bson_viewable [[nullable]] B)#
 - 
bson_byte *bson_mut_data(__bson_viewable [[nullable]] B)#
 Obtain a pointer to
bson_bytereferring to the first byte in the given document.[[nullable]]If the given object is a null view/document, this returns a null pointer.- Header:
 
The argument to
bson_mut_data()cannot be abson_view, as that is read-only. This will evaluate to a null pointer ifBis a null view/document object.Note
This is implemented as a C preprocessor macro
- 
uint32_t bson_size(__bson_viewable B)#
 - 
int32_t bson_ssize(__bson_viewable B)#
 Obtain the size of the given document object, in bytes.
- C++ API:
 - Parameters:
 - Returns:
 bson_size()returns auint32_t, whilebson_ssize()returns anint32_t- Header:
 
Note
This is implemented as a C preprocessor macro