|
| ValueBuilder () noexcept=default |
| Constructs a ValueBuilder that holds kNull.
|
|
| ValueBuilder (Type type) |
| Constructs a valueBuilder that holds default value for provided type .
|
|
| ValueBuilder (common::TransferTag, ValueBuilder &&) noexcept |
| Transfers the ValueBuilder object. More...
|
|
| ValueBuilder (const ValueBuilder &other) |
|
| ValueBuilder (ValueBuilder &&other) |
|
ValueBuilder & | operator= (const ValueBuilder &other) |
|
ValueBuilder & | operator= (ValueBuilder &&other) |
|
| ValueBuilder (const formats::json::Value &other) |
|
| ValueBuilder (formats::json::Value &&other) |
|
| ValueBuilder (bool t) |
| Converting constructors.
|
|
| ValueBuilder (const char *str) |
|
| ValueBuilder (const std::string &str) |
|
| ValueBuilder (std::string_view str) |
|
| ValueBuilder (int t) |
|
| ValueBuilder (unsigned int t) |
|
| ValueBuilder (uint64_t t) |
|
| ValueBuilder (int64_t t) |
|
| ValueBuilder (float t) |
|
| ValueBuilder (double t) |
|
template<typename T > |
| ValueBuilder (const T &t) |
| Universal constructor using Serialize. More...
|
|
ValueBuilder | operator[] (std::string key) |
| Access member by key for modification. More...
|
|
ValueBuilder | operator[] (std::size_t index) |
| Access array member by index for modification. More...
|
|
template<typename Tag , utils::StrongTypedefOps Ops, typename Enable = std::enable_if_t<utils::IsStrongTypedefLoggable(Ops)>> |
ValueBuilder | operator[] (utils::StrongTypedef< Tag, std::string, Ops > key) |
| Access member by key for modification. More...
|
|
void | EmplaceNocheck (std::string_view key, ValueBuilder value) |
| Emplaces new member w/o a check whether the key already exists. More...
|
|
void | Remove (std::string_view key) |
| Remove key from object. If key is missing nothing happens. More...
|
|
iterator | begin () |
|
iterator | end () |
|
bool | IsEmpty () const |
| Returns whether the array or object 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 map (Type::kObject).
|
|
std::size_t | GetSize () const |
| Returns array size or object members count. More...
|
|
bool | HasMember (std::string_view key) const |
| Returns true if value holds a key . More...
|
|
std::string | GetPath () const |
| Returns full path to this value.
|
|
void | Resize (std::size_t size) |
| Resize the array value or convert null value into an array of requested size. More...
|
|
void | PushBack (ValueBuilder &&bld) |
| Add element into the last position of array. More...
|
|
formats::json::Value | ExtractValue () |
| Take out the resulting Value object. After calling this method the object is in unspecified (but valid - possibly null) state. More...
|
|
Builder for JSON.
Class provides methods for building JSON. For read only access to the existing JSON values use formats::json::Value.
Example usage:
builder["key1"] = 1;
builder["key2"]["key3"] = "val";
ASSERT_EQ(json["key1"].As<int>(), 1);
ASSERT_EQ(json["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(JsonValueBuilder, ExampleCustomization) {
MyKeyValue object = {"val", 1};
builder["example"] = object;
ASSERT_EQ(json["example"]["field1"].As<std::string>(), "val");
ASSERT_EQ(json["example"]["field2"].As<int>(), 1);
}
TEST(JsonValueBuilder, StringViewRemove) {
const std::string str = "ab";
builder["a"] = 1;
builder[str] = 2;
builder.
Remove(std::string_view(str.data(), 1));
EXPECT_EQ(2, value[str].As<int>());
}
TEST(JsonValueBuilder, StringViewHasMember) {
const std::string str = "ab";
main_builder[str] = 2;
EXPECT_EQ(
false, main_builder.
HasMember(
"a"));
EXPECT_EQ(
false, main_builder.
HasMember(std::string_view(str.data(), 1)));
EXPECT_EQ(
true, main_builder.
HasMember(std::string_view(str.data(), 2)));
}
TEST(JsonValueBuilder, StringViewEmplaceNocheck) {
const std::string str = "ab";
main_builder.
EmplaceNocheck(std::string_view(std::string(1024,
'a') +
'b'),
1025);
EXPECT_EQ(1, value["a"].As<int>());
EXPECT_EQ(2, value["ab"].As<int>());
EXPECT_EQ(1025, value[std::string(1024, 'a') + 'b'].As<int>());
}
}
- See also
- Formats (JSON, YAML, BSON, ...)
- Examples
- samples/config_service/config_service.cpp, samples/mongo_service/mongo_service.cpp, samples/testsuite-support/src/metrics.cpp, samples/testsuite-support/src/now.cpp, samples/testsuite-support/src/tasks.cpp, and samples/testsuite-support/src/testpoint.cpp.
Definition at line 41 of file value_builder.hpp.