Merge pull request #5989 from cli/api-no-json-htmlescape
Avoid HTML-escaping `gh api` JSON output
This commit is contained in:
commit
96d64832c8
1 changed files with 18 additions and 1 deletions
|
|
@ -1,6 +1,7 @@
|
|||
package jsoncolor
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
|
|
@ -49,7 +50,7 @@ func Write(w io.Writer, r io.Reader, indent string) error {
|
|||
fmt.Fprintf(w, "\x1b[%sm%s\x1b[m", colorDelim, tt)
|
||||
}
|
||||
default:
|
||||
b, err := json.Marshal(tt)
|
||||
b, err := marshalJSON(tt)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
@ -94,3 +95,19 @@ func Write(w io.Writer, r io.Reader, indent string) error {
|
|||
|
||||
return nil
|
||||
}
|
||||
|
||||
// marshalJSON works like json.Marshal but with HTML-escaping disabled
|
||||
func marshalJSON(v interface{}) ([]byte, error) {
|
||||
buf := bytes.Buffer{}
|
||||
enc := json.NewEncoder(&buf)
|
||||
enc.SetEscapeHTML(false)
|
||||
if err := enc.Encode(v); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
bb := buf.Bytes()
|
||||
// omit trailing newline added by json.Encoder
|
||||
if len(bb) > 0 && bb[len(bb)-1] == '\n' {
|
||||
return bb[:len(bb)-1], nil
|
||||
}
|
||||
return bb, nil
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue