-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
257 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
# jsonhelper | ||
# jsonhelper | ||
|
||
Helpers for Go's 'json' package. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
[InternetShortcut] | ||
URL=https://github.com/solsw/jsonhelper |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module github.com/solsw/jsonhelper | ||
|
||
go 1.18 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
go test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
// Package jsonhelper contains 'json' package helpers. | ||
package jsonhelper | ||
|
||
import ( | ||
"encoding/json" | ||
"io" | ||
"strings" | ||
) | ||
|
||
// MarshalMust is like json.Marshal but panics in case of error. | ||
func MarshalMust(v any) []byte { | ||
bb, err := json.Marshal(v) | ||
if err != nil { | ||
panic(err) | ||
} | ||
return bb | ||
} | ||
|
||
// MarshalIndentMust is like json.MarshalIndent but panics in case of error. | ||
// (See json.MarshalIndent for 'prefix' and 'indent' usage.) | ||
func MarshalIndentMust(v any, prefix, indent string) []byte { | ||
bb, err := json.MarshalIndent(v, prefix, indent) | ||
if err != nil { | ||
panic(err) | ||
} | ||
return bb | ||
} | ||
|
||
// MarshalIndentStr formats 'v' to string. | ||
// (See json.MarshalIndent for 'prefix' and 'indent' usage.) | ||
func MarshalIndentStr(v any, prefix, indent string) (string, error) { | ||
bb, err := json.MarshalIndent(v, prefix, indent) | ||
if err != nil { | ||
return "", err | ||
} | ||
return string(bb), nil | ||
} | ||
|
||
// MarshalIndentStrMust is like MarshalIndentStr but panics in case of error. | ||
func MarshalIndentStrMust(v any, prefix, indent string) string { | ||
s, err := MarshalIndentStr(v, prefix, indent) | ||
if err != nil { | ||
panic(err) | ||
} | ||
return s | ||
} | ||
|
||
// Format reads JSON-encoded data from 'r', then writes formatted data to 'w'. | ||
// (See json.MarshalIndent for 'prefix' and 'indent' usage.) | ||
func Format(r io.Reader, w io.Writer, prefix, indent string) error { | ||
var v any | ||
if err := json.NewDecoder(r).Decode(&v); err != nil { | ||
return err | ||
} | ||
e := json.NewEncoder(w) | ||
e.SetIndent(prefix, indent) | ||
return e.Encode(v) | ||
} | ||
|
||
// FormatStr formats JSON-encoded 'json' to string. | ||
// (See json.MarshalIndent for 'prefix' and 'indent' usage.) | ||
func FormatStr(json, prefix, indent string) (string, error) { | ||
var b strings.Builder | ||
if err := Format(strings.NewReader(json), &b, prefix, indent); err != nil { | ||
return "", err | ||
} | ||
return b.String(), nil | ||
} | ||
|
||
// FormatStrMust is like FormatStr but panics in case of error. | ||
func FormatStrMust(json, prefix, indent string) string { | ||
s, err := FormatStr(json, prefix, indent) | ||
if err != nil { | ||
panic(err) | ||
} | ||
return s | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,167 @@ | ||
package jsonhelper | ||
|
||
import ( | ||
"bytes" | ||
"io" | ||
"strings" | ||
"testing" | ||
) | ||
|
||
func TestMarshalIndentStr(t *testing.T) { | ||
type args struct { | ||
v any | ||
prefix string | ||
indent string | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
want string | ||
wantErr bool | ||
}{ | ||
{name: "1", | ||
args: args{ | ||
v: complex(1.2, 3.4), | ||
prefix: "", | ||
indent: " ", | ||
}, | ||
wantErr: true, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
got, err := MarshalIndentStr(tt.args.v, tt.args.prefix, tt.args.indent) | ||
if (err != nil) != tt.wantErr { | ||
t.Errorf("MarshalIndentStr() error = %v, wantErr %v", err, tt.wantErr) | ||
return | ||
} | ||
if got != tt.want { | ||
t.Errorf("MarshalIndentStr() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestMarshalIndentStrMust(t *testing.T) { | ||
type is struct { | ||
I int | ||
S string | ||
} | ||
type args struct { | ||
v any | ||
prefix string | ||
indent string | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
want string | ||
}{ | ||
{name: "1", | ||
args: args{ | ||
v: is{1, "one"}, | ||
prefix: "", | ||
indent: " ", | ||
}, | ||
want: `{ | ||
"I": 1, | ||
"S": "one" | ||
}`, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if got := MarshalIndentStrMust(tt.args.v, tt.args.prefix, tt.args.indent); got != tt.want { | ||
t.Errorf("MarshalIndentStrMust() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestFormat(t *testing.T) { | ||
type args struct { | ||
r io.Reader | ||
prefix string | ||
indent string | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
want string | ||
wantErr bool | ||
}{ | ||
{name: "1e", | ||
args: args{ | ||
r: &strings.Reader{}, | ||
prefix: "", | ||
indent: " ", | ||
}, | ||
wantErr: true, | ||
}, | ||
{name: "1", | ||
args: args{ | ||
r: strings.NewReader(`{"i":1,"s":"string"}`), | ||
prefix: "", | ||
indent: " ", | ||
}, | ||
want: "{\n \"i\": 1,\n \"s\": \"string\"\n}\n", | ||
}, | ||
{name: "2", | ||
args: args{ | ||
r: strings.NewReader(`[{"i":1,"s":"one"}]`), | ||
prefix: "", | ||
indent: " ", | ||
}, | ||
want: "[\n {\n \"i\": 1,\n \"s\": \"one\"\n }\n]\n", | ||
}, | ||
{name: "3", | ||
args: args{ | ||
r: strings.NewReader(`[{"i":1,"s":"one"},{"i":2,"s":"two"}]`), | ||
prefix: "", | ||
indent: " ", | ||
}, | ||
want: "[\n {\n \"i\": 1,\n \"s\": \"one\"\n },\n {\n \"i\": 2,\n \"s\": \"two\"\n }\n]\n", | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
w := &bytes.Buffer{} | ||
if err := Format(tt.args.r, w, tt.args.prefix, tt.args.indent); (err != nil) != tt.wantErr { | ||
t.Errorf("Format() error = %v, wantErr %v", err, tt.wantErr) | ||
return | ||
} | ||
if got := w.String(); got != tt.want { | ||
t.Errorf("Format() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestFormatStrMust(t *testing.T) { | ||
type args struct { | ||
json string | ||
prefix string | ||
indent string | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
want string | ||
}{ | ||
{name: "1", | ||
args: args{ | ||
json: `{"i":1,"s":"string"}`, | ||
prefix: "", | ||
indent: " ", | ||
}, | ||
want: "{\n \"i\": 1,\n \"s\": \"string\"\n}\n", | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if got := FormatStrMust(tt.args.json, tt.args.prefix, tt.args.indent); got != tt.want { | ||
t.Errorf("FormatStrMust() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} |