Github   Telegram
Loading...
Searching...
No Matches
Public Types | Public Member Functions | List of all members
formats::bson::ValueBuilder Class Reference

Builder for BSON. More...

#include <userver/formats/bson/value_builder.hpp>

Public Types

using iterator = Iterator< ValueBuilder >
 
using Type = formats::common::Type
 

Public Member Functions

 ValueBuilder ()
 Constructs a null value (may be used as either document or array)
 
 ValueBuilder (Type)
 Constructs a value with the predefined type.
 
 ValueBuilder (const ValueBuilder &other)
 
 ValueBuilder (ValueBuilder &&other)
 
ValueBuilderoperator= (const ValueBuilder &other)
 
ValueBuilderoperator= (ValueBuilder &&other)
 
template<typename T >
 ValueBuilder (const T &t)
 Universal constructor using Serialize. More...
 
 ValueBuilder (common::TransferTag, ValueBuilder &&) noexcept
 Transfers the ValueBuilder object. More...
 
ValueBuilder operator[] (const std::string &name)
 Retrieves or creates document field by name. More...
 
template<typename Tag , utils::StrongTypedefOps Ops, typename Enable = std::enable_if_t<utils::IsStrongTypedefLoggable(Ops)>>
ValueBuilder operator[] (const utils::StrongTypedef< Tag, std::string, Ops > &name)
 Access member by key for modification. More...
 
ValueBuilder operator[] (uint32_t index)
 Retrieves array element by index. More...
 
void Remove (const std::string &key)
 Remove key from object. If key is missing nothing happens. More...
 
iterator begin ()
 Returns an iterator to the first array element/document field. More...
 
iterator end ()
 Returns an iterator following the last array element/document field. More...
 
bool IsEmpty () const
 Returns whether the document/array is empty. More...
 
bool IsNull () const noexcept
 Returns true if *this holds a Null (Type::kNull).
 
bool IsBool () const noexcept
 Returns true if *this is convertible to bool.
 
bool IsInt () const noexcept
 Returns true if *this is convertible to int.
 
bool IsInt64 () const noexcept
 Returns true if *this is convertible to int64_t.
 
bool IsUInt64 () const noexcept
 Returns true if *this is convertible to uint64_t.
 
bool IsDouble () const noexcept
 Returns true if *this is convertible to double.
 
bool IsString () const noexcept
 Returns true if *this is convertible to std::string.
 
bool IsArray () const noexcept
 Returns true if *this is an array (Type::kArray).
 
bool IsObject () const noexcept
 Returns true if *this holds a document (BSON_TYPE_DOCUMENT).
 
uint32_t GetSize () const
 Returns the number of elements in a document/array. More...
 
bool HasMember (const std::string &name) const
 Checks whether the document has a field. More...
 
void Resize (uint32_t size)
 Creates or resizes the array. More...
 
void PushBack (ValueBuilder &&elem)
 Appends an element to the array, possibly creating one. More...
 
Value ExtractValue ()
 Retrieves a compiled value from the builder. After calling this method the builder is in unspecified state.
 
 ValueBuilder (const Value &)
 
 ValueBuilder (const Document &)
 
 ValueBuilder (Value &&)
 
 ValueBuilder (Document &&)
 
Concrete type constructors
 ValueBuilder (std::nullptr_t)
 
 ValueBuilder (bool)
 
 ValueBuilder (int)
 
 ValueBuilder (unsigned int)
 
 ValueBuilder (long)
 
 ValueBuilder (unsigned long)
 
 ValueBuilder (long long)
 
 ValueBuilder (unsigned long long)
 
 ValueBuilder (double)
 
 ValueBuilder (const char *)
 
 ValueBuilder (std::string)
 
 ValueBuilder (std::string_view)
 
 ValueBuilder (const std::chrono::system_clock::time_point &)
 
 ValueBuilder (const Oid &)
 
 ValueBuilder (Binary)
 
 ValueBuilder (const Decimal128 &)
 
 ValueBuilder (MinKey)
 
 ValueBuilder (MaxKey)
 
 ValueBuilder (const Timestamp &)
 

Detailed Description

Builder for BSON.

Class provides methods for building BSON. For read only access to the existing BSON values use formats::bson::Value.

Example usage:

// #include <userver/formats/bson.hpp>
builder["key1"] = 1;
builder["key2"]["key3"] = "val";
ASSERT_EQ(bson["key1"].As<int>(), 1);
ASSERT_EQ(bson["key2"]["key3"].As<std::string>(), "val");

Customization example:

namespace my_namespace {
struct MyKeyValue {
std::string field1;
int field2;
};
// The function must be declared in the namespace of your type
formats::bson::Value Serialize(const MyKeyValue& data,
builder["field1"] = data.field1;
builder["field2"] = data.field2;
return builder.ExtractValue();
}
TEST(BsonValueBuilder, ExampleCustomization) {
MyKeyValue object = {"val", 1};
builder["example"] = object;
auto bson = builder.ExtractValue();
ASSERT_EQ(bson["example"]["field1"].As<std::string>(), "val");
ASSERT_EQ(bson["example"]["field2"].As<int>(), 1);
}
} // namespace my_namespace
See also
Formats (JSON, YAML, BSON, ...)

Definition at line 44 of file value_builder.hpp.

Member Typedef Documentation

◆ iterator

Definition at line 46 of file value_builder.hpp.

◆ Type

Definition at line 47 of file value_builder.hpp.

Constructor & Destructor Documentation

◆ ValueBuilder() [1/3]

formats::bson::ValueBuilder::ValueBuilder ( const Value )

Efficiently constructs a copy of an existing value

◆ ValueBuilder() [2/3]

template<typename T >
formats::bson::ValueBuilder::ValueBuilder ( const T &  t)
inline

Universal constructor using Serialize.

Definition at line 102 of file value_builder.hpp.

◆ ValueBuilder() [3/3]

formats::bson::ValueBuilder::ValueBuilder ( common::TransferTag  ,
ValueBuilder &&   
)
noexcept

Transfers the ValueBuilder object.

See also
formats::common::TransferTag for the transfer semantics

Member Function Documentation

◆ begin()

iterator formats::bson::ValueBuilder::begin ( )

Returns an iterator to the first array element/document field.

Exceptions
TypeMismatchExceptionif value is not a document, array or null

◆ end()

iterator formats::bson::ValueBuilder::end ( )

Returns an iterator following the last array element/document field.

Exceptions
TypeMismatchExceptionif value is not a document, array or null

◆ GetSize()

uint32_t formats::bson::ValueBuilder::GetSize ( ) const

Returns the number of elements in a document/array.

Exceptions
TypeMismatchExceptionif value is not a document, array or null
Note
Returns 0 for null.

◆ HasMember()

bool formats::bson::ValueBuilder::HasMember ( const std::string &  name) const

Checks whether the document has a field.

Parameters
namefield name
Exceptions
TypeMismatchExcepitonif value is not a document or null

◆ IsEmpty()

bool formats::bson::ValueBuilder::IsEmpty ( ) const

Returns whether the document/array is empty.

Exceptions
TypeMismatchExceptionif value is not a document, array or null
Note
Returns true for null.

◆ operator[]() [1/3]

ValueBuilder formats::bson::ValueBuilder::operator[] ( const std::string &  name)

Retrieves or creates document field by name.

Exceptions
TypeMismatchExceptionif value is not a document or null

◆ operator[]() [2/3]

template<typename Tag , utils::StrongTypedefOps Ops, typename Enable >
ValueBuilder formats::bson::ValueBuilder::operator[] ( const utils::StrongTypedef< Tag, std::string, Ops > &  name)

Access member by key for modification.

Exceptions
`TypeMismatchException`if not object or null value.

Definition at line 204 of file value_builder.hpp.

◆ operator[]() [3/3]

ValueBuilder formats::bson::ValueBuilder::operator[] ( uint32_t  index)

Retrieves array element by index.

Exceptions
TypeMismatchExceptionif value is not an array or null
OutOfBoundsExceptionif index is invalid for the array

◆ PushBack()

void formats::bson::ValueBuilder::PushBack ( ValueBuilder &&  elem)

Appends an element to the array, possibly creating one.

Parameters
elemelement to append
Exceptions
TypeMismatchExceptionif value is not an array or null

◆ Remove()

void formats::bson::ValueBuilder::Remove ( const std::string &  key)

Remove key from object. If key is missing nothing happens.

Exceptions
`TypeMismatchException`if value is not an object.

◆ Resize()

void formats::bson::ValueBuilder::Resize ( uint32_t  size)

Creates or resizes the array.

Parameters
sizenew size
Exceptions
TypeMismatchExceptionif value is not an array or null

The documentation for this class was generated from the following file: