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
18namespace formats::
json {
41class ValueBuilder
final {
44 using ValueType = formats::
json::ValueBuilder;
45 using Reference = formats::
json::ValueBuilder&;
46 using Pointer = formats::
json::ValueBuilder*;
47 using ContainerType = impl::MutableValueWrapper;
50 using iterator = Iterator<IterTraits>;
62 ValueBuilder(
const ValueBuilder& other);
64 ValueBuilder(ValueBuilder&& other);
65 ValueBuilder& operator=(
const ValueBuilder& other);
67 ValueBuilder& operator=(ValueBuilder&& other);
69 ValueBuilder(
const formats::
json::Value& other);
70 ValueBuilder(formats::
json::Value&& other);
74 ValueBuilder(std::nullptr_t) : ValueBuilder
() {}
76 ValueBuilder(
const char* str);
77 ValueBuilder(
char* str);
78 ValueBuilder(
const std::string& str);
79 ValueBuilder(std::string_view str);
81 ValueBuilder(
unsigned int t);
82 ValueBuilder(uint64_t t);
83 ValueBuilder(int64_t t);
84 ValueBuilder(
float t);
85 ValueBuilder(
double t);
103 utils::StrongTypedefOps Ops,
105 ValueBuilder
operator[](utils::StrongTypedef<Tag, std::string, Ops> key);
177 class EmplaceEnabler {};
181 ValueBuilder(EmplaceEnabler, impl::MutableValueWrapper)
noexcept;
185 enum class CheckMemberExists { kYes, kNo };
187 explicit ValueBuilder(impl::MutableValueWrapper)
noexcept;
189 static void Copy(impl::Value& to,
const ValueBuilder& from);
190 static void Move(impl::Value& to, ValueBuilder&& from);
192 impl::Value& AddMember(std::string_view key, CheckMemberExists);
194 template <
typename T>
195 static Value DoSerialize(
const T& t);
197 impl::MutableValueWrapper value_;
204Value ValueBuilder::DoSerialize(
const T& t) {
206 formats::
common::impl::kHasSerialize<Value, T>,
207 "There is no `Serialize(const T&, formats::serialize::To<json::Value>)` "
208 "in namespace of `T` or `formats::serialize`. "
210 "Probably you forgot to include the "
211 "<userver/formats/serialize/common_containers.hpp> header "
212 "or one of the <formats/json/serialize_*.hpp> headers or you "
213 "have not provided a `Serialize` function overload."
216 return Serialize(t, formats::serialize::To<Value>());
220std::enable_if_t<std::is_integral<T>::value &&
sizeof(T) <=
sizeof(int64_t), Value>
221Serialize(T value, formats::serialize::To<Value>) {
222 using Type = std::conditional_t<std::is_signed<T>::value, int64_t, uint64_t>;
223 return json::ValueBuilder(
static_cast<Type>(value)).ExtractValue();
226json::Value Serialize(std::chrono::system_clock::time_point tp, formats::
serialize::
To<Value>);
228template <
typename Tag, utils::StrongTypedefOps Ops,
typename Enable>
229ValueBuilder ValueBuilder::
operator[](utils::StrongTypedef<Tag, std::string, Ops> key) {
230 return (*
this)[std::move(key.GetUnderlying())];
238 for (
const auto& [key, value] : value) {
239 builder.EmplaceNocheck(key.GetUnderlying(), value);
241 return builder.ExtractValue();
249 for (
const auto& [key, value] : value) {
250 builder.EmplaceNocheck(key, value);
252 return builder.ExtractValue();