forked from objectcomputing/mFAST
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjson_encode.cpp
More file actions
245 lines (211 loc) · 6.76 KB
/
json_encode.cpp
File metadata and controls
245 lines (211 loc) · 6.76 KB
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
// Copyright (c) 2016, Huang-Ming Huang, Object Computing, Inc.
// All rights reserved.
//
// This file is part of mFAST.
// See the file license.txt for licensing information.
#include "json.h"
#include "../enum_ref.h"
#include "../field_visitor.h"
#include "../nested_message_ref.h"
#include <boost/io/ios_state.hpp>
#include <cstdio>
#ifdef _MSC_VER // someday someone at microsoft will read the C++11 standard.
#define snprintf _snprintf
#else // _MSCVER
#define snprintf std::snprintf
#endif // _MSCVER
namespace mfast {
namespace json {
namespace encode_detail {
struct quoted_string {
quoted_string(const char *str) : str_(str) {}
const char *str_;
};
std::ostream &operator<<(std::ostream &os, quoted_string str) {
os.put('"');
const char *ptr = str.str_;
char c;
while ((c = *ptr++) != '\0') {
// According to ECMA-404, the control charanter (U+0000 to U+001F) must be
// escaped
if ((c & 0xE0) == 0) {
// if c is '\x08', '\x09','\0x0A','\0x0C' and '\0x0D', encoded as "\b",
// "\t", "\n", "\f", "\r" repsectively;
// otherwise encoded as "\uxxxx" where xxxx is 4 digit hexidecimal
// characters.
const char control_table[33] = "uuuuuuuubtnufruuuuuuuuuuuuuuuuuu";
char buf[7] = "\\";
buf[1] = control_table[static_cast<int>(c)];
if (buf[1] == 'u') {
snprintf(buf + 2, 5, "%04x", static_cast<int>(c));
}
os << buf;
continue;
}
if (c == '\\' || c == '"')
os.put('\\');
os.put(c);
}
os.put('"');
return os;
}
class json_visitor {
protected:
std::ostream &strm_;
char separator_[2];
unsigned json_object_tag_mask_;
public:
json_visitor(std::ostream &strm, unsigned json_object_tag_mask)
: strm_(strm), json_object_tag_mask_(json_object_tag_mask) {
separator_[0] = 0;
separator_[1] = 0;
}
template <typename NumericTypeRef> void visit(const NumericTypeRef &ref) {
strm_ << separator_ << ref.value();
}
void visit(const decimal_cref &ref) {
const decimal_value_storage &storage =
*reinterpret_cast<const decimal_value_storage *>(
field_cref_core_access::storage_of(ref));
strm_ << separator_ << storage;
}
void visit(const enum_cref &ref) {
if (ref.is_boolean())
strm_ << separator_ << ref.value_name();
else
strm_ << separator_ << ref.value();
}
template <typename Char> void visit(const mfast::string_cref<Char> &ref) {
strm_ << separator_ << quoted_string(ref.c_str());
}
void visit(const mfast::byte_vector_cref &ref) {
if (ref.instruction()->tag().to_uint64() & json_object_tag_mask_) {
// if the json_object_tag_mask is on, that means the field contains
// json encoded object already, just write it as it is without any
// processing.
strm_.rdbuf()->sputn(reinterpret_cast<const char *>(ref.data()),
ref.size());
} else {
// json doesn't have byte vector, treat it as hex string now
strm_ << separator_ << "\"";
boost::io::ios_flags_saver ifs(strm_);
strm_ << std::hex << std::setfill('0') << std::setw(2);
for (std::size_t i = 0; i < ref.size(); ++i) {
// if the size is 16, we treat it as a UUID
if (ref.size() == 16 && (i == 4 || i == 6 || i == 8 || i == 10))
strm_ << '-';
strm_ << (0xFF & (int)ref[i]);
}
strm_ << "\"" << std::setfill(' ');
}
}
template <typename IntType>
void visit(const mfast::int_vector_cref<IntType> &ref) {
strm_ << separator_ << "[";
separator_[0] = '\0';
for (auto &&field : ref) {
strm_ << separator_ << field;
separator_[0] = ',';
}
strm_ << "]";
}
virtual void visit(const mfast::aggregate_cref &ref, int) {
if (ref.num_fields() == 1) {
field_cref f0 = ref[0];
if (f0.instruction()->field_type() == mfast::field_type_templateref) {
if (f0.present())
this->visit(mfast::nested_message_cref(f0), 0);
return;
}
}
strm_ << separator_ << "{";
separator_[0] = '\0';
for (auto &&field : ref) {
if (field.present()) {
strm_ << separator_ << quoted_string(field.name()) << ":";
separator_[0] = '\0';
apply_accessor(*this, field);
;
separator_[0] = ',';
}
}
strm_ << "}";
}
void visit(const mfast::sequence_cref &ref, int) {
strm_ << separator_ << "[";
if (ref.size()) {
separator_[0] = '\0';
for (auto &&elem : ref)
this->visit(elem, 0);
}
strm_ << "]";
}
void visit(const mfast::sequence_element_cref &ref, int) {
if (ref.element_unnamed()) {
apply_accessor(*this, ref[0]);
;
} else {
this->visit(mfast::aggregate_cref(ref), 0);
}
separator_[0] = ',';
}
void visit(const mfast::nested_message_cref &ref, int) {
this->visit(mfast::aggregate_cref(ref), 0);
}
};
class json_visitor_with_ignore_tag : public json_visitor {
private:
unsigned ignore_tag_mask_;
public:
using json_visitor::visit;
json_visitor_with_ignore_tag(std::ostream &strm,
unsigned json_object_tag_mask,
unsigned ignore_tag_mask)
: json_visitor(strm, json_object_tag_mask),
ignore_tag_mask_(ignore_tag_mask) {}
virtual void visit(const mfast::aggregate_cref &ref, int) override {
if (ref.num_fields() == 1) {
field_cref f0 = ref[0];
if (f0.instruction()->field_type() == mfast::field_type_templateref) {
if (f0.present())
this->visit(mfast::nested_message_cref(f0), 0);
return;
}
}
strm_ << separator_ << "{";
separator_[0] = '\0';
for (auto &&field : ref) {
if (field.present() &&
0 == (field.instruction()->tag().to_uint64() & ignore_tag_mask_)) {
strm_ << separator_ << quoted_string(field.name()) << ":";
separator_[0] = '\0';
apply_accessor(*this, field);
;
separator_[0] = ',';
}
}
strm_ << "}";
}
};
} // namspace encode_detail
bool encode(std::ostream &os, const mfast::aggregate_cref &msg,
unsigned json_object_tag_mask) {
encode_detail::json_visitor visitor(os, json_object_tag_mask);
visitor.visit(msg, 0);
return os.good();
}
bool encode(std::ostream &os, const mfast::sequence_cref &seq,
unsigned json_object_tag_mask) {
encode_detail::json_visitor visitor(os, json_object_tag_mask);
visitor.visit(seq, 0);
return os.good();
}
bool encode(std::ostream &os, const ::mfast::aggregate_cref &msg,
unsigned json_object_tag_mask, unsigned ignore_tag_mask) {
encode_detail::json_visitor_with_ignore_tag visitor(os, json_object_tag_mask,
ignore_tag_mask);
visitor.visit(msg, 0);
return os.good();
}
} // namespace json
} // namespace mfast