-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
encoder.v
578 lines (513 loc) · 15.9 KB
/
encoder.v
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
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
// Copyright (c) 2019-2024 Alexander Medvednikov. All rights reserved.
// Use of this source code is governed by an MIT license
// that can be found in the LICENSE file.
module json2
import time
// Encoder encodes the an `Any` type into JSON representation.
// It provides parameters in order to change the end result.
pub struct Encoder {
pub:
newline u8
newline_spaces_count int
escape_unicode bool = true
}
// byte array versions of the most common tokens/chars to avoid reallocations
const null_in_bytes = 'null'
const true_in_string = 'true'
const false_in_string = 'false'
const empty_array = [u8(`[`), `]`]!
const comma_rune = `,`
const colon_rune = `:`
const quote_rune = `"`
const back_slash = [u8(`\\`), `\\`]!
const quote = [u8(`\\`), `"`]!
const slash = [u8(`\\`), `/`]!
const null_unicode = [u8(`\\`), `u`, `0`, `0`, `0`, `0`]!
const ascii_control_characters = ['\\u0000', '\\t', '\\n', '\\r', '\\u0004', '\\u0005', '\\u0006',
'\\u0007', '\\b', '\\t', '\\n', '\\u000b', '\\f', '\\r', '\\u000e', '\\u000f', '\\u0010',
'\\u0011', '\\u0012', '\\u0013', '\\u0014', '\\u0015', '\\u0016', '\\u0017', '\\u0018', '\\u0019',
'\\u001a', '\\u001b', '\\u001c', '\\u001d', '\\u001e', '\\u001f']!
const curly_open_rune = `{`
const curly_close_rune = `}`
const ascii_especial_characters = [u8(`\\`), `"`, `/`]!
// encode is a generic function that encodes a type into a JSON string.
@[manualfree]
pub fn encode[T](val T) string {
$if T is $array {
return encode_array(val)
} $else {
mut count := Count{0}
count.count_chars(val)
mut buf := []u8{cap: count.total}
defer {
unsafe { buf.free() }
}
encoder := Encoder{}
encoder.encode_value(val, mut buf) or {
println(err)
encoder.encode_value[string]('null', mut buf) or {}
}
return buf.bytestr()
}
}
// encode_array is a generic function that encodes a array into a JSON string.
@[manualfree]
fn encode_array[T](val []T) string {
if val.len == 0 {
return '[]'
}
mut buf := []u8{}
defer {
unsafe { buf.free() }
}
encoder := Encoder{}
encoder.encode_array(val, 1, mut buf) or {
println(err)
encoder.encode_value[string]('null', mut buf) or {}
}
return buf.bytestr()
}
// encode_pretty ...
pub fn encode_pretty[T](typed_data T) string {
encoded := encode(typed_data)
raw_decoded := raw_decode(encoded) or { 0 }
return raw_decoded.prettify_json_str()
}
// encode_value encodes a value to the specific buffer.
pub fn (e &Encoder) encode_value[T](val T, mut buf []u8) ! {
e.encode_value_with_level[T](val, 1, mut buf)!
}
fn (e &Encoder) encode_newline(level int, mut buf []u8) ! {
if e.newline != 0 {
buf << e.newline
for j := 0; j < level * e.newline_spaces_count; j++ {
buf << ` `
}
}
}
fn (e &Encoder) encode_map[T](value T, level int, mut buf []u8) ! {
buf << json2.curly_open_rune
mut idx := 0
for k, v in value {
e.encode_newline(level, mut buf)!
// e.encode_string(k.str(), mut buf)!
e.encode_string(k, mut buf)!
buf << json2.colon_rune
if e.newline != 0 {
buf << ` `
}
// workaround to avoid `cannot convert 'struct x__json2__Any' to 'struct string'`
$if v is $sumtype {
$for variant_value in v.variants {
if v is variant_value {
e.encode_value_with_level(v, level + 1, mut buf)!
}
}
} $else {
e.encode_value_with_level(v, level + 1, mut buf)!
}
if idx < value.len - 1 {
buf << json2.comma_rune
}
idx++
}
// e.encode_newline(level, mut buf)!
e.encode_newline(level - 1, mut buf)!
buf << json2.curly_close_rune
}
fn (e &Encoder) encode_value_with_level[T](val T, level int, mut buf []u8) ! {
$if val is $option {
workaround := val
if workaround != none {
e.encode_value_with_level(val, level, mut buf)!
}
} $else $if T is string {
e.encode_string(val, mut buf)!
} $else $if T is $sumtype {
$for v in val.variants {
if val is v {
e.encode_value_with_level(val, level, mut buf)!
}
}
} $else $if T is $alias {
// TODO
} $else $if T is time.Time {
str_value := val.format_rfc3339()
buf << json2.quote_rune
unsafe { buf.push_many(str_value.str, str_value.len) }
buf << json2.quote_rune
} $else $if T is $map {
e.encode_map(val, level, mut buf)!
} $else $if T is $array {
e.encode_array(val, level, mut buf)!
} $else $if T is Encodable {
str_value := val.json_str()
unsafe { buf.push_many(str_value.str, str_value.len) }
} $else $if T is Null {
unsafe { buf.push_many(json2.null_in_bytes.str, json2.null_in_bytes.len) }
} $else $if T is $struct {
e.encode_struct(val, level, mut buf)!
} $else $if T is $enum {
str_int := int(val).str()
unsafe { buf.push_many(str_int.str, str_int.len) }
} $else $if T is $int || T is $float || T is bool {
str_int := val.str()
unsafe { buf.push_many(str_int.str, str_int.len) }
} $else {
return error('cannot encode value with ${typeof(val).name} type')
}
}
fn (e &Encoder) encode_struct[U](val U, level int, mut buf []u8) ! {
buf << json2.curly_open_rune
mut i := 0
mut fields_len := 0
$for field in U.fields {
mut @continue := false
for attr in field.attrs {
if attr.contains('json: ') {
if attr.replace('json: ', '') == '-' {
@continue = true
}
break
}
}
if !@continue {
$if field.is_option {
if val.$(field.name) != none {
fields_len++
}
} $else {
fields_len++
}
}
}
$for field in U.fields {
mut ignore_field := false
value := val.$(field.name)
is_nil := val.$(field.name).str() == '&nil'
mut json_name := ''
for attr in field.attrs {
if attr.contains('json: ') {
json_name = attr.replace('json: ', '')
if json_name == '-' {
ignore_field = true
}
break
}
}
if !ignore_field {
$if value is $option {
workaround := val.$(field.name)
if workaround != none { // smartcast
e.encode_newline(level, mut buf)!
if json_name != '' {
e.encode_string(json_name, mut buf)!
} else {
e.encode_string(field.name, mut buf)!
}
buf << json2.colon_rune
if e.newline != 0 {
buf << ` `
}
e.encode_value_with_level(value, level, mut buf)!
} else {
ignore_field = true
}
} $else {
is_none := val.$(field.name).str() == 'unknown sum type value' // assert json.encode(StructType[SumTypes]{}) == '{}'
if !is_none && !is_nil {
e.encode_newline(level, mut buf)!
if json_name != '' {
e.encode_string(json_name, mut buf)!
} else {
e.encode_string(field.name, mut buf)!
}
buf << json2.colon_rune
if e.newline != 0 {
buf << ` `
}
}
$if field.indirections != 0 {
if val.$(field.name) != unsafe { nil } {
$if field.indirections == 1 {
e.encode_value_with_level(*val.$(field.name), level + 1, mut
buf)!
}
$if field.indirections == 2 {
e.encode_value_with_level(**val.$(field.name), level + 1, mut
buf)!
}
$if field.indirections == 3 {
e.encode_value_with_level(***val.$(field.name), level + 1, mut
buf)!
}
}
} $else $if field.typ is string {
e.encode_string(val.$(field.name).str(), mut buf)!
} $else $if field.typ is time.Time {
str_value := val.$(field.name).format_rfc3339()
buf << json2.quote_rune
unsafe { buf.push_many(str_value.str, str_value.len) }
buf << json2.quote_rune
} $else $if field.typ is bool {
if value {
unsafe { buf.push_many(json2.true_in_string.str, json2.true_in_string.len) }
} else {
unsafe { buf.push_many(json2.false_in_string.str, json2.false_in_string.len) }
}
} $else $if field.typ in [$float, $int] {
str_value := val.$(field.name).str()
unsafe { buf.push_many(str_value.str, str_value.len) }
} $else $if field.is_array {
// TODO: replace for `field.typ is $array`
e.encode_array(value, level + 1, mut buf)!
} $else $if field.typ is $array {
// e.encode_array(value, level + 1, mut buf)! // FIXME: error: could not infer generic type `U` in call to `encode_array`
} $else $if field.typ is $struct {
e.encode_struct(value, level + 1, mut buf)!
} $else $if field.is_map {
e.encode_map(value, level + 1, mut buf)!
} $else $if field.is_enum {
// TODO: replace for `field.typ is $enum`
// str_value := int(val.$(field.name)).str()
// unsafe { buf.push_many(str_value.str, str_value.len) }
e.encode_value_with_level(val.$(field.name), level + 1, mut buf)!
} $else $if field.typ is $enum {
} $else $if field.typ is $sumtype {
field_value := val.$(field.name)
if field_value.str() != 'unknown sum type value' {
$for v in field_value.variants {
if field_value is v {
e.encode_value_with_level(field_value, level, mut buf)!
}
}
}
} $else $if field.typ is $alias {
$if field.unaliased_typ is string {
e.encode_string(val.$(field.name).str(), mut buf)!
} $else $if field.unaliased_typ is time.Time {
parsed_time := time.parse(val.$(field.name).str()) or { time.Time{} }
e.encode_string(parsed_time.format_rfc3339(), mut buf)!
} $else $if field.unaliased_typ is bool {
if val.$(field.name) {
unsafe { buf.push_many(json2.true_in_string.str, json2.true_in_string.len) }
} else {
unsafe { buf.push_many(json2.false_in_string.str, json2.false_in_string.len) }
}
} $else $if field.unaliased_typ in [$float, $int] {
str_value := val.$(field.name).str()
unsafe { buf.push_many(str_value.str, str_value.len) }
} $else $if field.unaliased_typ is $array {
// TODO
} $else $if field.unaliased_typ is $struct {
e.encode_struct(value, level + 1, mut buf)!
} $else $if field.unaliased_typ is $enum {
// TODO
} $else $if field.unaliased_typ is $sumtype {
// TODO
} $else {
return error('the alias ${typeof(val).name} cannot be encoded')
}
} $else {
return error('type ${typeof(val).name} cannot be array encoded')
}
}
}
if i < fields_len - 1 && !ignore_field {
if !is_nil {
buf << json2.comma_rune
}
}
if !ignore_field {
i++
}
}
e.encode_newline(level - 1, mut buf)!
buf << json2.curly_close_rune
// b.measure('encode_struct')
}
fn (e &Encoder) encode_array[U](val []U, level int, mut buf []u8) ! {
if val.len == 0 {
unsafe { buf.push_many(&json2.empty_array[0], json2.empty_array.len) }
return
}
buf << `[`
for i in 0 .. val.len {
e.encode_newline(level, mut buf)!
$if U is string || U is bool || U is $int || U is $float {
e.encode_value_with_level(val[i], level + 1, mut buf)!
} $else $if U is $array {
e.encode_array(val[i], level + 1, mut buf)!
} $else $if U is $struct {
e.encode_struct(val[i], level + 1, mut buf)!
} $else $if U is $sumtype {
e.encode_value_with_level(val[i], level + 1, mut buf)!
} $else $if U is $enum {
// TODO: test
e.encode_value_with_level(val[i], level + 1, mut buf)!
} $else {
return error('type ${typeof(val).name} cannot be array encoded')
}
if i < val.len - 1 {
buf << json2.comma_rune
}
}
e.encode_newline(level - 1, mut buf)!
buf << `]`
}
// str returns the JSON string representation of the `map[string]Any` type.
pub fn (f map[string]Any) str() string {
return Any(f).json_str()
}
// str returns the JSON string representation of the `[]Any` type.
pub fn (f []Any) str() string {
return Any(f).json_str()
}
// str returns the string representation of the `Any` type. Use the `json_str` method
// if you want to use the escaped str() version of the `Any` type.
pub fn (f Any) str() string {
if f is string {
return f
} else {
return f.json_str()
}
}
// json_str returns the JSON string representation of the `Any` type.
pub fn (f Any) json_str() string {
return encode(f)
}
// prettify_json_str returns the pretty-formatted JSON string representation of the `Any` type.
@[manualfree]
pub fn (f Any) prettify_json_str() string {
mut buf := []u8{}
defer {
unsafe { buf.free() }
}
mut enc := Encoder{
newline: `\n`
newline_spaces_count: 2
}
enc.encode_value(f, mut buf) or {}
return buf.bytestr()
}
// TODO: Need refactor. Is so slow. The longer the string, the lower the performance.
// encode_string returns the JSON spec-compliant version of the string.
@[direct_array_access]
fn (e &Encoder) encode_string(s string, mut buf []u8) ! {
if s == '' {
empty := [u8(json2.quote_rune), json2.quote_rune]!
unsafe { buf.push_many(&empty[0], 2) }
return
}
mut last_no_buffer_expansible_char_position_candidate := 0
buf << json2.quote_rune
if !e.escape_unicode {
unsafe {
buf.push_many(s.str, s.len)
buf << json2.quote_rune
}
return
}
for idx := 0; idx < s.len; idx++ {
current_byte := s[idx]
mut current_utf8_len := ((0xe5000000 >> ((current_byte >> 3) & 0x1e)) & 3) + 1
current_value_cause_buffer_expansion :=
(current_utf8_len == 1 && ((current_byte < 32 || current_byte > 127)
|| current_byte in json2.ascii_especial_characters))
|| current_utf8_len == 3
if !current_value_cause_buffer_expansion {
// while it is not the last one
if idx < s.len - 1 {
if s.len > (idx + current_utf8_len) {
if current_utf8_len == 2 || current_utf8_len == 4 {
// runes like: ã, ü, etc.
// Emojis ranges
// (0x1F300, 0x1F5FF), # Miscellaneous Symbols and Pictographs
// (0x1F600, 0x1F64F), # Emoticons
// (0x1F680, 0x1F6FF), # Transport and Map Symbols
idx += current_utf8_len - 1
continue
}
} else {
unsafe {
buf.push_many(s.str + last_no_buffer_expansible_char_position_candidate,
s.len - last_no_buffer_expansible_char_position_candidate)
}
break
}
} else if idx == s.len - 1 {
unsafe {
buf.push_many(s.str + last_no_buffer_expansible_char_position_candidate,
s.len - last_no_buffer_expansible_char_position_candidate)
}
}
} else {
if idx > 0 {
length := idx - last_no_buffer_expansible_char_position_candidate
unsafe {
buf.push_many(s.str + last_no_buffer_expansible_char_position_candidate,
length)
}
last_no_buffer_expansible_char_position_candidate = idx + 1
}
}
if current_utf8_len == 1 {
if current_byte < 32 {
// ASCII Control Characters
unsafe {
buf.push_many(json2.ascii_control_characters[current_byte].str, json2.ascii_control_characters[current_byte].len)
}
last_no_buffer_expansible_char_position_candidate = idx + 1
} else if current_byte >= 32 && current_byte < 128 {
// ASCII especial characters
if current_byte == `\\` {
unsafe { buf.push_many(&json2.back_slash[0], json2.back_slash.len) }
last_no_buffer_expansible_char_position_candidate = idx + 1
continue
} else if current_byte == `"` {
unsafe { buf.push_many(&json2.quote[0], json2.quote.len) }
last_no_buffer_expansible_char_position_candidate = idx + 1
continue
} else if current_byte == `/` {
unsafe { buf.push_many(&json2.slash[0], json2.slash.len) }
last_no_buffer_expansible_char_position_candidate = idx + 1
continue
}
}
continue
} else if current_utf8_len == 3 {
// runes like: ✔, ひらがな ...
// Handle multi-byte characters byte-by-byte
mut codepoint := u32(current_byte & ((1 << (7 - current_utf8_len)) - 1))
for j in 1 .. current_utf8_len {
if idx + j >= s.len {
// Incomplete UTF-8 sequence, TODO handle error
idx++
continue
}
mut b := s[idx + j]
if (b & 0xC0) != 0x80 {
// Invalid continuation byte, TODO handle error
idx++
continue
}
codepoint = u32((codepoint << 6) | (b & 0x3F))
}
// runes like: ✔, ひらがな ...
unsafe { buf.push_many(&json2.null_unicode[0], json2.null_unicode.len) }
buf[buf.len - 1] = hex_digit(codepoint & 0xF)
buf[buf.len - 2] = hex_digit((codepoint >> 4) & 0xF)
buf[buf.len - 3] = hex_digit((codepoint >> 8) & 0xF)
buf[buf.len - 4] = hex_digit((codepoint >> 12) & 0xF)
idx += current_utf8_len - 1
last_no_buffer_expansible_char_position_candidate = idx + 1
}
}
buf << json2.quote_rune
}
fn hex_digit(n int) u8 {
if n < 10 {
return `0` + n
}
return `a` + (n - 10)
}