userver: userver/formats/json/inline.hpp Source File
Loading...
Searching...
No Matches
inline.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/formats/json/inline.hpp
4/// @brief Inline value builders
5/// @ingroup userver_universal
6
7#include <chrono>
8#include <cstdint>
9#include <string_view>
10
11#include <userver/formats/json/impl/types.hpp>
12#include <userver/formats/json/value.hpp>
13
14USERVER_NAMESPACE_BEGIN
15
16namespace formats::json {
17
18/// @ingroup userver_formats
19///
20/// Constructs an object Value from provided key-value pairs
21template <typename... Args>
22Value MakeObject(Args&&...);
23
24/// @ingroup userver_formats
25///
26/// Constructs an array Value from provided element list
27template <typename... Args>
28Value MakeArray(Args&&...);
29
30namespace impl {
31
32class InlineObjectBuilder {
33public:
34 InlineObjectBuilder();
35
36 template <typename... Args>
37 formats::json::Value Build(Args&&... args) {
38 static_assert(sizeof...(args) % 2 == 0, "Cannot build an object from an odd number of key-value arguments");
39 Reserve(sizeof...(args) / 2);
40 return DoBuild(std::forward<Args>(args)...);
41 }
42
43private:
44 formats::json::Value DoBuild();
45
46 template <typename FieldValue, typename... Tail>
47 formats::json::Value DoBuild(std::string_view key, FieldValue&& value, Tail&&... tail) {
48 Append(key, std::forward<FieldValue>(value));
49 return DoBuild(std::forward<Tail>(tail)...);
50 }
51
52 void Reserve(size_t);
53
54 void Append(std::string_view key, std::nullptr_t);
55 void Append(std::string_view key, bool);
56 void Append(std::string_view key, int);
57 void Append(std::string_view key, unsigned int);
58 void Append(std::string_view key, long);
59 void Append(std::string_view key, unsigned long);
60 void Append(std::string_view key, long long);
61 void Append(std::string_view key, unsigned long long);
62 void Append(std::string_view key, double);
63 void Append(std::string_view key, const char*);
64 void Append(std::string_view key, std::string_view);
65 void Append(std::string_view key, std::chrono::system_clock::time_point);
66
67 void Append(std::string_view key, const formats::json::Value&);
68
69 VersionedValuePtr json_;
70};
71
72class InlineArrayBuilder {
73public:
74 InlineArrayBuilder();
75
76 formats::json::Value Build();
77
78 template <typename... Elements>
79 formats::json::Value Build(Elements&&... elements) {
80 Reserve(sizeof...(elements));
81 (Append(std::forward<Elements>(elements)), ...);
82 return Build();
83 }
84
85private:
86 void Reserve(size_t);
87
88 void Append(std::nullptr_t);
89 void Append(bool);
90 void Append(int);
91 void Append(unsigned int);
92 void Append(long);
93 void Append(unsigned long);
94 void Append(long long);
95 void Append(unsigned long long);
96 void Append(double);
97 void Append(const char*);
98 void Append(std::string_view);
99 void Append(std::chrono::system_clock::time_point);
100
101 void Append(const formats::json::Value&);
102
103 VersionedValuePtr json_;
104};
105
106} // namespace impl
107
108/// @cond
109template <typename... Args>
110Value MakeObject(Args&&... args) {
111 return impl::InlineObjectBuilder().Build(std::forward<Args>(args)...);
112}
113
114template <typename... Args>
115Value MakeArray(Args&&... args) {
116 return impl::InlineArrayBuilder().Build(std::forward<Args>(args)...);
117}
118/// @endcond
119
120} // namespace formats::json
121
122USERVER_NAMESPACE_END