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 {
32 public:
33 InlineObjectBuilder();
34
35 template <typename... Args>
36 formats::json::Value Build(Args&&... args) {
37 static_assert(
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)...);
42 }
43
44 private:
45 formats::json::Value DoBuild();
46
47 template <typename FieldValue, typename... Tail>
48 formats::json::Value DoBuild(std::string_view key, FieldValue&& value,
49 Tail&&... tail) {
50 Append(key, std::forward<FieldValue>(value));
51 return DoBuild(std::forward<Tail>(tail)...);
52 }
53
54 void Reserve(size_t);
55
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);
68
69 void Append(std::string_view key, const formats::json::Value&);
70
71 VersionedValuePtr json_;
72};
73
74class InlineArrayBuilder {
75 public:
76 InlineArrayBuilder();
77
78 formats::json::Value Build();
79
80 template <typename... Elements>
81 formats::json::Value Build(Elements&&... elements) {
82 Reserve(sizeof...(elements));
83 (Append(std::forward<Elements>(elements)), ...);
84 return Build();
85 }
86
87 private:
88 void Reserve(size_t);
89
90 void Append(std::nullptr_t);
91 void Append(bool);
92 void Append(int);
93 void Append(unsigned int);
94 void Append(long);
95 void Append(unsigned long);
96 void Append(long long);
97 void Append(unsigned long long);
98 void Append(double);
99 void Append(const char*);
100 void Append(std::string_view);
101 void Append(std::chrono::system_clock::time_point);
102
103 void Append(const formats::json::Value&);
104
105 VersionedValuePtr json_;
106};
107
108} // namespace impl
109
110/// @cond
111template <typename... Args>
112Value MakeObject(Args&&... args) {
113 return impl::InlineObjectBuilder().Build(std::forward<Args>(args)...);
114}
115
116template <typename... Args>
117Value MakeArray(Args&&... args) {
118 return impl::InlineArrayBuilder().Build(std::forward<Args>(args)...);
119}
120/// @endcond
121
122} // namespace formats::json
123
124USERVER_NAMESPACE_END