-
Notifications
You must be signed in to change notification settings - Fork 14
/
JsonSerializer.hpp
306 lines (273 loc) · 8.52 KB
/
JsonSerializer.hpp
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
#pragma once
#include <chrono>
#include <limits>
#include <memory>
#include <optional>
#include <seastar/core/shared_ptr.hh>
#include "../Allocators/StackAllocator.hpp"
#include "../Utility/ConstantStrings.hpp"
#include "../Utility/ObjectTrait.hpp"
#include "../Utility/Packet.hpp"
#include "../Utility/SharedString.hpp"
// construct JsonMemberKey, please ensure key is already encoded
#define CPV_JSONKEY(key) cpv::JsonMemberKey("\"" key "\":", ",\"" key "\":")
namespace cpv {
/**
* Encode string for use in json,
* may return original string if not changed.
*/
SharedString jsonEncode(SharedString&& str);
/**
* Encoded static member key of json object.
* Please use CPV_JSONKEY to construct it,
* and ensure the key pass to constructor is already encoded.
*/
class JsonMemberKey {
public:
/** Get "encoded key": */
std::string_view getEncodedKey() const {
return encodedKey_;
}
/** Get ,"encoded key": */
std::string_view getEncodedKeyWithPreviousComma() const {
return encodedKeyWithPreviousComma_;
}
/** Constructor */
JsonMemberKey(
std::string_view encodedKey,
std::string_view encodedKeyWithPreviousComma) :
encodedKey_(encodedKey),
encodedKeyWithPreviousComma_(encodedKeyWithPreviousComma) { }
private:
std::string_view encodedKey_;
std::string_view encodedKeyWithPreviousComma_;
};
/**
* The class used to build json packet.
* For performance reason, it won't validate the sequence of operations,
* you should use unit tests to ensure the generated json structure is valid.
*/
class JsonBuilder {
public:
/** Write { to json packet */
JsonBuilder& startObject() {
writeRaw(constants::CurlyBacketStart);
addPreviousComma_ = false;
return *this;
}
/**
* Write key and value to json packet.
* It should be used between startObject and endObject.
*/
template <class T>
JsonBuilder& addMember(const SharedString& key, const T& value);
/**
* Write static encoded key and value to json packet.
* It should be used between startObject and endObject.
*/
template <class T>
JsonBuilder& addMember(const JsonMemberKey& key, const T& value);
/** Write } to json packet */
JsonBuilder& endObject() {
writeRaw(constants::CurlyBacketEnd);
addPreviousComma_ = true;
return *this;
}
/** Write [ to json packet */
JsonBuilder& startArray() {
writeRaw(constants::SquareBacketStart);
addPreviousComma_ = false;
return *this;
}
/**
* Write item to json packet.
* It should be used between startArray and endArray.
*/
template <class T>
JsonBuilder& addItem(const T& value);
/** Write ] to json packet */
JsonBuilder& endArray() {
writeRaw(constants::SquareBacketEnd);
addPreviousComma_ = true;
return *this;
}
/** Write raw string */
template <class... Args>
void writeRaw(Args&&... args) {
fragments_->append(std::forward<Args>(args)...);
}
/** Get the json packet, don't touch the json builder after invoked this */
Packet toPacket() && {
fragments_ = nullptr;
return std::move(packet_);
}
/** Constructor with capacity of packet */
explicit JsonBuilder(std::size_t capacity) :
packet_(capacity),
fragments_(&packet_.getOrConvertToMultiple()),
addPreviousComma_(false) { }
private:
Packet packet_;
Packet::MultipleFragments* fragments_;
bool addPreviousComma_;
};
/**
* The class used to write model to json builder.
*
* The model type should contains a public function named dumpJson
* that takes an instance of JsonBuilder.
*
* You can specialize it for more types.
*/
template <class T, class = void /* for enable_if */>
struct JsonBuilderWriter {
/** Write model to json builder */
static void write(const T& model, JsonBuilder& builder) {
model.dumpJson(builder);
}
};
/** Specialize for integer (except of bool) */
template <class T>
struct JsonBuilderWriter<T,
std::enable_if_t<std::numeric_limits<T>::is_integer && !std::is_same_v<T, bool>>> {
/** Write integer to json builder */
static void write(const T& value, JsonBuilder& builder) {
builder.writeRaw(SharedString::fromInt(value));
}
};
/** Specialize for floating point */
template <class T>
struct JsonBuilderWriter<T,
std::enable_if_t<std::is_floating_point_v<T>>> {
/** Write floating point to json builder */
static void write(const T& value, JsonBuilder& builder) {
builder.writeRaw(SharedString::fromDouble(value));
}
};
/** Specialize for bool */
template <>
struct JsonBuilderWriter<bool> {
/** Write boolean to json builder */
static void write(bool value, JsonBuilder& builder) {
if (value) {
builder.writeRaw(constants::True);
} else {
builder.writeRaw(constants::False);
}
}
};
/** Specialize for SharedString */
template <>
struct JsonBuilderWriter<SharedString> {
/** Write SharedString to json builder */
static void write(const SharedString& value, JsonBuilder& builder) {
builder.writeRaw(constants::DoubleQuote);
builder.writeRaw(jsonEncode(value.share()));
builder.writeRaw(constants::DoubleQuote);
}
};
/** Specialize for std::string */
template <>
struct JsonBuilderWriter<std::string> {
/** Write std::string to json builder */
static void write(const std::string& value, JsonBuilder& builder) {
builder.writeRaw(constants::DoubleQuote);
builder.writeRaw(jsonEncode(SharedString(value)));
builder.writeRaw(constants::DoubleQuote);
}
};
/** Specialize for collection like types */
template <class T>
struct JsonBuilderWriter<T, std::enable_if_t<
ObjectTrait<T>::IsCollectionLike && !ObjectTrait<T>::IsPointerLike>> {
/** Write collection like oject to json builder */
static void write(const T& values, JsonBuilder& builder) {
builder.startArray();
ObjectTrait<T>::apply(values, [&builder] (const auto& value) {
builder.addItem(value);
});
builder.endArray();
}
};
/** Specialize for pointer like types */
template <class T>
struct JsonBuilderWriter<T, std::enable_if_t<ObjectTrait<T>::IsPointerLike>> {
/** Write pointer like object to json builder */
static void write(const T& value, JsonBuilder& builder) {
using UnderlyingType = typename ObjectTrait<T>::UnderlyingType;
const UnderlyingType* ptr = ObjectTrait<T>::get(value);
if (ptr != nullptr) {
JsonBuilderWriter<UnderlyingType>::write(*ptr, builder);
} else {
builder.writeRaw(constants::Null);
}
}
};
/** Specialize for chrono durations */
template <class Rep, class Period>
struct JsonBuilderWriter<std::chrono::duration<Rep, Period>> {
/** Write chrono durations to json builder */
static void write(
const std::chrono::duration<Rep, Period>& value, JsonBuilder& builder) {
builder.writeRaw(SharedString::fromInt(value.count()));
}
};
/** Write key and value to json packet */
template <class T>
JsonBuilder& JsonBuilder::addMember(const SharedString& key, const T& value) {
if (addPreviousComma_) {
writeRaw(constants::Comma);
} else {
addPreviousComma_ = true;
}
JsonBuilderWriter<SharedString>::write(key, *this);
writeRaw(constants::Colon);
JsonBuilderWriter<T>::write(value, *this);
return *this;
}
/** Write static encoded key and value to json packet */
template <class T>
JsonBuilder& JsonBuilder::addMember(const JsonMemberKey& key, const T& value) {
if (addPreviousComma_) {
writeRaw(SharedString::fromStatic(key.getEncodedKeyWithPreviousComma()));
} else {
writeRaw(SharedString::fromStatic(key.getEncodedKey()));
addPreviousComma_ = true;
}
JsonBuilderWriter<T>::write(value, *this);
return *this;
}
/** Write item to json packet */
template <class T>
JsonBuilder& JsonBuilder::addItem(const T& value) {
if (addPreviousComma_) {
writeRaw(constants::Comma);
} else {
addPreviousComma_ = true;
}
JsonBuilderWriter<T>::write(value, *this);
return *this;
}
/**
* The class used to serialize model to json packet.
* The model should be writable to JsonBuilder from JsonBuilderWriter.
* If you want to build a json without model, you can use JsonBuilder directly.
*/
template <class T, class = void /* for enable_if */>
class JsonSerializer {
public:
/** The default packet capacity */
static const constexpr std::size_t PacketCapacity = 128;
/** Serialize model to json packet */
static Packet serialize(const T& model) {
JsonBuilder builder(PacketCapacity);
JsonBuilderWriter<T>::write(model, builder);
return std::move(builder).toPacket();
}
};
/** Convenient static function for JsonSerializer */
template <class T>
static inline Packet serializeJson(const T& model) {
return JsonSerializer<T>::serialize(model);
}
}