Fix HTML escape encoding

Signed-off-by: Ulysses Souza <ulyssessouza@gmail.com>
This commit is contained in:
Ulysses Souza 2020-10-02 18:53:22 +02:00
parent 83f21b9293
commit 74e86ab06a
3 changed files with 13 additions and 15 deletions

View File

@ -56,7 +56,7 @@ func runInspect(ctx context.Context, id string) error {
if err != nil {
return err
}
fmt.Println(j)
fmt.Print(j)
return nil
}

View File

@ -38,11 +38,11 @@ func Print(toJSON interface{}, format string, outWriter io.Writer, writerFn func
s := reflect.ValueOf(toJSON)
for i := 0; i < s.Len(); i++ {
obj := s.Index(i).Interface()
jsonLine, err := ToCompressedJSON(obj)
jsonLine, err := ToJSON(obj, "", "")
if err != nil {
return err
}
_, _ = fmt.Fprintln(outWriter, jsonLine)
_, _ = fmt.Fprint(outWriter, jsonLine)
}
default:
outJSON, err := ToStandardJSON(toJSON)

View File

@ -17,6 +17,7 @@
package formatter
import (
"bytes"
"encoding/json"
)
@ -24,18 +25,15 @@ const standardIndentation = " "
// ToStandardJSON return a string with the JSON representation of the interface{}
func ToStandardJSON(i interface{}) (string, error) {
b, err := json.MarshalIndent(i, "", standardIndentation)
if err != nil {
return "", err
}
return string(b), nil
return ToJSON(i, "", standardIndentation)
}
// ToCompressedJSON return a string with the JSON representation of the interface{}
func ToCompressedJSON(i interface{}) (string, error) {
b, err := json.Marshal(i)
if err != nil {
return "", err
}
return string(b), nil
// ToJSON return a string with the JSON representation of the interface{}
func ToJSON(i interface{}, prefix string, indentation string) (string, error) {
buffer := &bytes.Buffer{}
encoder := json.NewEncoder(buffer)
encoder.SetEscapeHTML(false)
encoder.SetIndent(prefix, indentation)
err := encoder.Encode(i)
return buffer.String(), err
}