Skip to content

Commit 80bb49d

Browse files
authored
Format messages with protojson when using --json option. (#609)
1 parent b08e825 commit 80bb49d

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

pkg/util/json.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,18 @@ package util
1717
import (
1818
"encoding/json"
1919
"fmt"
20+
21+
"google.golang.org/protobuf/encoding/protojson"
22+
"google.golang.org/protobuf/proto"
2023
)
2124

2225
func PrintJSON(obj any) {
23-
txt, _ := json.MarshalIndent(obj, "", " ")
26+
const indent = " "
27+
var txt []byte
28+
if m, ok := obj.(proto.Message); ok {
29+
txt, _ = protojson.MarshalOptions{Indent: indent}.Marshal(m)
30+
} else {
31+
txt, _ = json.MarshalIndent(obj, "", indent)
32+
}
2433
fmt.Println(string(txt))
2534
}

0 commit comments

Comments
 (0)