-
Notifications
You must be signed in to change notification settings - Fork 13
/
generator.d
524 lines (479 loc) · 15.9 KB
/
generator.d
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
/**
* Contains routines for converting JSON values to their string represencation.
*
* Synopsis:
* ---
* ...
* ---
*
* Copyright: Copyright 2012 - 2014, Sönke Ludwig.
* License: $(WEB www.boost.org/LICENSE_1_0.txt, Boost License 1.0).
* Authors: Sönke Ludwig
* Source: $(PHOBOSSRC std/data/json/generator.d)
*/
module stdx.data.json.generator;
@safe:
import stdx.data.json.lexer;
import stdx.data.json.parser;
import stdx.data.json.value;
import std.bigint;
import std.range;
/**
* Converts the given JSON document(s) to a formatted string representation.
*
* Pretty printed strings are indented multi-line strings suitable for human
* consumption.
*
* See_also: $(D writePrettyJSON), $(D toJSON)
*/
string toPrettyJSON(GeneratorOptions options = GeneratorOptions.init)(JSONValue value)
{
import std.array;
auto dst = appender!string();
value.writePrettyJSON!options(dst);
return dst.data;
}
/// ditto
string toPrettyJSON(GeneratorOptions options = GeneratorOptions.init, Input)(Input nodes)
if (isJSONParserNodeInputRange!Input)
{
import std.array;
auto dst = appender!string();
nodes.writePrettyJSON!options(dst);
return dst.data;
}
/**
* Converts the given JSON document(s) to a compact string representation.
*
* The input can be a $(D JSONValue), or an input range of either $(D JSONToken)
* or $(D JSONParserNode) elements.
*
* Params:
* value = A single JSON document
* nodes = A set of JSON documents encoded as single parser nodes. The nodes
* must be in valid document order, or the parser result will be undefined.
* tokens = List of JSON tokens to be converted to strings. The tokens may
* occur in any order and are simply appended in order to the final string.
* token = A single token to convert to a string
*
* Returns:
* Returns a JSON formatted string.
*
* See_also: $(D writeJSON), $(D toPrettyJSON)
*/
string toJSON(GeneratorOptions options = GeneratorOptions.init)(JSONValue value)
{
import std.array;
auto dst = appender!string();
value.writeJSON!options(dst);
return dst.data;
}
/// ditto
string toJSON(GeneratorOptions options = GeneratorOptions.init, Input)(Input nodes)
if (isJSONParserNodeInputRange!Input)
{
import std.array;
auto dst = appender!string();
nodes.writeJSON!options(dst);
return dst.data;
}
/// ditto
string toJSON(GeneratorOptions options = GeneratorOptions.init, Input)(Input tokens)
if (isJSONTokenInputRange!Input)
{
import std.array;
auto dst = appender!string();
tokens.writeJSON!options(dst);
return dst.data;
}
/// ditto
string toJSON(GeneratorOptions options = GeneratorOptions.init)(JSONToken token)
{
import std.array;
auto dst = appender!string();
token.writeJSON!options(dst);
return dst.data;
}
///
unittest
{
JSONValue value = true;
assert(value.toJSON() == "true");
}
///
unittest
{
auto a = toJSONValue(`{"a": [], "b": [1, {}]}`);
// write compact JSON
assert(a.toJSON() == `{"a":[],"b":[1,{}]}`, a.toJSON());
// pretty print:
// {
// "a": [],
// "b": [
// 1,
// {},
// ]
// }
assert(a.toPrettyJSON() == "{\n\t\"a\": [],\n\t\"b\": [\n\t\t1,\n\t\t{}\n\t]\n}");
}
unittest
{
auto nodes = parseJSONStream(`{"a": [], "b": [1, {}]}`);
assert(nodes.toJSON() == `{"a":[],"b":[1,{}]}`);
assert(nodes.toPrettyJSON() == "{\n\t\"a\": [],\n\t\"b\": [\n\t\t1,\n\t\t{}\n\t]\n}");
auto tokens = lexJSON(`{"a": [], "b": [1, {}, null, true, false]}`);
assert(tokens.toJSON() == `{"a":[],"b":[1,{},null,true,false]}`);
JSONToken tok;
tok.string = "Hello World";
assert(tok.toJSON() == `"Hello World"`);
}
/**
* Formats the given JSON document(s) as an indented multi-line string.
*
* This function produces output suitable for human consumption by properly
* indenting based on the nesting level.
*
* See_also: $(D toPrettyJSON), $(D writeJSON)
*/
void writePrettyJSON(GeneratorOptions options = GeneratorOptions.init, Output)(JSONValue value, ref Output output)
if (isOutputRange!(Output, char))
{
writeAsStringImpl!(true, options)(value, output);
}
/// ditto
void writePrettyJSON(GeneratorOptions options = GeneratorOptions.init, Output, Input)(Input nodes, ref Output output)
if (isOutputRange!(Output, char) && isJSONParserNodeInputRange!Input)
{
writeAsStringImpl!(true, options)(nodes, output);
}
/**
* Formats the given JSON document(s)/tokens as a compact string.
*
* See $(D toJSON) for more information.
*
* Params:
* output = The output range to take the result string in UTF-8 encoding.
* value = A single JSON document
* nodes = A set of JSON documents encoded as single parser nodes. The nodes
* must be in valid document order, or the parser result will be undefined.
* tokens = List of JSON tokens to be converted to strings. The tokens may
* occur in any order and are simply appended in order to the final string.
* token = A single token to convert to a string
*
* See_also: $(D toJSON), $(D writePrettyJSON)
*/
void writeJSON(GeneratorOptions options = GeneratorOptions.init, Output)(JSONValue value, ref Output output)
if (isOutputRange!(Output, char))
{
writeAsStringImpl!(false, options)(value, output);
}
/// ditto
void writeJSON(GeneratorOptions options = GeneratorOptions.init, Output, Input)(Input nodes, ref Output output)
if (isOutputRange!(Output, char) && isJSONParserNodeInputRange!Input)
{
writeAsStringImpl!(false, options)(nodes, output);
}
/// ditto
void writeJSON(GeneratorOptions options = GeneratorOptions.init, Output, Input)(Input tokens, ref Output output)
if (isOutputRange!(Output, char) && isJSONTokenInputRange!Input)
{
while (!tokens.empty)
{
tokens.front.writeJSON!options(output);
tokens.popFront();
}
}
/// ditto
void writeJSON(GeneratorOptions options = GeneratorOptions.init, Output)(in ref JSONToken token, ref Output output)
if (isOutputRange!(Output, char))
{
final switch (token.kind) with (JSONToken.Kind)
{
case none: assert(false);
case error: output.put("_error_"); break;
case null_: output.put("null"); break;
case boolean: output.put(token.boolean ? "true" : "false"); break;
case number: output.writeNumber!options(token.number); break;
case string: output.put('"'); output.escapeString!(options & GeneratorOptions.escapeUnicode)(token.string); output.put('"'); break;
case objectStart: output.put('{'); break;
case objectEnd: output.put('}'); break;
case arrayStart: output.put('['); break;
case arrayEnd: output.put(']'); break;
case colon: output.put(':'); break;
case comma: output.put(','); break;
}
}
/**
* Flags for configuring the JSON generator.
*
* These flags can be combined using a bitwise or operation.
*/
enum GeneratorOptions {
/// Default value - enable none of the supported options
init = 0,
/// Output special float values as 'NaN' or 'Infinity' instead of 'null'
specialFloatLiterals = 1<<1,
/// Output all non-ASCII characters as unicode escape sequences
escapeUnicode = 1<<2,
}
private void writeAsStringImpl(bool pretty_print = false, GeneratorOptions options = GeneratorOptions.init, Output)(JSONValue value, ref Output output, size_t nesting_level = 0)
if (isOutputRange!(Output, char))
{
void indent(size_t depth)
{
output.put('\n');
foreach (tab; 0 .. depth) output.put('\t');
}
if (value.peek!(typeof(null))) output.put("null");
else if (auto pv = value.peek!bool) output.put(*pv ? "true" : "false");
else if (auto pv = value.peek!double) output.writeNumber!options(*pv);
else if (auto pv = value.peek!long) output.writeNumber(*pv);
else if (auto pv = value.peek!BigInt) output.writeNumber(*pv);
else if (auto pv = value.peek!string) { output.put('"'); output.escapeString!(options & GeneratorOptions.escapeUnicode)(*pv); output.put('"'); }
else if (auto pv = value.peek!(JSONValue[string]))
{
output.put('{');
bool first = true;
foreach (string k, ref e; *pv)
{
if (!first) output.put(',');
else first = false;
static if (pretty_print) indent(nesting_level+1);
output.put('\"');
output.escapeString!(options & GeneratorOptions.escapeUnicode)(k);
output.put(pretty_print ? `": ` : `":`);
e.writeAsStringImpl!pretty_print(output, nesting_level+1);
}
static if (pretty_print)
{
if (!first) indent(nesting_level);
}
output.put('}');
}
else if (auto pv = value.peek!(JSONValue[]))
{
output.put('[');
foreach (i, ref e; *pv)
{
if (i > 0) output.put(',');
static if (pretty_print) indent(nesting_level+1);
e.writeAsStringImpl!pretty_print(output, nesting_level+1);
}
static if (pretty_print)
{
if (pv.length > 0) indent(nesting_level);
}
output.put(']');
}
else assert(false);
}
private void writeAsStringImpl(bool pretty_print = false, GeneratorOptions options = GeneratorOptions.init, Output, Input)(Input nodes, ref Output output)
if (isOutputRange!(Output, char) && isJSONParserNodeInputRange!Input)
{
size_t nesting = 0;
bool first = false;
bool is_object_field = false;
void indent(size_t depth)
{
output.put('\n');
foreach (tab; 0 .. depth) output.put('\t');
}
void preValue()
{
if (!is_object_field)
{
if (nesting > 0 && !first) output.put(',');
else first = false;
static if (pretty_print)
{
if (nesting > 0) indent(nesting);
}
}
else is_object_field = false;
}
while (!nodes.empty)
{
final switch (nodes.front.kind) with (JSONParserNode.Kind)
{
case none: assert(false);
case key:
if (nesting > 0 && !first) output.put(',');
else first = false;
is_object_field = true;
static if (pretty_print) indent(nesting);
output.put('"');
output.escapeString!(options & GeneratorOptions.escapeUnicode)(nodes.front.key);
output.put(pretty_print ? `": ` : `":`);
break;
case literal:
preValue();
nodes.front.literal.writeJSON!options(output);
break;
case objectStart:
preValue();
output.put('{');
nesting++;
first = true;
break;
case objectEnd:
nesting--;
static if (pretty_print)
{
if (!first) indent(nesting);
}
first = false;
output.put('}');
break;
case arrayStart:
preValue();
output.put('[');
nesting++;
first = true;
is_object_field = false;
break;
case arrayEnd:
nesting--;
static if (pretty_print)
{
if (!first) indent(nesting);
}
first = false;
output.put(']');
break;
}
nodes.popFront();
}
}
private void writeNumber(GeneratorOptions options, R)(ref R dst, JSONNumber num) @trusted
{
import std.format;
import std.math;
final switch (num.type)
{
case JSONNumber.Type.double_: dst.writeNumber!options(num.doubleValue); break;
case JSONNumber.Type.long_: dst.writeNumber(num.longValue); break;
case JSONNumber.Type.bigInt: dst.writeNumber(num.bigIntValue); break;
}
}
private void writeNumber(GeneratorOptions options, R)(ref R dst, double num) @trusted
{
import std.format;
import std.math;
static if (options & GeneratorOptions.specialFloatLiterals)
{
if (isNaN(num)) dst.put("NaN");
else if (num == +double.infinity) dst.put("Infinity");
else if (num == -double.infinity) dst.put("-Infinity");
else dst.formattedWrite("%.16g", num);
}
else
{
if (isNaN(num) || num == -double.infinity || num == double.infinity)
dst.put("null");
else dst.formattedWrite("%.16g", num);
}
}
private void writeNumber(R)(ref R dst, long num) @trusted
{
import std.format;
dst.formattedWrite("%d", num);
}
private void writeNumber(R)(ref R dst, BigInt num) @trusted
{
num.toString(str => dst.put(str), null);
}
unittest
{
import std.math;
import std.string;
auto num = toJSONValue("-67.199307");
auto exp = -67.199307;
assert(num.get!double.approxEqual(exp));
auto snum = appender!string;
snum.writeNumber!(GeneratorOptions.init)(JSONNumber(num.get!double));
auto pnum = toJSONValue(snum.data);
assert(pnum.get!double.approxEqual(num.get!double));
}
unittest // special float values
{
static void test(GeneratorOptions options = GeneratorOptions.init)(double val, string expected)
{
auto dst = appender!string;
dst.writeNumber!options(val);
assert(dst.data == expected);
}
test(double.nan, "null");
test(double.infinity, "null");
test(-double.infinity, "null");
test!(GeneratorOptions.specialFloatLiterals)(double.nan, "NaN");
test!(GeneratorOptions.specialFloatLiterals)(double.infinity, "Infinity");
test!(GeneratorOptions.specialFloatLiterals)(-double.infinity, "-Infinity");
}
private void escapeString(bool use_surrogates = false, R)(ref R dst, string s)
{
import std.format;
import std.utf : decode;
for (size_t pos = 0; pos < s.length; pos++)
{
immutable ch = s[pos];
switch (ch)
{
case '\\': dst.put(`\\`); break;
case '\b': dst.put(`\b`); break;
case '\f': dst.put(`\f`); break;
case '\r': dst.put(`\r`); break;
case '\n': dst.put(`\n`); break;
case '\t': dst.put(`\t`); break;
case '\"': dst.put(`\"`); break;
default:
static if (use_surrogates)
{
if (ch >= 0x20 && ch < 0x80)
{
dst.put(ch);
break;
}
dchar cp = decode(s, pos);
pos--; // account for the next loop increment
// encode as one or two UTF-16 code points
if (cp < 0x10000)
{ // in BMP -> 1 CP
formattedWrite(dst, "\\u%04X", cp);
}
else
{ // not in BMP -> surrogate pair
int first, last;
cp -= 0x10000;
first = 0xD800 | ((cp & 0xffc00) >> 10);
last = 0xDC00 | (cp & 0x003ff);
formattedWrite(dst, "\\u%04X\\u%04X", first, last);
}
}
else
{
if (ch < 0x20) formattedWrite(dst, "\\u%04X", ch);
else dst.put(ch);
}
break;
}
}
}
unittest
{
static void test(bool surrog)(string str, string expected)
{
auto res = appender!string;
res.escapeString!surrog(str);
assert(res.data == expected, res.data);
}
test!false("hello", "hello");
test!false("hällo", "hällo");
test!false("a\U00010000b", "a\U00010000b");
test!false("a\u1234b", "a\u1234b");
test!false("\r\n\b\f\t\\\"", `\r\n\b\f\t\\\"`);
test!true("hello", "hello");
test!true("hällo", `h\u00E4llo`);
test!true("a\U00010000b", `a\uD800\uDC00b`);
test!true("a\u1234b", `a\u1234b`);
test!true("\r\n\b\f\t\\\"", `\r\n\b\f\t\\\"`);
}