forked from scylladb/seastar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
formatter.hh
177 lines (151 loc) · 4.63 KB
/
formatter.hh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*
* This file is open source software, licensed to you under the terms
* of the Apache License, Version 2.0 (the "License"). See the NOTICE file
* distributed with this work for additional information regarding copyright
* ownership. You may not use this file except in compliance with the License.
*
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
/*
* Copyright 2015 Cloudius Systems
*/
#ifndef FORMATTER_HH_
#define FORMATTER_HH_
#include <string>
#include <vector>
#include <unordered_map>
#include <map>
#include <time.h>
#include <sstream>
#include "core/sstring.hh"
namespace json {
struct jsonable;
typedef struct tm date_time;
/**
* The formatter prints json values in a json format
* it overload to_json method for each of the supported format
* all to_json parameters are passed as a pointer
*/
class formatter {
enum class state {
none, array, map
};
static sstring begin(state);
static sstring end(state);
template<typename K, typename V>
static sstring to_json(state s, const std::pair<K, V>& p) {
return s == state::array ?
"{" + to_json(state::none, p) + "}" :
to_json(p.first) + ":" + to_json(p.second);
}
template<typename Iter>
static sstring to_json(state s, Iter i, Iter e) {
std::stringstream res;
res << begin(s);
size_t n = 0;
while (i != e) {
if (n++ != 0) {
res << ",";
}
res << to_json(s, *i++);
}
res << end(s);
return res.str();
}
// fallback template
template<typename T>
static sstring to_json(state, const T& t) {
return to_json(t);
}
public:
/**
* return a json formated string
* @param str the string to format
* @return the given string in a json format
*/
static sstring to_json(const sstring& str);
/**
* return a json formated int
* @param n the int to format
* @return the given int in a json format
*/
static sstring to_json(int n);
/**
* return a json formated long
* @param n the long to format
* @return the given long in a json format
*/
static sstring to_json(long n);
/**
* return a json formated float
* @param n the float to format
* @return the given float in a json format
*/
static sstring to_json(float f);
/**
* return a json formated double
* @param d the double to format
* @return the given double in a json format
*/
static sstring to_json(double d);
/**
* return a json formated char* (treated as string)
* @param str the char* to foramt
* @return the given char* in a json foramt
*/
static sstring to_json(const char* str);
/**
* return a json formated bool
* @param d the bool to format
* @return the given bool in a json format
*/
static sstring to_json(bool d);
/**
* return a json formated list of a given vector of params
* @param vec the vector to format
* @return the given vector in a json format
*/
template<typename... Args>
static sstring to_json(const std::vector<Args...>& vec) {
return to_json(state::array, vec.begin(), vec.end());
}
template<typename... Args>
static sstring to_json(const std::map<Args...>& map) {
return to_json(state::map, map.begin(), map.end());
}
template<typename... Args>
static sstring to_json(const std::unordered_map<Args...>& map) {
return to_json(state::map, map.begin(), map.end());
}
/**
* return a json formated date_time
* @param d the date_time to format
* @return the given date_time in a json format
*/
static sstring to_json(const date_time& d);
/**
* return a json formated json object
* @param obj the date_time to format
* @return the given json object in a json format
*/
static sstring to_json(const jsonable& obj);
/**
* return a json formated unsigned long
* @param l unsigned long to format
* @return the given unsigned long in a json format
*/
static sstring to_json(unsigned long l);
private:
static constexpr const char* TIME_FORMAT = "%a %b %d %I:%M:%S %Z %Y";
};
}
#endif /* FORMATTER_HH_ */