-
Notifications
You must be signed in to change notification settings - Fork 39
/
json_writer.h
296 lines (244 loc) · 8.54 KB
/
json_writer.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
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
/*
Extremely simple JSON serialization for C++
www.github.com/g40/esj
Copyright (c) Jerry Evans, 2012-2014
All rights reserved.
Trivial example:
Given a class with std::wstring members a,b,c implement
a function called serialize() as in:
void serialize(JSON::json_adapter& adapter)
{
JSON::Class root(adapter,"classname");
JSON_E(adapter,a);
JSON_E(adapter,b);
JSON_T(adapter,c); // Note 'T' type
}
That's all that is required. The adapter can be a reader or writer, the
system is symmetric.
Given the constraints of JSON/Javascript semantics the only container supported
out of the box is std::vector<> and there is obviously no pointer support as the
additional JSON overhead required would not interop with any JS consumers/producers.
The MIT License (MIT)
Copyright (c) 2012-2014 Jerry Evans
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.
*/
#ifndef JSON_WRITER_H
#define JSON_WRITER_H
#ifndef JSON_ADAPTER_H
#include "json_adapter.h"
#endif
namespace JSON
{
//-----------------------------------------------------------------------------
// base class for facade over eventual destination,
// e.g. a socket instead of a string
class ISink
{
public:
virtual ISink& operator<<(const char*) = 0;
virtual ISink& operator<<(const std::string&) = 0;
virtual ISink& operator<<(const std::wstring&) = 0;
virtual ISink& operator<<(const char&) = 0;
virtual ISink& operator<<(const int&) = 0;
virtual ISink& operator<<(const unsigned int&) = 0;
virtual ISink& operator<<(const long&) = 0;
virtual ISink& operator<<(const double&) = 0;
virtual ISink& operator<<(const bool&) = 0;
};
//-----------------------------------------------------------------------------
// Simply a facade over stringer, writes directly to a std::string.
class StringSink : public ISink
{
//
Chordia::stringer* _sink;
public:
//
StringSink(Chordia::stringer* sink) : _sink(sink) {}
//
virtual ISink& operator<<(const char* arg) { (*_sink) << arg; return (*this); }
virtual ISink& operator<<(const std::string& arg) { (*_sink) << arg; return (*this); }
virtual ISink& operator<<(const std::wstring& arg) { (*_sink) << arg; return (*this); }
virtual ISink& operator<<(const char& arg) { (*_sink) << arg; return (*this); }
virtual ISink& operator<<(const int& arg) { (*_sink) << arg; return (*this); }
virtual ISink& operator<<(const long& arg) { (*_sink) << arg; return (*this); }
virtual ISink& operator<<(const unsigned int& arg) { (*_sink) << arg; return (*this); }
virtual ISink& operator<<(const double& arg) { (*_sink) << arg; return (*this); }
virtual ISink& operator<<(const bool& arg) { (*_sink) << arg; return (*this); }
};
//-------------------------------------------------------------------------
// for serializing (writing) JSON
class Writer : public Adapter
{
//---------------------------------------------------------------------
ISink* _sink;
//
static const char* Quote()
{
return "\"";
}
public:
//---------------------------------------------------------------------
//
Writer(ISink* sink) : _sink(sink) {}
//---------------------------------------------------------------------
//
virtual bool storing() { return true; }
//---------------------------------------------------------------------
// write a key/value pair with optional continuation
virtual void serialize(const std::string& key,std::string& value,bool more)
{
(*_sink) << "\"" << key << Quote() << ':' << Quote() << Chordia::escape(value) << Quote() << (more ? "," : "");
}
virtual void serialize(const std::string& key,std::wstring& value,bool more)
{
(*_sink) << Quote() << key << Quote() << ':' << Quote() << Chordia::escape(Chordia::w2n(value)) << Quote() << (more ? "," : "");
}
virtual void serialize(const std::string& key,int& value,bool more)
{
(*_sink) << Quote() << key << Quote() << ':' << value << (more ? "," : "");
}
virtual void serialize(const std::string& key,unsigned int& value,bool more)
{
(*_sink) << Quote() << key << Quote() << ':' << value << (more ? "," : "");
}
virtual void serialize(const std::string& key,long& value,bool more)
{
(*_sink) << Quote() << key << Quote() << ':' << value << (more ? "," : "");
}
virtual void serialize(const std::string& key,unsigned char& value,bool more)
{
(*_sink) << Quote() << key << Quote() << ':' << value << (more ? "," : "");
}
virtual void serialize(const std::string& key,double& value,bool more)
{
(*_sink) << Quote() << key << Quote() << ':' << value << (more ? "," : "");
}
virtual void serialize(const std::string& key,bool& value,bool more)
{
// literal true of false
(*_sink) << Quote() << key << Quote() << ':' << value << (more ? "," : "");
}
//---------------------------------------------------------------------
// write a literal
virtual void serialize(const std::string& value)
{
(*_sink) << Quote() << value << Quote() ;
}
//---------------------------------------------------------------------
// write a delimiter
virtual void serialize(TokenType type)
{
bool ok = true;
const char* delimiter = 0;
switch (type)
{
case T_OBJ_BEGIN:
delimiter = "{";
break;
case T_OBJ_END:
delimiter = "}";
break;
case T_ARRAY_BEGIN:
delimiter = "[";
break;
case T_ARRAY_END:
delimiter = "]";
break;
case T_COMMA:
delimiter = ",";
break;
case T_COLON:
delimiter = ":";
break;
default:
ok = false;
}
if (ok)
{
(*_sink) << delimiter;
}
}
//---------------------------------------------------------------------
// serialize a string value. needs to handle escape sequences
virtual void serialize(std::string& value)
{
(*_sink) << Quote() << Chordia::escape(value) << Quote() ;
}
//---------------------------------------------------------------------
// needs to convert to JSON \u encoding for
// any characters outside of UTF8
virtual void serialize(std::wstring& value)
{
(*_sink) << Quote() << Chordia::escape(Chordia::w2n(value)) << Quote() ;
}
//---------------------------------------------------------------------
virtual void serialize(int& value)
{
(*_sink) << value;
}
//---------------------------------------------------------------------
virtual void serialize(unsigned int& value)
{
(*_sink) << value;
}
//---------------------------------------------------------------------
virtual void serialize(long& value)
{
(*_sink) << value;
}
//---------------------------------------------------------------------
virtual void serialize(double& value)
{
(*_sink) << value;
}
//---------------------------------------------------------------------
virtual void serialize(bool& value)
{
(*_sink) << value;
}
};
//-----------------------------------------------------------------------------
// template the writer. usage is:
// std::string json = JSON::producer<JSONExample>::convert(source);
template <typename serializable_type, typename sink_type = StringSink>
class producer
{
public:
static std::string convert(serializable_type& source)
{
Chordia::stringer json;
// by default serialize to a string
sink_type ssi(&json);
// create a writer
JSON::Writer writer(&ssi);
// serialize the instance
source.serialize(writer);
// and get the JSON string from the sink
//
#ifdef _INTEGRATED
#ifdef _UNICODE
return Chordia::convert_w(json.c_str());
#else
return json.c_str();
#endif
#else
return json.c_str();
#endif
}
};
}
#endif