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
6
7#include <chrono>
8#include <cstdint>
9#include <string_view>
10#include <utility>
11
12#include <userver/formats/json/impl/types.hpp>
14
15USERVER_NAMESPACE_BEGIN
16
17namespace formats::json {
18
22template <typename... Args>
23Value MakeObject(Args&&...);
24
28template <typename... Args>
29Value MakeArray(Args&&...);
30
31namespace impl {
32
33class InlineObjectBuilder {
34public:
35 InlineObjectBuilder();
36
37 template <typename... Args>
38 formats::json::Value Build(Args&&... args) {
39 static_assert(sizeof...(args) % 2 == 0, "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
44private:
45 formats::json::Value DoBuild();
46
47 template <typename FieldValue, typename... Tail>
48 formats::json::Value DoBuild(std::string_view key, FieldValue&& value, Tail&&... tail) {
49 Append(key, std::forward<FieldValue>(value));
50 return DoBuild(std::forward<Tail>(tail)...);
51 }
52
53 void Reserve(size_t);
54
55 void Append(std::string_view key, std::nullptr_t);
56 void Append(std::string_view key, bool);
57 void Append(std::string_view key, int);
58 void Append(std::string_view key, unsigned int);
59 void Append(std::string_view key, long);
60 void Append(std::string_view key, unsigned long);
61 void Append(std::string_view key, long long);
62 void Append(std::string_view key, unsigned long long);
63 void Append(std::string_view key, double);
64 void Append(std::string_view key, const char*);
65 void Append(std::string_view key, std::string_view);
66 void Append(std::string_view key, std::chrono::system_clock::time_point);
67
68 void Append(std::string_view key, const formats::json::Value&);
69 void Append(std::string_view key, formats::json::Value&&);
70
71 VersionedValuePtr json_;
72};
73
74class InlineArrayBuilder {
75public:
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
87private:
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 void Append(formats::json::Value&&);
105
106 VersionedValuePtr json_;
107};
108
109} // namespace impl
110
112template <typename... Args>
113Value MakeObject(Args&&... args) {
114 return impl::InlineObjectBuilder().Build(std::forward<Args>(args)...);
115}
116
117template <typename... Args>
118Value MakeArray(Args&&... args) {
119 return impl::InlineArrayBuilder().Build(std::forward<Args>(args)...);
120}
122
123} // namespace formats::json
124
125USERVER_NAMESPACE_END