/
json5_write_archive.h
248 lines (210 loc) · 6.7 KB
/
json5_write_archive.h
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
// Copyright 2019-2020 Josh Pieper, jjp@pobox.com.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// 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.
#pragma once
#include <array>
#include <iostream>
#include <optional>
#include <sstream>
#include <string>
#include <vector>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <fmt/format.h>
#include "mjlib/base/bytes.h"
#include "mjlib/base/escape_json_string.h"
#include "mjlib/base/priority_tag.h"
#include "mjlib/base/visitor.h"
#include "mjlib/base/visit_archive.h"
namespace mjlib {
namespace base {
/// Emit JSON5 from a given serializable.
///
/// https://json5.org
class Json5WriteArchive : public VisitArchive<Json5WriteArchive> {
public:
struct Options {
int indent = 0;
/// Emit only standard JSON
bool standard = false;
Options() {}
Options& set_indent(int value) {
indent = value;
return *this;
}
Options& set_standard(bool value) {
standard = value;
return *this;
}
};
Json5WriteArchive(std::ostream& stream, const Options& options = Options())
: stream_(stream),
options_(options) {}
template <typename Value>
static std::string Write(const Value& value,
const Options& options = Options()) {
std::ostringstream ostr;
Json5WriteArchive(ostr, options).Value(value);
return ostr.str();
}
template <typename Serializable>
Json5WriteArchive& Accept(Serializable* serializable) {
stream_ << "{\n";
VisitArchive<Json5WriteArchive>::Accept(serializable);
stream_ << "\n" << Indent(-2) << "}";
return *this;
}
template <typename ValueType>
Json5WriteArchive& Value(const ValueType& value) {
base::ReferenceNameValuePair nvp(const_cast<ValueType*>(&value), "");
VisitArchive<Json5WriteArchive>::Visit(nvp);
return *this;
}
template <typename NameValuePair>
void Visit(const NameValuePair& nvp) {
if (!first_) {
stream_ << ",\n";
}
first_ = false;
// Write out the field name and colon.
stream_ << Indent() << "\"" << nvp.name() << "\" : ";
VisitArchive<Json5WriteArchive>::Visit(nvp);
}
template <typename NameValuePair>
void VisitSerializable(const NameValuePair& nvp) {
auto new_options = options_;
Json5WriteArchive sub_archive(
stream_, new_options.set_indent(options_.indent + 2));
sub_archive.Accept(nvp.value());
}
template <typename NameValuePair, typename NameMapGetter>
void VisitEnumeration(const NameValuePair& nvp,
NameMapGetter enumeration_mapper) {
stream_ << "\"" << enumeration_mapper().at(*nvp.value()) << "\"";
}
template <typename NameValuePair>
void VisitArray(const NameValuePair& nvp) {
WriteArray(*nvp.value());
}
template <typename NameValuePair>
void VisitScalar(const NameValuePair& nvp) {
VisitHelper(nvp, nvp.value(), base::PriorityTag<2>());
}
private:
template <typename NameValuePair>
void VisitHelper(const NameValuePair& nvp,
Bytes* value,
base::PriorityTag<2> tag) {
VisitHelper(nvp, static_cast<std::vector<uint8_t>*>(value), tag);
}
template <typename NameValuePair, typename T>
void VisitHelper(const NameValuePair&,
std::vector<T>* value,
base::PriorityTag<1>) {
WriteArray(*value);
}
template <typename NameValuePair, typename T>
void VisitHelper(const NameValuePair&,
std::optional<T>* value,
base::PriorityTag<1>) {
if (!(*value)) {
stream_ << "null";
} else {
Value(**value);
}
}
template <typename NameValuePair>
void VisitHelper(const NameValuePair&,
boost::posix_time::ptime* value,
base::PriorityTag<1>) {
stream_ << "\"";
stream_ << boost::posix_time::to_simple_string(*value);
stream_ << "\"";
}
template <typename NameValuePair>
void VisitHelper(const NameValuePair&,
boost::posix_time::time_duration* value,
base::PriorityTag<1>) {
stream_ << "\"";
stream_ << boost::posix_time::to_simple_string(*value);
stream_ << "\"";
}
template <typename NameValuePair>
void VisitHelper(const NameValuePair&,
std::string* value,
base::PriorityTag<1>) {
stream_ << "\"";
stream_ << EscapeJsonString(*value);
stream_ << "\"";
}
template <typename T>
void FormatFloat(T value, const char* format_string) {
if (value == std::numeric_limits<T>::infinity()) {
stream_ << (options_.standard ? "null" : "Infinity");
} else if (value == -std::numeric_limits<T>::infinity()) {
stream_ << (options_.standard ? "null" : "-Infinity");
} else if (!std::isfinite(value)) {
stream_ << (options_.standard ? "null" : "NaN");
} else {
stream_ << fmt::format(format_string, value);
}
}
template <typename NameValuePair>
void VisitHelper(const NameValuePair&,
float* value,
base::PriorityTag<1>) {
FormatFloat(*value, "{:.9g}");
}
template <typename NameValuePair>
void VisitHelper(const NameValuePair&,
double* value,
base::PriorityTag<1>) {
FormatFloat(*value, "{:.17g}");
}
template <typename NameValuePair>
void VisitHelper(const NameValuePair& nvp,
bool*,
base::PriorityTag<1>) {
stream_ << (nvp.get_value() ? "true" : "false");
}
template <typename NameValuePair, typename T>
void VisitHelper(const NameValuePair&,
T* value,
base::PriorityTag<0>) {
stream_ << fmt::format("{:d}", *value);
}
template <typename Array>
void WriteArray(Array& array) {
stream_ << "[\n";
auto options_copy = options_;
Json5WriteArchive sub_archive(
stream_, options_copy.set_indent(options_copy.indent + 2));
bool first = true;
for (auto& item : array) {
if (!first) {
stream_ << ",\n";
}
first = false;
stream_ << Indent(2);
Value(item);
}
stream_ << "\n" << Indent() << "]";
}
std::string Indent(int extra = 0) {
return std::string(options_.indent + extra + 2, ' ');
}
std::ostream& stream_;
const Options options_;
bool first_ = true;
};
}
}