-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjsonfmt.go
More file actions
740 lines (611 loc) · 15.2 KB
/
jsonfmt.go
File metadata and controls
740 lines (611 loc) · 15.2 KB
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
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
/*
Flexible JSON formatter. Features:
- Preserves order.
- Fits dicts and lists on a single line until a certain width (configurable).
- Supports comments (configurable).
- Supports trailing commas (configurable).
- Fixes missing or broken punctuation.
- Tiny Go library + optional tiny CLI.
Current limitations:
- Always permissive. Unrecognized non-whitespace is treated as arbitrary
content on par with strings, numbers, etc.
- Slower than `json.Indent` from the Go standard library.
- Input must be UTF-8.
Source and readme: https://github.com/mitranim/jsonfmt.
*/
package jsonfmt
import (
"bytes"
"encoding/json"
"strings"
"unicode/utf8"
"unsafe"
)
/*
Default configuration. To override, make a copy:
conf := jsonfmt.Default
conf.CommentLine = `#`
content = jsonfmt.FormatBytes(conf, content)
See `Conf` for details.
*/
var Default = Conf{
Indent: ` `,
Width: 80,
CommentLine: `//`,
CommentBlockStart: `/*`,
CommentBlockEnd: `*/`,
TrailingComma: false,
StripComments: false,
}
/*
Configuration passed to `Format`. See the variable `Default`.
`.Indent` enables multi-line output. When empty, `jsonfmt` will not emit
separator spaces or newlines, except at the end of single-line comments.
When non-empty, `jsonfmt` will emit separator spaces, newlines, and indents
for contents of lists and dicts. To enforce single-line output, use
`.Indent = ""` and `.StripComments = true`.
`.Width` is the width limit for lists and dicts. If 0, then depending on other
configuration, `jsonfmt` will format lists and dicts either always in
multi-line mode, or always in single-line mode. If > 0, then `jsonfmt` will
attempt to format each list or dict entirely on a single line until the width
limit, falling back on multi-line mode when exceeding the width limit. Note
that multi-line mode also requires non-empty `.Indent`.
`.CommentLine` starts a single-line comment. If empty, single-line comments
won't be detected, and will be treated as arbitrary JSON content.
`.CommentBlockStart` and `.CommentBlockEnd` enable support for block comments.
If both are non-empty, block comments are detected. Nested block comments are
supported. If at least one is empty, then the other will be ignored, block
comments will not be detected, and will be treated as arbitrary JSON content.
`.TrailingComma` enables trailing commas for last elements in dicts and lists in
multi-line mode. In single-line mode, trailing commas are always omitted.
`.StripComments` omits all comments from the output. To enforce single-line
output, specify this together with `.Indent = ""`. When single-line comments
are not omitted from the output, they cause the output to contain newlines,
because each single-line comment must be followed by a newline.
*/
type Conf struct {
Indent string `json:"indent"`
Width uint64 `json:"width"`
CommentLine string `json:"commentLine"`
CommentBlockStart string `json:"commentBlockStart"`
CommentBlockEnd string `json:"commentBlockEnd"`
TrailingComma bool `json:"trailingComma"`
StripComments bool `json:"stripComments"`
}
const (
separator = ' '
newline = '\n'
)
// Describes various interchangeable text types.
type Text interface{ ~string | ~[]byte }
// Formats JSON according to the config. See `Conf`.
func Format[Out, Src Text](conf Conf, src Src) Out {
fmter := fmter{source: text[string](src), conf: conf}
fmter.top()
return text[Out](fmter.buf.Bytes())
}
// Formats JSON text according to config, returning a string.
func FormatString[Src Text](conf Conf, src Src) string {
return Format[string](conf, src)
}
// Formats JSON text according to config, returning bytes.
func FormatBytes[Src Text](conf Conf, src Src) []byte {
return Format[[]byte](conf, src)
}
/*
Shortcut that combines formatting with `json.Unmarshal`. Allows to decode JSON
with comments or invalid punctuation, such as trailing commas. Slower than
simply using `json.Unmarshal`. Avoid this when your input is guaranteed to be
valid JSON, or when you should be enforcing valid JSON.
*/
func Unmarshal[Src Text](src Src, out any) error {
return json.Unmarshal(Format[[]byte](Conf{}, src), out)
}
type fmter struct {
source string
cursor int
conf Conf
buf bytes.Buffer
indent int
row int
col int
discard bool
snapshot *fmter
}
func (self *fmter) top() {
for self.more() {
if self.skipped() {
continue
}
if self.isNextComment() {
assert(self.scannedAny())
self.writeMaybeCommentNewline()
continue
}
if self.scannedAny() {
self.writeMaybeNewline()
continue
}
self.skipChar()
}
}
func (self *fmter) any() {
if self.isNextByte('{') {
self.dict()
} else if self.isNextByte('[') {
self.list()
} else if self.isNextByte('"') {
self.string()
} else if self.isNextCommentLine() {
self.commentSingle()
} else if self.isNextCommentBlock() {
self.commentMulti()
} else {
self.atom()
}
}
func (self *fmter) scannedAny() bool {
return self.scanned((*fmter).any)
}
func (self *fmter) dict() {
if !self.preferSingle() || !self.scanned((*fmter).dictSingle) {
self.dictMulti()
}
}
func (self *fmter) dictSingle() {
prev := self.snap()
defer self.maybeRollback(prev)
assert(self.isNextByte('{'))
self.byte()
key := true
for self.more() {
if self.isNextByte('}') {
self.byte()
return
}
if self.skipped() {
continue
}
if self.isNextComment() {
assert(self.scannedAny())
continue
}
if key {
assert(self.scannedAny())
self.writeByte(':')
self.writeMaybeSeparator()
key = false
continue
}
assert(self.scannedAny())
if self.hasNonCommentsBefore('}') {
self.writeByte(',')
self.writeMaybeSeparator()
}
key = true
}
}
func (self *fmter) dictMulti() {
assert(self.isNextByte('{'))
self.indent++
self.byte()
self.writeMaybeNewline()
key := true
for self.more() {
if self.isNextByte('}') {
self.indent--
self.writeMaybeNewlineIndent()
self.byte()
return
}
if self.skipped() {
continue
}
if self.isNextComment() {
self.writeMaybeCommentNewlineIndent()
assert(self.scannedAny())
continue
}
if key {
self.writeMaybeNewlineIndent()
assert(self.scannedAny())
self.writeByte(':')
self.writeMaybeSeparator()
key = false
continue
}
assert(self.scannedAny())
if self.hasNonCommentsBefore('}') {
self.writeByte(',')
} else {
self.writeMaybeTrailingComma()
}
key = true
}
}
func (self *fmter) list() {
if !self.preferSingle() || !self.scanned((*fmter).listSingle) {
self.listMulti()
}
}
func (self *fmter) listSingle() {
prev := self.snap()
defer self.maybeRollback(prev)
assert(self.isNextByte('['))
self.byte()
for self.more() {
if self.isNextByte(']') {
self.byte()
return
}
if self.skipped() {
continue
}
if self.isNextComment() {
assert(self.scannedAny())
continue
}
assert(self.scannedAny())
if self.hasNonCommentsBefore(']') {
self.writeByte(',')
self.writeMaybeSeparator()
}
}
}
func (self *fmter) listMulti() {
assert(self.isNextByte('['))
self.indent++
self.byte()
self.writeMaybeNewline()
for self.more() {
if self.isNextByte(']') {
self.indent--
self.writeMaybeNewlineIndent()
self.byte()
return
}
if self.skipped() {
continue
}
if self.isNextComment() {
self.writeMaybeCommentNewlineIndent()
assert(self.scannedAny())
continue
}
self.writeMaybeNewlineIndent()
assert(self.scannedAny())
if self.hasNonCommentsBefore(']') {
self.writeByte(',')
} else {
self.writeMaybeTrailingComma()
}
}
}
func (self *fmter) string() {
assert(self.isNextByte('"'))
self.byte()
for self.more() {
if self.isNextByte('"') {
self.byte()
return
}
if self.isNextByte('\\') {
self.byte()
if self.more() {
self.char()
}
continue
}
self.char()
}
}
func (self *fmter) commentSingle() {
prefix := self.nextCommentLinePrefix()
assert(prefix != ``)
if self.conf.StripComments {
self.setDiscard(true)
defer self.setDiscard(false)
}
self.strInc(prefix)
for self.more() {
if self.isNextPrefix("\r\n") {
self.skipString("\r\n")
self.writeNewline()
return
}
if self.isNextByte('\n') || self.isNextByte('\r') {
self.skipByte()
self.writeNewline()
return
}
self.char()
}
}
func (self *fmter) commentMulti() {
prefix, suffix := self.nextCommentBlockPrefixSuffix()
assert(prefix != `` && suffix != ``)
if self.conf.StripComments {
self.setDiscard(true)
defer self.setDiscard(false)
}
self.strInc(prefix)
level := 1
for self.more() {
if self.isNextPrefix(suffix) {
self.strInc(suffix)
level--
if level == 0 {
return
}
continue
}
if self.isNextPrefix(prefix) {
self.strInc(prefix)
level++
continue
}
self.char()
}
}
func (self *fmter) atom() {
for self.more() && !self.isNextSpace() && !self.isNextTerminal() {
self.char()
}
}
func (self *fmter) char() {
char, size := utf8.DecodeRuneInString(self.rest())
assert(size > 0)
self.writeRune(char)
self.cursor += size
}
func (self *fmter) byte() {
self.writeByte(self.source[self.cursor])
self.cursor++
}
// Performance seems fine, probably because `bytes.Buffer` short-circuits ASCII runes.
func (self *fmter) writeByte(char byte) {
self.writeRune(rune(char))
}
// ALL writes must call this function.
func (self *fmter) writeRune(char rune) {
if self.discard {
return
}
if char == '\n' || char == '\r' {
self.row++
self.col = 0
} else {
self.col++
}
self.buf.WriteRune(char)
if self.snapshot != nil && self.exceedsLine(self.snapshot) {
panic(rollback)
}
}
func (self *fmter) writeString(str string) {
for _, char := range str {
self.writeRune(char)
}
}
func (self *fmter) writeMaybeSeparator() {
if self.whitespace() {
self.writeByte(separator)
}
}
func (self *fmter) writeMaybeTrailingComma() {
if self.conf.TrailingComma {
self.writeByte(',')
}
}
func (self *fmter) writeMaybeNewline() {
if self.whitespace() && !self.hasNewlineSuffix() {
self.writeByte(newline)
}
}
func (self *fmter) writeNewline() {
if !self.wrote((*fmter).writeMaybeNewline) {
self.writeByte(newline)
}
}
func (self *fmter) writeIndent() {
for ind := 0; ind < self.indent; ind++ {
self.writeString(self.conf.Indent)
}
}
func (self *fmter) writeMaybeNewlineIndent() {
if self.whitespace() {
self.writeMaybeNewline()
self.writeIndent()
}
}
func (self *fmter) writeMaybeCommentNewlineIndent() {
if !self.conf.StripComments {
self.writeMaybeNewlineIndent()
}
}
func (self *fmter) writeMaybeCommentNewline() {
if !self.conf.StripComments {
self.writeMaybeNewline()
}
}
func (self *fmter) nextCommentLinePrefix() string {
prefix := self.conf.CommentLine
if prefix != `` && strings.HasPrefix(self.rest(), prefix) {
return prefix
}
return ``
}
func (self *fmter) nextCommentBlockPrefixSuffix() (string, string) {
prefix := self.conf.CommentBlockStart
suffix := self.conf.CommentBlockEnd
if prefix != `` && suffix != `` && strings.HasPrefix(self.rest(), prefix) {
return prefix, suffix
}
return ``, ``
}
func (self *fmter) hasNonCommentsBefore(char byte) bool {
prev := *self
defer self.reset(&prev)
for self.more() {
if self.isNextByte(char) {
return false
}
if self.skipped() {
continue
}
if self.isNextComment() {
assert(self.scannedAny())
continue
}
return true
}
return false
}
func (self *fmter) reset(prev *fmter) {
self.cursor = prev.cursor
self.indent = prev.indent
self.row = prev.row
self.col = prev.col
self.buf.Truncate(prev.buf.Len())
}
// Causes an escape and a minor heap allocation, but this isn't our bottleneck.
// Ensuring stack allocation in this particular case seems to have no effect on
// performance.
func (self *fmter) snap() *fmter {
prev := self.snapshot
snapshot := *self
self.snapshot = &snapshot
return prev
}
var rollback = new(struct{})
func (self *fmter) maybeRollback(prev *fmter) {
snapshot := self.snapshot
self.snapshot = prev
val := recover()
if val == rollback {
self.reset(snapshot)
} else if val != nil {
panic(val)
}
}
// Used for `defer`.
func (self *fmter) setDiscard(val bool) {
self.discard = val
}
func (self *fmter) more() bool {
return self.left() > 0
}
func (self *fmter) left() int {
return len(self.source) - self.cursor
}
func (self *fmter) headByte() byte {
if self.cursor < len(self.source) {
return self.source[self.cursor]
}
return 0
}
func (self *fmter) rest() string {
if self.more() {
return self.source[self.cursor:]
}
return ``
}
func (self *fmter) isNextPrefix(prefix string) bool {
return strings.HasPrefix(self.rest(), prefix)
}
func (self *fmter) isNextByte(char byte) bool {
return self.headByte() == char
}
func (self *fmter) isNextSpace() bool {
return self.isNextByte(' ') || self.isNextByte('\t') || self.isNextByte('\v') ||
self.isNextByte('\n') || self.isNextByte('\r')
}
/*
We skip punctuation and insert it ourselves where appropriate. This allows us to
automatically fix missing or broken punctuation. The user can write lists or
dicts without punctuation, and we'll insert it. In JSON, this is completely
unambiguous.
Skipping `:` in lists also assists in the edge case of converting between lists
and dicts.
*/
func (self *fmter) isNextPunctuation() bool {
return self.isNextByte(',') || self.isNextByte(':')
}
func (self *fmter) isNextCommentLine() bool {
return self.nextCommentLinePrefix() != ``
}
func (self *fmter) isNextCommentBlock() bool {
prefix, suffix := self.nextCommentBlockPrefixSuffix()
return prefix != `` && suffix != ``
}
func (self *fmter) isNextTerminal() bool {
return self.isNextByte('{') ||
self.isNextByte('}') ||
self.isNextByte('[') ||
self.isNextByte(']') ||
self.isNextByte(',') ||
self.isNextByte(':') ||
self.isNextByte('"') ||
self.isNextComment()
}
func (self *fmter) isNextComment() bool {
return self.isNextCommentLine() || self.isNextCommentBlock()
}
var (
bytesLf = []byte("\n")
bytesCr = []byte("\r")
)
func (self *fmter) hasNewlineSuffix() bool {
content := self.buf.Bytes()
return bytes.HasSuffix(content, bytesLf) || bytes.HasSuffix(content, bytesCr)
}
func (self *fmter) exceedsLine(prev *fmter) bool {
return self.row > prev.row || self.conf.Width > 0 && self.col > int(self.conf.Width)
}
func (self *fmter) skipByte() {
self.cursor++
}
func (self *fmter) skipChar() {
_, size := utf8.DecodeRuneInString(self.rest())
self.cursor += size
}
func (self *fmter) skipString(str string) {
self.skipNBytes(len(str))
}
func (self *fmter) skipNBytes(n int) {
self.cursor += n
}
func (self *fmter) strInc(str string) {
self.writeString(str)
self.skipString(str)
}
func (self *fmter) scanned(fun func(*fmter)) bool {
start := self.cursor
fun(self)
return self.cursor > start
}
func (self *fmter) wrote(fun func(*fmter)) bool {
start := self.buf.Len()
fun(self)
return self.buf.Len() > start
}
func (self *fmter) skipped() bool {
if self.isNextSpace() || self.isNextPunctuation() {
self.skipByte()
return true
}
return false
}
func (self *fmter) preferSingle() bool {
return self.conf.Width > 0
}
func (self *fmter) whitespace() bool {
return self.conf.Indent != ``
}
// Allocation-free conversion between two text types.
func text[Out, Src Text](src Src) Out { return *(*Out)(unsafe.Pointer(&src)) }
func assert(ok bool) {
if !ok {
panic(`[jsonfmt] internal error: failed a condition that should never be failed, see the stacktrace`)
}
}