-
Notifications
You must be signed in to change notification settings - Fork 26
/
jsonpp.go
151 lines (129 loc) · 2.88 KB
/
jsonpp.go
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
package main
import (
"bufio"
"bytes"
"encoding/json"
"flag"
"fmt"
"io"
"io/ioutil"
"os"
)
var (
single = flag.Bool("s", false, "parse already formatted JSON by interpretinting each input stream as a single JSON object")
help = flag.Bool("help", false, "help")
)
var newline = []uint8("\n")
func main() {
flag.Parse()
if *help {
cmd := os.Args[0]
if cmd[0:2] == "./" {
cmd = cmd[2:]
}
fmt.Fprintf(os.Stderr, "Usage: "+cmd+" [file]"+"\n")
fmt.Fprintf(os.Stderr, " or: $COMMAND | "+cmd+"\n")
os.Exit(0)
}
indent := os.Getenv("JSONPP_INDENT")
if indent == "" {
indent = " "
}
processFunc := processMultipleFile
if *single {
processFunc = processSingularFile
}
var exitStatus = 0
if len(flag.Args()) > 0 {
for _, filename := range flag.Args() {
file, err := os.OpenFile(filename, os.O_RDONLY, 0)
if err != nil {
printError(err)
exitStatus = 1
continue
}
defer file.Close()
exitStatus = processFunc(file, indent)
if exitStatus > 0 {
break
}
}
} else {
exitStatus = processFunc(os.Stdin, indent)
}
os.Exit(exitStatus)
}
func processMultipleFile(fn *os.File, indent string) int {
bufIn := bufio.NewReader(fn)
arr := make([]byte, 0, 1024*1024)
buf := bytes.NewBuffer(arr)
lineNum := int64(1)
for {
lastLine, err := bufIn.ReadBytes('\n')
if err != nil && err != io.EOF {
printError(err)
return 2
}
if err == io.EOF && len(lastLine) == 0 {
break
}
status := indentAndPrint(buf, lastLine, lineNum, indent)
if status > 0 {
return status
}
buf.Reset()
lineNum += 1
if err == io.EOF {
break
}
}
return 0
}
func processSingularFile(fn *os.File, indent string) int {
b, err := ioutil.ReadAll(fn)
if err != nil {
printError(err)
return 2
}
b = bytes.Join(bytes.Split(b, newline), nil)
arr := make([]byte, 0, 1024*1024)
buf := bytes.NewBuffer(arr)
return indentAndPrint(buf, b, 1, indent)
}
func indentAndPrint(buf *bytes.Buffer, js []byte, lineNum int64, indent string) int {
jsErr := json.Indent(buf, js, "", indent)
if jsErr != nil {
malformedJSON(jsErr, js, lineNum)
return 1
}
os.Stdout.Write(buf.Bytes())
return 0
}
func malformedJSON(jsErr error, js []uint8, lineNum int64) {
os.Stdout.Sync()
synErr, isSynError := (jsErr).(*json.SyntaxError)
if isSynError {
fmt.Fprintf(os.Stderr, "ERROR: Broken json on line %d, char %d: %s\n", lineNum, synErr.Offset, jsErr)
prefix := ""
suffix := ""
begin := 0
if synErr.Offset > 15 {
begin = int(synErr.Offset - 15)
prefix = "..."
}
end := begin + 30
if end > len(js) {
end = len(js)
} else {
suffix = "..."
}
b := bytes.TrimRight(js[begin:end], "\r\n")
fmt.Fprintf(os.Stderr, " Context: %s%s%s\n", prefix, b, suffix)
} else {
fmt.Fprintf(os.Stderr, "ERROR: Broken json on line %d: %s\n", lineNum, jsErr)
}
}
func printError(err error) {
os.Stdout.Sync()
fmt.Fprintf(os.Stderr, "ERROR: %s\n", err)
}