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(
const char* str);
75 ValueBuilder(
const std::string& str);
76 ValueBuilder(std::string_view str);
78 ValueBuilder(
unsigned int t);
79 ValueBuilder(uint64_t t);
80 ValueBuilder(int64_t t);
81 ValueBuilder(
float t);
82 ValueBuilder(
double t);
100 ValueBuilder
operator[](utils::StrongTypedef<Tag, std::string, Ops> key);
172 class EmplaceEnabler {};
176 ValueBuilder(EmplaceEnabler, impl::MutableValueWrapper)
noexcept;
180 enum class CheckMemberExists { kYes, kNo };
182 explicit ValueBuilder(impl::MutableValueWrapper)
noexcept;
184 static void Copy(impl::Value& to,
const ValueBuilder& from);
185 static void Move(impl::Value& to, ValueBuilder&& from);
187 impl::Value& AddMember(std::string_view key, CheckMemberExists);
189 template <
typename T>
190 static Value DoSerialize(
const T& t);
192 impl::MutableValueWrapper value_;
199Value ValueBuilder::DoSerialize(
const T& t) {
201 formats::
common::impl::kHasSerialize<Value, T>,
202 "There is no `Serialize(const T&, formats::serialize::To<json::Value>)` "
203 "in namespace of `T` or `formats::serizalize`. "
205 "Probably you forgot to include the "
206 "<userver/formats/serialize/common_containers.hpp> header "
207 "or one of the <formats/json/serialize_*.hpp> headers or you "
208 "have not provided a `Serialize` function overload.");
210 return Serialize(t, formats::serialize::To<Value>());
214std::enable_if_t<std::is_integral<T>::value &&
sizeof(T) <=
sizeof(int64_t),
216Serialize(T value, formats::serialize::To<Value>) {
217 using Type = std::conditional_t<std::is_signed<T>::value, int64_t, uint64_t>;
218 return json::ValueBuilder(
static_cast<Type>(value)).ExtractValue();
221json::Value Serialize(std::chrono::system_clock::time_point tp,
225ValueBuilder ValueBuilder::
operator[](
226 utils::StrongTypedef<Tag, std::string, Ops> key) {
227 return (*
this)[std::move(key.GetUnderlying())];
237 for (
const auto& [key, value] : value) {
238 builder.EmplaceNocheck(key.GetUnderlying(), value);
240 return builder.ExtractValue();
250 for (
const auto& [key, value] : value) {
251 builder.EmplaceNocheck(key, value);
253 return builder.ExtractValue();