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
6#include <chrono>
7#include <cstdint>
8#include <string_view>
9
10#include <userver/formats/json/impl/types.hpp>
11#include <userver/formats/json/value.hpp>
12
13USERVER_NAMESPACE_BEGIN
14
15namespace formats::json {
16
17/// @ingroup userver_universal userver_formats
18///
19/// Constructs an object Value from provided key-value pairs
20template <typename... Args>
21Value MakeObject(Args&&...);
22
23/// @ingroup userver_universal userver_formats
24///
25/// Constructs an array Value from provided element list
26template <typename... Args>
27Value MakeArray(Args&&...);
28
29namespace impl {
30
31class InlineObjectBuilder {
32public:
33 InlineObjectBuilder();
34
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)...);
40 }
41
42private:
43 formats::json::Value DoBuild();
44
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)...);
49 }
50
51 void Reserve(size_t);
52
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);
65
66 void Append(std::string_view key, const formats::json::Value&);
67
68 VersionedValuePtr json_;
69};
70
71class InlineArrayBuilder {
72public:
73 InlineArrayBuilder();
74
75 formats::json::Value Build();
76
77 template <typename... Elements>
78 formats::json::Value Build(Elements&&... elements) {
79 Reserve(sizeof...(elements));
80 (Append(std::forward<Elements>(elements)), ...);
81 return Build();
82 }
83
84private:
85 void Reserve(size_t);
86
87 void Append(std::nullptr_t);
88 void Append(bool);
89 void Append(int);
90 void Append(unsigned int);
91 void Append(long);
92 void Append(unsigned long);
93 void Append(long long);
94 void Append(unsigned long long);
95 void Append(double);
96 void Append(const char*);
97 void Append(std::string_view);
98 void Append(std::chrono::system_clock::time_point);
99
100 void Append(const formats::json::Value&);
101
102 VersionedValuePtr json_;
103};
104
105} // namespace impl
106
107/// @cond
108template <typename... Args>
109Value MakeObject(Args&&... args) {
110 return impl::InlineObjectBuilder().Build(std::forward<Args>(args)...);
111}
112
113template <typename... Args>
114Value MakeArray(Args&&... args) {
115 return impl::InlineArrayBuilder().Build(std::forward<Args>(args)...);
116}
117/// @endcond
118
119} // namespace formats::json
120
121USERVER_NAMESPACE_END