forked from scylladb/seastar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjson_elements.hh
267 lines (234 loc) · 6.41 KB
/
json_elements.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/*
* 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 JSON_ELEMENTS_HH_
#define JSON_ELEMENTS_HH_
#include <string>
#include <vector>
#include <time.h>
#include <sstream>
#include "formatter.hh"
#include "core/sstring.hh"
namespace json {
/**
* The base class for all json element.
* Every json element has a name
* An indication if it was set or not
* And is this element is mandatory.
* When a mandatory element is not set
* this is not a valid object
*/
class json_base_element {
public:
/**
* The constructors
*/
json_base_element()
: _mandatory(false), _set(false) {
}
virtual ~json_base_element() = default;
/**
* Check if it's a mandatory parameter
* and if it's set.
* @return true if this is not a mandatory parameter
* or if it is and it's value is set
*/
virtual bool is_verify() {
return !(_mandatory && !_set);
}
json_base_element& operator=(const json_base_element& o) {
// Names and mandatory are never changed after creation
_set = o._set;
return *this;
}
/**
* returns the internal value in a json format
* Each inherit class must implement this method
* @return formated internal value
*/
virtual std::string to_string() = 0;
std::string _name;
bool _mandatory;
bool _set;
};
/**
* Basic json element instantiate
* the json_element template.
* it adds a value to the base definition
* and the to_string implementation using the formatter
*/
template<class T>
class json_element : public json_base_element {
public:
/**
* the assignment operator also set
* the set value to true.
* @param new_value the new value
* @return the value itself
*/
json_element &operator=(const T& new_value) {
_value = new_value;
_set = true;
return *this;
}
/**
* the assignment operator also set
* the set value to true.
* @param new_value the new value
* @return the value itself
*/
template<class C>
json_element &operator=(const C& new_value) {
_value = new_value;
_set = true;
return *this;
}
/**
* The brackets operator
* @return the value
*/
const T& operator()() const {
return _value;
}
/**
* The to_string return the value
* formated as a json value
* @return the value foramted for json
*/
virtual std::string to_string() override
{
return formatter::to_json(_value);
}
private:
T _value;
};
/**
* json_list is based on std vector implementation.
*
* When values are added with push it is set the "set" flag to true
* hence will be included in the parsed object
*/
template<class T>
class json_list : public json_base_element {
public:
/**
* Add an element to the list.
* @param element a new element that will be added to the list
*/
void push(const T& element) {
_set = true;
_elements.push_back(element);
}
virtual std::string to_string() override
{
return formatter::to_json(_elements);
}
/**
* Assignment can be done from any object that support const range
* iteration and that it's elements can be assigned to the list elements
*/
template<class C>
json_list& operator=(const C& list) {
_elements.clear();
for (auto i : list) {
push(i);
}
return *this;
}
std::vector<T> _elements;
};
class jsonable {
public:
virtual ~jsonable() = default;
/**
* create a foramted string of the object.
* @return the object formated.
*/
virtual std::string to_json() const = 0;
};
/**
* The base class for all json objects
* It holds a list of all the element in it,
* allowing it implement the to_json method.
*
* It also allows iterating over the element
* in the object, even if not all the member
* are known in advance and in practice mimic
* reflection
*/
struct json_base : public jsonable {
virtual ~json_base() = default;
json_base() = default;
json_base(const json_base&) = delete;
json_base operator=(const json_base&) = delete;
/**
* create a foramted string of the object.
* @return the object formated.
*/
virtual std::string to_json() const;
/**
* Check that all mandatory elements are set
* @return true if all mandatory parameters are set
*/
virtual bool is_verify() const;
/**
* Register an element in an object
* @param element the element to be added
* @param name the element name
* @param mandatory is this element mandatory.
*/
virtual void add(json_base_element* element, std::string name,
bool mandatory = false);
std::vector<json_base_element*> _elements;
};
/**
* There are cases where a json request needs to return a successful
* empty reply.
* The json_void class will be used to mark that the reply should be empty.
*
*/
struct json_void : public jsonable{
virtual std::string to_json() const {
return "";
}
};
/**
* The json return type, is a helper class to return a json
* formatted string.
* It uses autoboxing in its constructor so when a function return
* type is json_return_type, it could return a type that would be converted
* ie.
* json_return_type foo() {
* return "hello";
* }
*
* would return a json formatted string: "hello" (rather then hello)
*/
struct json_return_type {
sstring _res;
template<class T>
json_return_type(const T& res) {
_res = formatter::to_json(res);
}
json_return_type(json_return_type&&) = default;
json_return_type& operator=(json_return_type&&) = default;
};
}
#endif /* JSON_ELEMENTS_HH_ */