Builder for BSON.
Class provides methods for building BSON. For read only access to the existing BSON values use formats::bson::Value.
Example usage:
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;
};
builder["field1"] = data.field1;
builder["field2"] = data.field2;
}
TEST(BsonValueBuilder, ExampleCustomization) {
MyKeyValue object = {"val", 1};
builder["example"] = object;
ASSERT_EQ(bson["example"]["field1"].As<std::string>(), "val");
ASSERT_EQ(bson["example"]["field2"].As<int>(), 1);
}
TEST(BsonValueBuilder, EmplaceNocheckTest) {
EXPECT_EQ(1, value["field1"].As<int>());
EXPECT_EQ(2, value["field2"].As<int>());
EXPECT_EQ(91, value["ffffqwe3"].As<int>());
}
}
- See also
- Formats (JSON, YAML, BSON, ...)
Definition at line 44 of file value_builder.hpp.
|
| ValueBuilder () |
| Constructs a null value (may be used as either document or array)
|
|
| ValueBuilder (formats::common::Type type) |
| Constructs a value with the predefined type.
|
|
| ValueBuilder (const ValueBuilder &other) |
|
| ValueBuilder (ValueBuilder &&other) |
|
ValueBuilder & | operator= (const ValueBuilder &other) |
|
ValueBuilder & | operator= (ValueBuilder &&other) |
|
template<typename T > |
| ValueBuilder (const T &t) |
| Universal constructor using Serialize.
|
|
| ValueBuilder (common::TransferTag, ValueBuilder &&) noexcept |
| Transfers the ValueBuilder object.
|
|
ValueBuilder | operator[] (const std::string &name) |
| Retrieves or creates document field by name.
|
|
void | EmplaceNocheck (std::string_view key, ValueBuilder value) |
| Emplaces new member w/o a check whether the key already exists.
|
|
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.
|
|
ValueBuilder | operator[] (uint32_t index) |
| Retrieves array element by index.
|
|
void | Remove (const std::string &key) |
| Remove key from object. If key is missing nothing happens.
|
|
iterator | begin () |
| Returns an iterator to the first array element/document field.
|
|
iterator | end () |
| Returns an iterator following the last array element/document field.
|
|
bool | IsEmpty () const |
| Returns whether the document/array is empty.
|
|
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.
|
|
bool | HasMember (const std::string &name) const |
| Checks whether the document has a field.
|
|
void | Resize (uint32_t size) |
| Creates or resizes the array.
|
|
void | PushBack (ValueBuilder &&elem) |
| Appends an element to the array, possibly creating one.
|
|
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 &&) |
|
|
| 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 (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 &) |
|