#include <userver/formats/bson/value.hpp>
Non-mutable BSON value representation.
Class provides non mutable access BSON value. For modification and construction of new BSON values use formats::bson::ValueBuilder.
To iterate over Value
as object use formats::common::Items.
Classes | |
struct | DefaultConstructed |
Public Types | |
enum class | DuplicateFieldsPolicy { kForbid , kUseFirst , kUseLast } |
Selectors for duplicate fields parsing behavior. More... | |
using | const_iterator = Iterator<const Value, common::IteratorDirection::kForward> |
using | const_reverse_iterator = Iterator<const Value, common::IteratorDirection::kReverse> |
using | Exception = formats::bson::BsonException |
using | ParseException = formats::bson::ParseException |
using | ExceptionWithPath = formats::bson::ExceptionWithPath |
using | Builder = ValueBuilder |
Public Member Functions | |
Value () | |
Constructs a null value. | |
Value (const Value &)=default | |
Value (Value &&) noexcept=default | |
Value & | operator= (const Value &) &=default |
Value & | operator= (Value &&) &noexcept=default |
template<class T> | |
Value & | operator= (T &&) && |
Value | operator[] (const std::string &name) const |
Retrieves document field by name. | |
Value | operator[] (uint32_t index) const |
Retrieves array element by index. | |
bool | HasMember (const std::string &name) const |
Checks whether the document has a field. | |
const_iterator | begin () const |
Returns an iterator to the first array element/document field. | |
const_iterator | end () const |
Returns an iterator following the last array element/document field. | |
const_reverse_iterator | rbegin () const |
Returns a reversed iterator to the last array element. | |
const_reverse_iterator | rend () const |
Returns a reversed iterator following the first array element. | |
bool | IsEmpty () const |
Returns whether the document/array is empty. | |
uint32_t | GetSize () const |
Returns the number of elements in a document/array. | |
std::string | GetPath () const |
Returns value path in a document. | |
bool | operator== (const Value &) const |
bool | operator!= (const Value &) const |
bool | IsMissing () const |
Checks whether the selected element exists. | |
template<typename T> | |
auto | As () const |
template<typename T, typename First, typename... Rest> | |
auto | As (First &&default_arg, Rest &&... more_default_args) const |
template<typename T> | |
auto | As (DefaultConstructed) const |
Returns value of *this converted to T or T() if this->IsMissing(). | |
template<typename T> | |
T | ConvertTo () const |
Extracts the specified type with relaxed type checks. For example, true may be converted to 1.0. | |
template<typename T, typename First, typename... Rest> | |
T | ConvertTo (First &&default_arg, Rest &&... more_default_args) const |
void | SetDuplicateFieldsPolicy (DuplicateFieldsPolicy) |
Changes parsing behavior when duplicate fields are encountered. Should not be used normally. | |
void | CheckNotMissing () const |
Throws a MemberMissingException if the selected element does not exist. | |
void | CheckArrayOrNull () const |
Throws a TypeMismatchException if the selected element is not an array or null. | |
void | CheckDocumentOrNull () const |
Throws a TypeMismatchException if the selected element is not a document or null. | |
Type checking | |
bool | IsArray () const |
bool | IsDocument () const |
bool | IsNull () const |
bool | IsBool () const |
bool | IsInt32 () const |
bool | IsInt64 () const |
bool | IsDouble () const |
bool | IsString () const |
bool | IsDateTime () const |
bool | IsOid () const |
bool | IsBinary () const |
bool | IsDecimal128 () const |
bool | IsMinKey () const |
bool | IsMaxKey () const |
bool | IsTimestamp () const |
bool | IsObject () const |
Protected Member Functions | |
const impl::BsonHolder & | GetBson () const |
using formats::bson::Value::const_iterator = Iterator<const Value, common::IteratorDirection::kForward> |
using formats::bson::Value::const_reverse_iterator = Iterator<const Value, common::IteratorDirection::kReverse> |
|
strong |
Selectors for duplicate fields parsing behavior.
|
inline |
Extracts the specified type with strict type checks
|
inline |
Returns value of *this converted to T or T() if this->IsMissing().
Anything | derived from std::exception. |
value.As<T>({})
|
inline |
const_iterator formats::bson::Value::begin | ( | ) | const |
Returns an iterator to the first array element/document field.
TypeMismatchException | if value is not a document, array or null |
To iterate over Value
as object use formats::common::Items.
|
inline |
|
inline |
const_iterator formats::bson::Value::end | ( | ) | const |
Returns an iterator following the last array element/document field.
TypeMismatchException | if value is not a document, array or null |
uint32_t formats::bson::Value::GetSize | ( | ) | const |
Returns the number of elements in a document/array.
TypeMismatchException | if value is not a document, array or null |
null
. bool formats::bson::Value::HasMember | ( | const std::string & | name | ) | const |
Checks whether the document has a field.
name | field name |
TypeMismatchExcepiton | if value is not a document or null |
bool formats::bson::Value::IsEmpty | ( | ) | const |
Returns whether the document/array is empty.
TypeMismatchException | if value is not a document, array or null |
true
for null
. bool formats::bson::Value::IsMissing | ( | ) | const |
Checks whether the selected element exists.
|
inline |
Value formats::bson::Value::operator[] | ( | const std::string & | name | ) | const |
Retrieves document field by name.
name | field name |
TypeMismatchException | if value is not a missing value, a document, or null |
Value formats::bson::Value::operator[] | ( | uint32_t | index | ) | const |
Retrieves array element by index.
index | element index |
TypeMismatchException | if value is not an array or null |
OutOfBoundsException | if index is invalid for the array |
const_reverse_iterator formats::bson::Value::rbegin | ( | ) | const |
Returns a reversed iterator to the last array element.
TypeMismatchException | if value is not an array or null |
const_reverse_iterator formats::bson::Value::rend | ( | ) | const |
Returns a reversed iterator following the first array element.
TypeMismatchException | if value is not an array or null |
void formats::bson::Value::SetDuplicateFieldsPolicy | ( | DuplicateFieldsPolicy | ) |
Changes parsing behavior when duplicate fields are encountered. Should not be used normally.
Should be called before the first field access. Only affects documents. Default policy is to throw an exception when duplicate fields are encountered.
|
friend |