10#include <userver/formats/common/meta.hpp>
11#include <userver/formats/common/transfer_tag.hpp>
12#include <userver/formats/json/impl/mutable_value_wrapper.hpp>
13#include <userver/formats/json/value.hpp>
14#include <userver/utils/strong_typedef.hpp>
16USERVER_NAMESPACE_BEGIN
41class ValueBuilder
final {
47 using ContainerType = impl::MutableValueWrapper;
62 ValueBuilder(
const ValueBuilder& other);
64 ValueBuilder(ValueBuilder&& other);
65 ValueBuilder& operator=(
const ValueBuilder& other);
67 ValueBuilder& operator=(ValueBuilder&& other);
74 ValueBuilder(std::nullptr_t)
78 ValueBuilder(
const char* str);
79 ValueBuilder(
char* str);
80 ValueBuilder(
const std::string& str);
81 ValueBuilder(std::string_view str);
83 ValueBuilder(
unsigned int t);
84 ValueBuilder(uint64_t t);
85 ValueBuilder(int64_t t);
86 ValueBuilder(
float t);
87 ValueBuilder(
double t);
93 typename = std::enable_if_t<
94 !std::is_same_v<std::decay_t<T>, ValueBuilder> && !std::is_same_v<std::decay_t<T>,
Value> &&
95 !std::is_same_v<std::decay_t<T>, std::string>>>
97 : ValueBuilder(DoSerialize(std::forward<T>(t)))
111 utils::StrongTypedefOps Ops,
112 typename Enable = std::enable_if_t<utils::IsStrongTypedefLoggable(Ops)>>
113 ValueBuilder
operator[](utils::StrongTypedef<Tag, std::string, Ops> key);
185 class EmplaceEnabler {};
189 ValueBuilder(EmplaceEnabler, impl::MutableValueWrapper)
noexcept;
193 enum class CheckMemberExists { kYes, kNo };
195 explicit ValueBuilder(impl::MutableValueWrapper)
noexcept;
197 static void Copy(impl::Value& to,
const ValueBuilder& from);
198 static void Move(impl::Value& to, ValueBuilder&& from);
200 impl::Value& AddMember(std::string_view key, CheckMemberExists);
202 template <
typename T>
203 static Value DoSerialize(T&& t);
205 impl::MutableValueWrapper value_;
212Value ValueBuilder::DoSerialize(T&& t) {
215 "There is no `Serialize(const T&, formats::serialize::To<json::Value>)` "
216 "in namespace of `T` or `formats::serialize`. "
218 "Probably you forgot to include the "
219 "<userver/formats/serialize/common_containers.hpp> header "
220 "or one of the <formats/json/serialize_*.hpp> headers or you "
221 "have not provided a `Serialize` function overload."
228std::enable_if_t<std::is_integral<T>::value &&
sizeof(T) <=
sizeof(int64_t),
Value>
230 using Type = std::conditional_t<std::is_signed<T>::value, int64_t, uint64_t>;
236template <
typename Tag, utils::StrongTypedefOps Ops,
typename Enable>
237ValueBuilder ValueBuilder::
operator[](utils::StrongTypedef<Tag, std::string, Ops> key) {
238 return (*
this)[std::move(key.GetUnderlying())];
243std::enable_if_t<meta::kIsUniqueMap<T> && utils::IsStrongTypedefLoggable(T::key_type::kOps),
Value>
246 for (
const auto& [key, value] : value) {
254std::enable_if_t<meta::kIsUniqueMap<T> && std::is_convertible_v<
typename T::key_type, std::string>,
Value>
257 for (
const auto& [key, value] : value) {