-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuilder.cpp
206 lines (173 loc) · 7.12 KB
/
builder.cpp
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
/*
MIT License
Copyright (c) 2021 pavel.sokolov@gmail.com / CEZEO software Ltd. All rights reserved.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit
persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include "builder.h"
#include <rapidjson/stringbuffer.h>
#include <rapidjson/writer.h>
namespace json {
namespace {
// recursive function
void RecursiveJsonBuilder(rapidjson::Writer<rapidjson::StringBuffer>& writer, const builder::value_holder& value) {
switch (value.type) {
case builder::value_type::string_t:
writer.String(
value.primitive.string.data(), static_cast<rapidjson::SizeType>(value.primitive.string.size()), false);
break;
case builder::value_type::null_t:
writer.Null();
break;
case builder::value_type::bool_t:
writer.Bool(value.primitive.bool_flag);
break;
case builder::value_type::int_t:
writer.Int64(value.primitive.int_num);
break;
case builder::value_type::uint_t:
writer.Uint64(value.primitive.uint_num);
break;
case builder::value_type::double_t:
writer.Double(value.primitive.double_num);
break;
case builder::value_type::object_t:
// start writing object recursively
writer.StartObject();
for (const auto& field : value.object_holder) {
RAPIDJSON_ASSERT(nullptr != field.name.data());
writer.Key(field.name.data(), static_cast<rapidjson::SizeType>(field.name.size()), false);
RecursiveJsonBuilder(writer, field.value);
}
writer.EndObject();
// end writing object recursively
break;
case builder::value_type::array_t:
// start writing array recursively
writer.StartArray();
// detect source of items
if (builder::array_source::vector_t == value.array_value.source) {
for (const auto& array_holder : value.array_value.items) {
RecursiveJsonBuilder(writer, array_holder);
}
} else {
for (const auto& array_holder : value.array_value.list_items) {
RecursiveJsonBuilder(writer, array_holder);
}
}
writer.EndArray();
// end writing array recursively
break;
default:
RAPIDJSON_ASSERT(false);
}
}
// recursive function
void RecursiveValueBuilder(rapidjson::Value& result,
rapidjson::Document::AllocatorType& allocator,
const builder::value_holder& value) {
switch (value.type) {
case builder::value_type::string_t:
result.SetString(value.primitive.string.data(), static_cast<rapidjson::SizeType>(value.primitive.string.size()));
break;
case builder::value_type::null_t:
result.SetNull();
break;
case builder::value_type::bool_t:
result.SetBool(value.primitive.bool_flag);
break;
case builder::value_type::int_t:
result.SetInt64(value.primitive.int_num);
break;
case builder::value_type::uint_t:
result.SetUint64(value.primitive.uint_num);
break;
case builder::value_type::double_t:
result.SetDouble(value.primitive.double_num);
break;
case builder::value_type::object_t:
// start writing object recursively
result.SetObject();
for (const auto& field : value.object_holder) {
RAPIDJSON_ASSERT(nullptr != field.name.data());
// create rapid json value from details::value
rapidjson::Value member_value;
RecursiveValueBuilder(member_value, allocator, field.value);
result.AddMember(
rapidjson::StringRef(field.name.data(), field.name.size()), std::move(member_value), allocator);
}
// end writing object recursively
break;
case builder::value_type::array_t:
// start writing array recursively
result.SetArray();
// detect source of items
if (builder::array_source::vector_t == value.array_value.source) {
for (const auto& array_holder : value.array_value.items) {
rapidjson::Value member_value;
RecursiveValueBuilder(member_value, allocator, array_holder);
result.PushBack(std::move(member_value), allocator);
}
} else {
for (const auto& array_holder : value.array_value.list_items) {
rapidjson::Value member_value;
RecursiveValueBuilder(member_value, allocator, array_holder);
result.PushBack(std::move(member_value), allocator);
}
}
// end writing array recursively
break;
default:
RAPIDJSON_ASSERT(false);
}
}
} // namespace
std::string stringify(const rapidjson::Document& document) {
rapidjson::StringBuffer buffer;
rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
document.Accept(writer);
return std::string(buffer.GetString(), buffer.GetSize());
}
builder::array_holder array(std::initializer_list<builder::value_holder> list) {
return builder::array_holder(list);
}
/**
* \brief build json string
*/
std::string build(const builder::value_holder& value) {
RAPIDJSON_ASSERT(builder::value_type::array_t == value.type || builder::value_type::object_t == value.type);
rapidjson::StringBuffer string_buffer;
rapidjson::Writer<rapidjson::StringBuffer> writer(string_buffer);
// recursive builder
RecursiveJsonBuilder(writer, value);
// get the json string and return it
return std::string(string_buffer.GetString(), string_buffer.GetSize());
}
/**
* \brief build rapidjson value (array or object)
*/
rapidjson::Value build_value(const builder::value_holder& value, rapidjson::Document::AllocatorType& allocator) {
rapidjson::Value result;
RAPIDJSON_ASSERT(builder::value_type::array_t == value.type || builder::value_type::object_t == value.type);
// recursive builder
RecursiveValueBuilder(result, allocator, value);
return result;
}
/**
* \brief build rapidjson document with array or object
*/
rapidjson::Document build_document(const builder::value_holder& value) {
rapidjson::Document result;
rapidjson::Value rapid_value(build_value(value, result.GetAllocator()));
rapid_value.Swap(result);
return result;
}
} // namespace json