Github   Telegram
Loading...
Searching...
No Matches
Classes | Public Types | Public Member Functions | Friends | List of all members
formats::yaml::ValueBuilder Class Referencefinal

Builder for YAML. More...

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

Classes

struct  IterTraits
 

Public Types

using iterator = Iterator< IterTraits >
 

Public Member Functions

 ValueBuilder ()
 Constructs a valueBuilder that holds kNull.
 
 ValueBuilder (Type type)
 Constructs a valueBuilder that holds default value for provided type.
 
 ValueBuilder (const ValueBuilder &other)
 
 ValueBuilder (ValueBuilder &&other)
 
ValueBuilderoperator= (const ValueBuilder &other)
 
ValueBuilderoperator= (ValueBuilder &&other)
 
 ValueBuilder (const formats::yaml::Value &other)
 
 ValueBuilder (formats::yaml::Value &&other)
 
 ValueBuilder (bool t)
 Converting constructors.
 
 ValueBuilder (const char *str)
 
 ValueBuilder (const std::string &str)
 
 ValueBuilder (int t)
 
 ValueBuilder (unsigned int t)
 
 ValueBuilder (long t)
 
 ValueBuilder (unsigned long t)
 
 ValueBuilder (long long t)
 
 ValueBuilder (unsigned long long t)
 
 ValueBuilder (float t)
 
 ValueBuilder (double t)
 
 ValueBuilder (common::TransferTag, ValueBuilder &&) noexcept
 Transfers the ValueBuilder object. More...
 
template<typename T >
 ValueBuilder (const T &t)
 Universal constructor using Serialize. More...
 
ValueBuilder operator[] (const 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[] (const utils::StrongTypedef< Tag, std::string, Ops > &key)
 Access member by key for modification. More...
 
void Remove (const std::string &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 (const char *key) const
 Returns true if value holds a key.
 
bool HasMember (const std::string &key) const
 Returns true if value holds a key.
 
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::yaml::Value ExtractValue ()
 Take out the resulting Value object. After calling this method the object is in unspecified (but valid - possibly null) state. More...
 

Friends

class Iterator< IterTraits >
 

Detailed Description

Builder for YAML.

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

Example usage:

// #include <userver/formats/yaml.hpp>
builder["key1"] = 1;
sub_builder["key3"] = "val";
builder["key2"] = sub_builder.ExtractValue();
ASSERT_EQ(yaml["key1"].As<int>(), 1);
ASSERT_EQ(yaml["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::json::Value Serialize(const MyKeyValue& data,
builder["field1"] = data.field1;
builder["field2"] = data.field2;
return builder.ExtractValue();
}
TEST(JsonValueBuilder, ExampleCustomization) {
MyKeyValue object = {"val", 1};
builder["example"] = object;
auto json = builder.ExtractValue();
ASSERT_EQ(json["example"]["field1"].As<std::string>(), "val");
ASSERT_EQ(json["example"]["field2"].As<int>(), 1);
}
} // namespace my_namespace
See also
Formats (JSON, YAML, BSON, ...)

Definition at line 36 of file value_builder.hpp.

Member Typedef Documentation

◆ iterator

Definition at line 45 of file value_builder.hpp.

Constructor & Destructor Documentation

◆ ValueBuilder() [1/2]

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

Transfers the ValueBuilder object.

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

◆ ValueBuilder() [2/2]

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

Universal constructor using Serialize.

Definition at line 82 of file value_builder.hpp.

Member Function Documentation

◆ ExtractValue()

formats::yaml::Value formats::yaml::ValueBuilder::ExtractValue ( )

Take out the resulting Value object. After calling this method the object is in unspecified (but valid - possibly null) state.

Exceptions
`YamlException`if called not from the root builder.

◆ GetSize()

std::size_t formats::yaml::ValueBuilder::GetSize ( ) const

Returns array size or object members count.

Exceptions
`TypeMismatchException`if not an array or an object.

◆ IsEmpty()

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

Returns whether the array or object is empty.

Exceptions
`TypeMismatchException`if not an array or an object.

◆ operator[]() [1/3]

ValueBuilder formats::yaml::ValueBuilder::operator[] ( const std::string &  key)

Access member by key for modification.

Exceptions
`TypeMismatchException`if not object or null value.

◆ operator[]() [2/3]

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

Access member by key for modification.

Exceptions
`TypeMismatchException`if not object or null value.

Definition at line 195 of file value_builder.hpp.

◆ operator[]() [3/3]

ValueBuilder formats::yaml::ValueBuilder::operator[] ( std::size_t  index)

Access array member by index for modification.

Exceptions
`TypeMismatchException`if not an array value.
`OutOfBoundsException`if index is greater than size.

◆ PushBack()

void formats::yaml::ValueBuilder::PushBack ( ValueBuilder &&  bld)

Add element into the last position of array.

Exceptions
`TypeMismatchException`if not an array or null.

◆ Remove()

void formats::yaml::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::yaml::ValueBuilder::Resize ( std::size_t  size)

Resize the array value or convert null value into an array of requested size.

Exceptions
`TypeMismatchException`if not an array or null.

Friends And Related Function Documentation

◆ Iterator< IterTraits >

friend class Iterator< IterTraits >
friend

Definition at line 189 of file value_builder.hpp.


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