Skip to content

Commit

Permalink
Fix binary data in JSON output
Browse files Browse the repository at this point in the history
  • Loading branch information
johnkerl committed Aug 25, 2024
1 parent 60bdd6c commit 0965815
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 6 deletions.
24 changes: 18 additions & 6 deletions pkg/mlrval/mlrval_json.go
Original file line number Diff line number Diff line change
Expand Up @@ -352,25 +352,33 @@ func (mv *Mlrval) marshalJSONString(outputIsStdout bool) (string, error) {
}

// Wraps with double-quotes and escape-encoded JSON-special characters.
//
// Per https://www.json.org/json-en.html:
//
// * Escapes: \b \f \n \r \t \u
// * Acceptable ranges: 0x20..0x10FFFF
//
// Since these are bytes here, we only need to check < 0x20, and special-case the five valid
// escapes, and then \u the rest.

func millerJSONEncodeString(input string) string {
var buffer bytes.Buffer

buffer.WriteByte('"')

for _, b := range []byte(input) {
switch b {
case '\\':
buffer.WriteByte('\\')
buffer.WriteByte('\\')
case '\n':
buffer.WriteByte('\\')
buffer.WriteByte('n')
case '\b':
buffer.WriteByte('\\')
buffer.WriteByte('b')
case '\f':
buffer.WriteByte('\\')
buffer.WriteByte('f')
case '\n':
buffer.WriteByte('\\')
buffer.WriteByte('n')
case '\r':
buffer.WriteByte('\\')
buffer.WriteByte('r')
Expand All @@ -381,12 +389,16 @@ func millerJSONEncodeString(input string) string {
buffer.WriteByte('\\')
buffer.WriteByte('"')
default:
buffer.WriteByte(b)
if b < 0x20 {
s := fmt.Sprintf("\\u%04x", b)
buffer.WriteString(s)
} else {
buffer.WriteByte(b)
}
}
}

buffer.WriteByte('"')

return buffer.String()
}

Expand Down
1 change: 1 addition & 0 deletions test/cases/io-json-io/0036/cmd
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
mlr --ijson --opprint cat test/input/binary.json
Empty file.
2 changes: 2 additions & 0 deletions test/cases/io-json-io/0036/expout
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
msg
X����Y
1 change: 1 addition & 0 deletions test/cases/io-json-io/0037/cmd
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
mlr -j cat test/input/binary.json
Empty file.
5 changes: 5 additions & 0 deletions test/cases/io-json-io/0037/expout
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[
{
"msg": "X\u0001\b����\u0012Y"
}
]

0 comments on commit 0965815

Please sign in to comment.