10#include <userver/formats/json/impl/types.hpp>
11#include <userver/formats/json/value.hpp>
13USERVER_NAMESPACE_BEGIN
15namespace formats::
json {
20template <
typename... Args>
26template <
typename... Args>
31class InlineObjectBuilder {
33 InlineObjectBuilder();
35 template <
typename... Args>
36 formats::
json::Value Build(Args&&... args) {
37 static_assert(
sizeof...(args) % 2 == 0,
"Cannot build an object from an odd number of key-value arguments");
38 Reserve(
sizeof...(args) / 2);
39 return DoBuild(std::forward<Args>(args)...);
43 formats::
json::Value DoBuild();
45 template <
typename FieldValue,
typename... Tail>
46 formats::
json::Value DoBuild(std::string_view key, FieldValue&& value, Tail&&... tail) {
47 Append(key, std::forward<FieldValue>(value));
48 return DoBuild(std::forward<Tail>(tail)...);
53 void Append(std::string_view key, std::nullptr_t);
54 void Append(std::string_view key,
bool);
55 void Append(std::string_view key,
int);
56 void Append(std::string_view key,
unsigned int);
57 void Append(std::string_view key,
long);
58 void Append(std::string_view key,
unsigned long);
59 void Append(std::string_view key,
long long);
60 void Append(std::string_view key,
unsigned long long);
61 void Append(std::string_view key,
double);
62 void Append(std::string_view key,
const char*);
63 void Append(std::string_view key, std::string_view);
64 void Append(std::string_view key, std::chrono::system_clock::time_point);
66 void Append(std::string_view key,
const formats::
json::Value&);
68 VersionedValuePtr json_;
71class InlineArrayBuilder {
75 formats::
json::Value Build();
77 template <
typename... Elements>
78 formats::
json::Value Build(Elements&&... elements) {
79 Reserve(
sizeof...(elements));
80 (Append(std::forward<Elements>(elements)), ...);
87 void Append(std::nullptr_t);
90 void Append(
unsigned int);
92 void Append(
unsigned long);
93 void Append(
long long);
94 void Append(
unsigned long long);
96 void Append(
const char*);
97 void Append(std::string_view);
98 void Append(std::chrono::system_clock::time_point);
100 void Append(
const formats::
json::Value&);
102 VersionedValuePtr json_;
108template <
typename... Args>
110 return impl::InlineObjectBuilder().Build(std::forward<Args>(args)...);
113template <
typename... Args>
115 return impl::InlineArrayBuilder().Build(std::forward<Args>(args)...);