Skip to content

Commit ec64d93

Browse files
committed
github.com/qiniu/x/jsonutil.Stringify
1 parent 08338a8 commit ec64d93

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

jsonutil/unmarshal_string.go renamed to jsonutil/json_string.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88

99
// ----------------------------------------------------------
1010

11+
// Unmarshal parses the JSON-encoded data and stores the result in the value pointed to by v.
1112
func Unmarshal(data string, v interface{}) error {
1213

1314
sh := *(*reflect.StringHeader)(unsafe.Pointer(&data))
@@ -17,3 +18,10 @@ func Unmarshal(data string, v interface{}) error {
1718

1819
// ----------------------------------------------------------
1920

21+
// Stringify converts a value into string.
22+
func Stringify(v interface{}) string {
23+
b, _ := json.Marshal(v)
24+
return string(b)
25+
}
26+
27+
// ----------------------------------------------------------

jsonutil/unmarshal_test.go renamed to jsonutil/json_string_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ import (
77
func Test(t *testing.T) {
88

99
var ret struct {
10-
Id string `json:"id"`
10+
ID string `json:"id"`
1111
}
1212
err := Unmarshal(`{"id": "123"}`, &ret)
1313
if err != nil {
1414
t.Fatal("Unmarshal failed:", err)
1515
}
16-
if ret.Id != "123" {
17-
t.Fatal("Unmarshal uncorrect:", ret.Id)
16+
if ret.ID != "123" {
17+
t.Fatal("Unmarshal uncorrect:", ret.ID)
1818
}
1919
}

0 commit comments

Comments
 (0)