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);
104 ValueBuilder
operator[](utils::StrongTypedef<Tag, std::string, Ops> key);
176 class EmplaceEnabler {};
180 ValueBuilder(EmplaceEnabler, impl::MutableValueWrapper)
noexcept;
184 enum class CheckMemberExists { kYes, kNo };
186 explicit ValueBuilder(impl::MutableValueWrapper)
noexcept;
188 static void Copy(impl::Value& to,
const ValueBuilder& from);
189 static void Move(impl::Value& to, ValueBuilder&& from);
191 impl::Value& AddMember(std::string_view key, CheckMemberExists);
193 template <
typename T>
194 static Value DoSerialize(
const T& t);
196 impl::MutableValueWrapper value_;
203Value ValueBuilder::DoSerialize(
const T& t) {
205 formats::
common::impl::kHasSerialize<Value, T>,
206 "There is no `Serialize(const T&, formats::serialize::To<json::Value>)` "
207 "in namespace of `T` or `formats::serialize`. "
209 "Probably you forgot to include the "
210 "<userver/formats/serialize/common_containers.hpp> header "
211 "or one of the <formats/json/serialize_*.hpp> headers or you "
212 "have not provided a `Serialize` function overload.");
214 return Serialize(t, formats::serialize::To<Value>());
218std::enable_if_t<std::is_integral<T>::value &&
sizeof(T) <=
sizeof(int64_t),
220Serialize(T value, formats::serialize::To<Value>) {
221 using Type = std::conditional_t<std::is_signed<T>::value, int64_t, uint64_t>;
222 return json::ValueBuilder(
static_cast<Type>(value)).ExtractValue();
225json::Value Serialize(std::chrono::system_clock::time_point tp,
230 utils::StrongTypedef<Tag, std::string, Ops> key) {
231 return (*
this)[std::move(key.GetUnderlying())];
241 for (
const auto& [key, value] : value) {
242 builder.EmplaceNocheck(key.GetUnderlying(), value);
244 return builder.ExtractValue();
254 for (
const auto& [key, value] : value) {
255 builder.EmplaceNocheck(key, value);
257 return builder.ExtractValue();