Skip to content

Commit 2659090

Browse files
committed
feat: add pretty-json flag to control JSON message formatting
1 parent 747ea6f commit 2659090

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

example/main.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ func main() {
2020
privateKey := flag.String("key", "", "Private key hex (will generate if not provided)")
2121
topics := flag.String("topics", "broadcast_p2p_poc", "Comma-separated list of topics to subscribe to")
2222
noBroadcast := flag.Bool("no-broadcast", false, "Disable message broadcasting")
23+
prettyJson := flag.Bool("pretty-json", false, "Pretty print JSON messages")
2324

2425
flag.Parse()
2526

@@ -102,11 +103,15 @@ func main() {
102103
for msg := range allMsgChan {
103104
var data string
104105

105-
// Try to unmarshal and re-marshal for pretty printing
106-
var jsonObj interface{}
107-
if err := json.Unmarshal(msg.Data, &jsonObj); err == nil {
108-
if jsonBytes, err := json.MarshalIndent(jsonObj, "", " "); err == nil {
109-
data = string(jsonBytes)
106+
if *prettyJson {
107+
// Try to unmarshal and re-marshal for pretty printing
108+
var jsonObj interface{}
109+
if err := json.Unmarshal(msg.Data, &jsonObj); err == nil {
110+
if jsonBytes, err := json.MarshalIndent(jsonObj, "", " "); err == nil {
111+
data = string(jsonBytes)
112+
} else {
113+
data = string(msg.Data)
114+
}
110115
} else {
111116
data = string(msg.Data)
112117
}

0 commit comments

Comments
 (0)