Skip to content

Commit d233b17

Browse files
committed
IndentAny added
1 parent c3467de commit d233b17

File tree

4 files changed

+92
-37
lines changed

4 files changed

+92
-37
lines changed

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2022 Oleg Subachev
3+
Copyright (c) 2022-2025 Oleg Subachev
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

indent.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package jsonhelper
2+
3+
import (
4+
"bytes"
5+
"encoding/json"
6+
"io"
7+
)
8+
9+
// IndentRW reads JSON data from [io.Reader] and writes indented data to [io.Writer].
10+
// (See [json.Indent] for 'prefix' and 'indent' usage.)
11+
func IndentRW(r io.Reader, w io.Writer, prefix, indent string) error {
12+
bb, err := io.ReadAll(r)
13+
if err != nil {
14+
return err
15+
}
16+
var buf bytes.Buffer
17+
if err := json.Indent(&buf, bb, prefix, indent); err != nil {
18+
return err
19+
}
20+
_, err = buf.WriteTo(w)
21+
if err != nil {
22+
return err
23+
}
24+
return nil
25+
}
26+
27+
// IndentStr returns the indented form of JSON string 'j'.
28+
// (See [json.Indent] for 'prefix' and 'indent' usage.)
29+
func IndentStr(j, prefix, indent string) (string, error) {
30+
var buf bytes.Buffer
31+
if err := json.Indent(&buf, []byte(j), prefix, indent); err != nil {
32+
return "", err
33+
}
34+
return buf.String(), nil
35+
}
36+
37+
// IndentAny returns the indented JSON encoding of 'v' as string.
38+
// (See [json.Indent] for 'prefix' and 'indent' usage.)
39+
func IndentAny(v any, prefix, indent string) (string, error) {
40+
bb, err := json.MarshalIndent(v, prefix, indent)
41+
if err != nil {
42+
return "", err
43+
}
44+
return string(bb), nil
45+
}

jsonhelper_test.go renamed to indent_test.go

Lines changed: 46 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,23 +33,23 @@ func TestIndentRW(t *testing.T) {
3333
prefix: "",
3434
indent: " ",
3535
},
36-
want: "{\n \"i\": 1,\n \"s\": \"string\"\n}\n",
36+
want: "{\n \"i\": 1,\n \"s\": \"string\"\n}",
3737
},
3838
{name: "2",
3939
args: args{
4040
r: strings.NewReader(`[{"i":1,"s":"one"}]`),
4141
prefix: "",
4242
indent: " ",
4343
},
44-
want: "[\n {\n \"i\": 1,\n \"s\": \"one\"\n }\n]\n",
44+
want: "[\n {\n \"i\": 1,\n \"s\": \"one\"\n }\n]",
4545
},
4646
{name: "3",
4747
args: args{
4848
r: strings.NewReader(`[{"i":1,"s":"one"},{"i":2,"s":"two"}]`),
4949
prefix: "",
5050
indent: " ",
5151
},
52-
want: "[\n {\n \"i\": 1,\n \"s\": \"one\"\n },\n {\n \"i\": 2,\n \"s\": \"two\"\n }\n]\n",
52+
want: "[\n {\n \"i\": 1,\n \"s\": \"one\"\n },\n {\n \"i\": 2,\n \"s\": \"two\"\n }\n]",
5353
},
5454
}
5555
for _, tt := range tests {
@@ -68,7 +68,7 @@ func TestIndentRW(t *testing.T) {
6868

6969
func TestIndentStr(t *testing.T) {
7070
type args struct {
71-
json string
71+
j string
7272
prefix string
7373
indent string
7474
}
@@ -79,18 +79,57 @@ func TestIndentStr(t *testing.T) {
7979
}{
8080
{name: "1",
8181
args: args{
82-
json: `{"i":1,"s":"string"}`,
82+
j: `{"i":1,"s":"string"}`,
8383
prefix: "",
8484
indent: " ",
8585
},
86-
want: "{\n \"i\": 1,\n \"s\": \"string\"\n}\n",
86+
want: "{\n \"i\": 1,\n \"s\": \"string\"\n}",
8787
},
8888
}
8989
for _, tt := range tests {
9090
t.Run(tt.name, func(t *testing.T) {
91-
if got, _ := IndentStr(tt.args.json, tt.args.prefix, tt.args.indent); got != tt.want {
91+
got, _ := IndentStr(tt.args.j, tt.args.prefix, tt.args.indent)
92+
if got != tt.want {
9293
t.Errorf("IndentStr() = %v, want %v", got, tt.want)
9394
}
9495
})
9596
}
9697
}
98+
99+
func TestIndentAny(t *testing.T) {
100+
type args struct {
101+
v any
102+
prefix string
103+
indent string
104+
}
105+
tests := []struct {
106+
name string
107+
args args
108+
want string
109+
wantErr bool
110+
}{
111+
{name: "1",
112+
args: args{
113+
v: struct {
114+
I int `json:"i"`
115+
S string `json:"s"`
116+
}{I: 1, S: "string"},
117+
prefix: "",
118+
indent: " ",
119+
},
120+
want: "{\n \"i\": 1,\n \"s\": \"string\"\n}",
121+
},
122+
}
123+
for _, tt := range tests {
124+
t.Run(tt.name, func(t *testing.T) {
125+
got, err := IndentAny(tt.args.v, tt.args.prefix, tt.args.indent)
126+
if (err != nil) != tt.wantErr {
127+
t.Errorf("IndentAny() error = %v, wantErr %v", err, tt.wantErr)
128+
return
129+
}
130+
if got != tt.want {
131+
t.Errorf("IndentAny() = %v, want %v", got, tt.want)
132+
}
133+
})
134+
}
135+
}

jsonhelper.go

Lines changed: 0 additions & 29 deletions
This file was deleted.

0 commit comments

Comments
 (0)