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) {
38 sizeof...(args) % 2 == 0,
39 "Cannot build an object from an odd number of key-value arguments");
40 Reserve(
sizeof...(args) / 2);
41 return DoBuild(std::forward<Args>(args)...);
45 formats::
json::Value DoBuild();
47 template <
typename FieldValue,
typename... Tail>
48 formats::
json::Value DoBuild(std::string_view key, FieldValue&& value,
50 Append(key, std::forward<FieldValue>(value));
51 return DoBuild(std::forward<Tail>(tail)...);
56 void Append(std::string_view key, std::nullptr_t);
57 void Append(std::string_view key,
bool);
58 void Append(std::string_view key,
int);
59 void Append(std::string_view key,
unsigned int);
60 void Append(std::string_view key,
long);
61 void Append(std::string_view key,
unsigned long);
62 void Append(std::string_view key,
long long);
63 void Append(std::string_view key,
unsigned long long);
64 void Append(std::string_view key,
double);
65 void Append(std::string_view key,
const char*);
66 void Append(std::string_view key, std::string_view);
67 void Append(std::string_view key, std::chrono::system_clock::time_point);
69 void Append(std::string_view key,
const formats::
json::Value&);
71 VersionedValuePtr json_;
74class InlineArrayBuilder {
78 formats::
json::Value Build();
80 template <
typename... Elements>
81 formats::
json::Value Build(Elements&&... elements) {
82 Reserve(
sizeof...(elements));
83 (Append(std::forward<Elements>(elements)), ...);
90 void Append(std::nullptr_t);
93 void Append(
unsigned int);
95 void Append(
unsigned long);
96 void Append(
long long);
97 void Append(
unsigned long long);
99 void Append(
const char*);
100 void Append(std::string_view);
101 void Append(std::chrono::system_clock::time_point);
103 void Append(
const formats::
json::Value&);
105 VersionedValuePtr json_;
111template <
typename... Args>
113 return impl::InlineObjectBuilder().Build(std::forward<Args>(args)...);
116template <
typename... Args>
118 return impl::InlineArrayBuilder().Build(std::forward<Args>(args)...);