File: jsons.go 1 /* 2 The MIT License (MIT) 3 4 Copyright (c) 2026 pacman64 5 6 Permission is hereby granted, free of charge, to any person obtaining a copy of 7 this software and associated documentation files (the "Software"), to deal 8 in the Software without restriction, including without limitation the rights to 9 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies 10 of the Software, and to permit persons to whom the Software is furnished to do 11 so, subject to the following conditions: 12 13 The above copyright notice and this permission notice shall be included in all 14 copies or substantial portions of the Software. 15 16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 SOFTWARE. 23 */ 24 25 /* 26 To compile a smaller-sized command-line app, you can use the `go` command as 27 follows: 28 29 go build -ldflags "-s -w" -trimpath jsons.go 30 */ 31 32 package main 33 34 import ( 35 "bufio" 36 "io" 37 "os" 38 "strings" 39 ) 40 41 const info = ` 42 jsons [options...] [filenames...] 43 44 JSON Strings turns TSV (tab-separated values) data into a JSON array of 45 objects whose values are strings or nulls, the latter being used for 46 missing trailing values. 47 ` 48 49 func main() { 50 if len(os.Args) > 1 { 51 switch os.Args[1] { 52 case `-h`, `--h`, `-help`, `--help`: 53 os.Stdout.WriteString(info[1:]) 54 return 55 } 56 } 57 58 err := run(os.Args[1:]) 59 if err == io.EOF { 60 err = nil 61 } 62 63 if err != nil { 64 os.Stderr.WriteString(err.Error()) 65 os.Stderr.WriteString("\n") 66 os.Exit(1) 67 } 68 } 69 70 type runConfig struct { 71 lines int 72 keys []string 73 } 74 75 func run(paths []string) error { 76 bw := bufio.NewWriter(os.Stdout) 77 defer bw.Flush() 78 79 dashes := 0 80 var cfg runConfig 81 82 for _, path := range paths { 83 if path == `-` { 84 dashes++ 85 if dashes > 1 { 86 continue 87 } 88 89 if err := handleInput(bw, os.Stdin, &cfg); err != nil { 90 return err 91 } 92 93 continue 94 } 95 96 if err := handleFile(bw, path, &cfg); err != nil { 97 return err 98 } 99 } 100 101 if len(paths) == 0 { 102 if err := handleInput(bw, os.Stdin, &cfg); err != nil { 103 return err 104 } 105 } 106 107 if cfg.lines > 1 { 108 bw.WriteString("\n]\n") 109 } else { 110 bw.WriteString("[]\n") 111 } 112 return nil 113 } 114 115 func handleFile(w *bufio.Writer, path string, cfg *runConfig) error { 116 f, err := os.Open(path) 117 if err != nil { 118 return err 119 } 120 defer f.Close() 121 return handleInput(w, f, cfg) 122 } 123 124 func escapeKeys(line string) []string { 125 var keys []string 126 var sb strings.Builder 127 128 loopTSV(line, func(i int, s string) { 129 sb.WriteByte('"') 130 for _, r := range s { 131 if r == '\\' || r == '"' { 132 sb.WriteByte('\\') 133 } 134 sb.WriteRune(r) 135 } 136 sb.WriteByte('"') 137 138 keys = append(keys, sb.String()) 139 sb.Reset() 140 }) 141 142 return keys 143 } 144 145 func emitRow(w *bufio.Writer, line string, keys []string) { 146 j := 0 147 w.WriteByte('{') 148 149 loopTSV(line, func(i int, s string) { 150 j = i 151 if i > 0 { 152 w.WriteString(", ") 153 } 154 155 w.WriteString(keys[i]) 156 w.WriteString(": \"") 157 158 for _, r := range s { 159 if r == '\\' || r == '"' { 160 w.WriteByte('\\') 161 } 162 w.WriteRune(r) 163 } 164 w.WriteByte('"') 165 }) 166 167 for i := j + 1; i < len(keys); i++ { 168 if i > 0 { 169 w.WriteString(", ") 170 } 171 w.WriteString(keys[i]) 172 w.WriteString(": null") 173 } 174 w.WriteByte('}') 175 } 176 177 func loopTSV(line string, f func(i int, s string)) { 178 for i := 0; len(line) > 0; i++ { 179 pos := strings.IndexByte(line, '\t') 180 if pos < 0 { 181 f(i, line) 182 return 183 } 184 185 f(i, line[:pos]) 186 line = line[pos+1:] 187 } 188 } 189 190 func handleInput(w *bufio.Writer, r io.Reader, cfg *runConfig) error { 191 const gb = 1024 * 1024 * 1024 192 sc := bufio.NewScanner(r) 193 sc.Buffer(nil, 8*gb) 194 195 for i := 0; sc.Scan(); i++ { 196 s := sc.Text() 197 if i == 0 && strings.HasPrefix(s, "\xef\xbb\xbf") { 198 s = s[3:] 199 } 200 201 if cfg.lines == 0 { 202 cfg.keys = escapeKeys(s) 203 w.WriteByte('[') 204 cfg.lines++ 205 continue 206 } 207 208 if cfg.lines == 1 { 209 w.WriteString("\n ") 210 } else { 211 if _, err := w.WriteString(",\n "); err != nil { 212 return io.EOF 213 } 214 } 215 216 emitRow(w, s, cfg.keys) 217 cfg.lines++ 218 } 219 220 return sc.Err() 221 }