68class ValueBuilder
final {
71 using ValueType = formats::
json::ValueBuilder;
72 using Reference = formats::
json::ValueBuilder&;
73 using Pointer = formats::
json::ValueBuilder*;
74 using ContainerType = impl::MutableValueWrapper;
89 ValueBuilder(
const ValueBuilder& other);
91 ValueBuilder(ValueBuilder&& other);
93 ValueBuilder(
const formats::
json::
Value& other);
94 ValueBuilder(formats::
json::
Value&& other);
98 requires(impl::IsNativeArithmetic<T> && !
meta::IsCharacter<T>)
100 : value_(CreateArithmetic(NormalizeArithmetic(value)))
104 template <
typename T>
105 requires(impl::IsNativeArithmetic<T> &&
meta::IsCharacter<T>)
107 "Characters will be converted into underlying integer type. Avoid this or wrap with static_cast<int>(...) "
111 : value_(CreateArithmetic(NormalizeArithmetic(value)))
115 template <
typename T>
116 requires impl::IsNativeString<T>
118 : value_(CreateString(std::string_view{value}))
123 template <
typename T>
124 requires(!impl::IsNativeArithmetic<T> && !impl::IsNativeString<T>)
126 : value_(GetSerializedRoot(DoSerialize(t)))
132 ValueBuilder(std::nullptr_t)
145 template <
typename Tag,
utils::StrongTypedefOps Ops>
146 requires(
utils::IsStrongTypedefLoggable(Ops))
148 return (*
this)[std::move(key.GetUnderlying())];
151 ValueBuilder& operator=(
const ValueBuilder& other);
153 ValueBuilder& operator=(ValueBuilder&& other);
156 template <
typename T>
157 requires(impl::IsNativeArithmetic<T> && !
meta::IsCharacter<std::remove_cv_t<T>>)
159 AssignArithmetic(NormalizeArithmetic(value));
164 template <
typename T>
165 requires(impl::IsNativeArithmetic<T> &&
meta::IsCharacter<std::remove_cv_t<T>>)
167 "Characters will be converted into underlying integer type. Avoid this or wrap with static_cast<int>(...) "
171 AssignArithmetic(NormalizeArithmetic(value));
177 template <
typename T>
178 requires impl::IsNativeString<T>
180 AssignString(std::string_view{value});
188 template <
typename T>
189 requires std::is_same_v<std::remove_cvref_t<T>, formats::
json::
Value>
191 AssignValue(std::forward<T>(value));
224 template <
typename T>
225 requires std::is_convertible_v<
const T&, std::string_view>
227 if constexpr (impl::IsNativeString<T>) {
237 template <
impl::IsWrappedValue T>
238 USERVER_IMPL_NODEBUG_INLINE_FUNC
void EmplaceNocheck(std::string_view key,
const T& value) {
239 EmplaceNocheck(key, DoSerialize(value));
243 template <
typename T>
244 requires(impl::IsNativeArithmetic<T> && !
meta::IsCharacter<std::remove_cv_t<T>>)
246 EmplaceNocheckArithmetic(key, NormalizeArithmetic(value));
251 template <
typename T>
252 requires(impl::IsNativeArithmetic<T> &&
meta::IsCharacter<std::remove_cv_t<T>>)
254 "Characters will be converted into underlying integer type. Avoid this or wrap with static_cast<int>(...) "
258 EmplaceNocheckArithmetic(key, NormalizeArithmetic(value));
262 template <
typename T>
264 "Use natively passed argument instead of curly-braces wrapped. Such usage could affect the performance."
266 void EmplaceNocheck(std::string_view key, std::initializer_list<T> initializer) {
267 CheckSingleElementInitializerList(initializer.size());
268 EmplaceNocheck(key, *initializer.begin());
348 template <
typename T>
349 requires std::is_convertible_v<
const T&, std::string_view>
351 if constexpr (impl::IsNativeString<T>) {
361 template <
impl::IsWrappedValue T>
362 USERVER_IMPL_NODEBUG_INLINE_FUNC
void PushBack(
const T& value) {
363 PushBack(DoSerialize(value));
368 template <
typename T>
369 requires(std::is_arithmetic_v<T> && !
meta::IsCharacter<std::remove_cv_t<T>>)
371 if constexpr (std::is_same_v<T,
long double>) {
372 PushBackArithmetic(
static_cast<
double>(value));
374 PushBackArithmetic(NormalizeArithmetic(value));
380 template <
typename T>
381 requires(std::is_arithmetic_v<T> &&
meta::IsCharacter<std::remove_cv_t<T>>)
383 "Characters will be converted into underlying integer type. Avoid this or wrap with static_cast<int>(...) "
387 PushBackArithmetic(NormalizeArithmetic(value));
395 template <
typename T>
397 "Use natively passed argument instead of curly-braces wrapped. Such usage could affect the performance."
399 void PushBack(std::initializer_list<T> initializer) {
400 CheckSingleElementInitializerList(initializer.size());
401 if constexpr (std::is_same_v<T, ValueBuilder>) {
404 PushBack(*initializer.begin());
428 class EmplaceEnabler {};
432 ValueBuilder(EmplaceEnabler, impl::MutableValueWrapper)
noexcept;
436 enum class CheckMemberExists { kYes, kNo };
438 explicit ValueBuilder(impl::MutableValueWrapper)
noexcept;
440 static void Copy(impl::Value& to,
const ValueBuilder& from);
441 static void Move(impl::Value& to, ValueBuilder&& from);
443 static impl::VersionedValuePtr GetSerializedRoot(
Value value);
444 static impl::VersionedValuePtr GetMovedValueRoot(
Value& value);
445 static impl::MutableValueWrapper GetMovedWrapper(ValueBuilder& other);
447 static impl::VersionedValuePtr CreateString(std::string_view value);
449 template <
typename T>
450 requires impl::IsNormalizedArithmetic<T>
451 static impl::VersionedValuePtr CreateArithmetic(T value);
453 void AssignNative(impl::Value value);
455 void AssignString(std::string_view value);
456 void AssignValue(
const Value& value);
457 void AssignValue(
Value&& value);
458 template <
typename T>
459 requires impl::IsNormalizedArithmetic<T>
460 void AssignArithmetic(T value);
462 template <
typename T>
463 requires std::is_arithmetic_v<T>
464 static constexpr auto NormalizeArithmetic(T value)
noexcept {
465 if constexpr (std::is_same_v<T,
bool> || std::is_same_v<T,
int> || std::is_same_v<T,
unsigned int> ||
466 std::is_same_v<T,
float> || std::is_same_v<T,
double>)
471 std::is_integral_v<T> &&
sizeof(T) <=
sizeof(std::int64_t),
472 "JSON arithmetic normalization requires an integer no wider than 64 bits"
474 if constexpr (std::is_signed_v<T>) {
475 return static_cast<std::int64_t>(value);
477 return static_cast<std::uint64_t>(value);
482 template <
typename T>
483 requires impl::IsNormalizedArithmetic<T>
484 void EmplaceNocheckArithmetic(std::string_view key, T value);
486 template <
typename T>
487 requires impl::IsNormalizedArithmetic<T>
488 void PushBackArithmetic(T value);
490 template <
typename MakeValue>
491 USERVER_IMPL_NODEBUG_INLINE_FUNC
inline void PushBackNative(MakeValue&& make_value);
492 static void CheckSingleElementInitializerList(std::size_t size);
494 impl::Value& AddMember(std::string_view key, CheckMemberExists);
496 template <
typename MakeValue>
497 USERVER_IMPL_NODEBUG_INLINE_FUNC
inline impl::Value& AddMember(
498 std::string_view key,
500 MakeValue&& make_value
503 template <
typename T>
504 USERVER_IMPL_NODEBUG_INLINE_FUNC
static Value DoSerialize(
const T& t) {
506 formats::common::impl::HasSerialize<
Value, T>,
507 "There is no `Serialize(const T&, formats::serialize::To<json::Value>)` "
508 "in namespace of `T` or `formats::serialize`. "
510 "Probably you forgot to include the <userver/formats/serialize/common_containers.hpp> header "
511 "or one of the <formats/json/serialize_*.hpp> headers or you have not provided a `Serialize` function "
518 impl::MutableValueWrapper value_;