-
Notifications
You must be signed in to change notification settings - Fork 191
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ feat: jsonutil - add new func MustPretty, MustString and add more u…
…nit tests
- Loading branch information
Showing
4 changed files
with
218 additions
and
92 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package jsonutil | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"io" | ||
) | ||
|
||
// MustString encode data to json string, will panic on error | ||
func MustString(v any) string { | ||
bs, err := json.Marshal(v) | ||
if err != nil { | ||
panic(err) | ||
} | ||
return string(bs) | ||
} | ||
|
||
// Encode data to json bytes. alias of json.Marshal | ||
func Encode(v any) ([]byte, error) { | ||
return json.Marshal(v) | ||
} | ||
|
||
// EncodePretty encode data to pretty JSON bytes. | ||
func EncodePretty(v any) ([]byte, error) { | ||
return json.MarshalIndent(v, "", " ") | ||
} | ||
|
||
// EncodeString encode data to JSON string. | ||
func EncodeString(v any) (string, error) { | ||
bs, err := json.MarshalIndent(v, "", " ") | ||
return string(bs), err | ||
} | ||
|
||
// EncodeToWriter encode data to json and write to writer. | ||
func EncodeToWriter(v any, w io.Writer) error { | ||
return json.NewEncoder(w).Encode(v) | ||
} | ||
|
||
// EncodeUnescapeHTML data to json bytes. will close escape HTML | ||
func EncodeUnescapeHTML(v any) ([]byte, error) { | ||
buf := &bytes.Buffer{} | ||
enc := json.NewEncoder(buf) | ||
enc.SetEscapeHTML(false) | ||
|
||
if err := enc.Encode(v); err != nil { | ||
return nil, err | ||
} | ||
return buf.Bytes(), nil | ||
} | ||
|
||
// Decode json bytes to data ptr. alias of json.Unmarshal | ||
func Decode(bts []byte, ptr any) error { | ||
return json.Unmarshal(bts, ptr) | ||
} | ||
|
||
// DecodeString json string to data ptr. | ||
func DecodeString(str string, ptr any) error { | ||
return json.Unmarshal([]byte(str), ptr) | ||
} | ||
|
||
// DecodeReader decode JSON from io reader. | ||
func DecodeReader(r io.Reader, ptr any) error { | ||
return json.NewDecoder(r).Decode(ptr) | ||
} |
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 jsonutil_test | ||
|
||
import ( | ||
"bytes" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/gookit/goutil/jsonutil" | ||
"github.com/gookit/goutil/testutil/assert" | ||
) | ||
|
||
func TestEncode(t *testing.T) { | ||
bts, err := jsonutil.Encode(testUser) | ||
assert.NoErr(t, err) | ||
assert.Eq(t, `{"name":"inhere","age":200}`, string(bts)) | ||
|
||
bts, err = jsonutil.Encode(&testUser) | ||
assert.NoErr(t, err) | ||
assert.Eq(t, `{"name":"inhere","age":200}`, string(bts)) | ||
|
||
assert.Eq(t, `{"name":"inhere","age":200}`, jsonutil.MustString(testUser)) | ||
|
||
assert.Panics(t, func() { | ||
jsonutil.MustString(invalid) | ||
}) | ||
|
||
bts, err = jsonutil.Encode(&testUser) | ||
assert.NoErr(t, err) | ||
assert.Eq(t, `{"name":"inhere","age":200}`, string(bts)) | ||
} | ||
|
||
func TestEncodeUnescapeHTML(t *testing.T) { | ||
bts, err := jsonutil.EncodeUnescapeHTML(&testUser) | ||
assert.NoErr(t, err) | ||
assert.Eq(t, `{"name":"inhere","age":200} | ||
`, string(bts)) | ||
|
||
_, err = jsonutil.EncodeUnescapeHTML(invalid) | ||
assert.Err(t, err) | ||
} | ||
|
||
func TestEncodeToWriter(t *testing.T) { | ||
buf := &bytes.Buffer{} | ||
|
||
err := jsonutil.EncodeToWriter(testUser, buf) | ||
assert.NoErr(t, err) | ||
assert.Eq(t, `{"name":"inhere","age":200} | ||
`, buf.String()) | ||
} | ||
|
||
func TestDecode(t *testing.T) { | ||
str := `{"name":"inhere","age":200}` | ||
usr := &user{} | ||
err := jsonutil.Decode([]byte(str), usr) | ||
|
||
assert.NoErr(t, err) | ||
assert.Eq(t, "inhere", usr.Name) | ||
assert.Eq(t, 200, usr.Age) | ||
} | ||
|
||
func TestDecodeString(t *testing.T) { | ||
str := `{"name":"inhere","age":200}` | ||
usr := &user{} | ||
err := jsonutil.DecodeString(str, usr) | ||
|
||
assert.NoErr(t, err) | ||
assert.Eq(t, "inhere", usr.Name) | ||
assert.Eq(t, 200, usr.Age) | ||
|
||
// DecodeReader | ||
usr = &user{} | ||
err = jsonutil.DecodeReader(strings.NewReader(str), usr) | ||
|
||
assert.NoErr(t, err) | ||
assert.Eq(t, "inhere", usr.Name) | ||
assert.Eq(t, 200, usr.Age) | ||
} |
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