Skip to content

Commit

Permalink
add codec/json
Browse files Browse the repository at this point in the history
  • Loading branch information
lixiaohui committed May 5, 2022
1 parent 7135b20 commit 5e3d746
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions codec/json.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package codec

import (
"encoding/json"

"github.com/go-board/std/result"
)

func MarshalJSON[T any](t T) result.Result[[]byte] {
return result.Of(json.Marshal(t))
}

func UnmarshalJSON[T any](data []byte) result.Result[T] {
var t T
err := json.Unmarshal(data, &t)
return result.Of(t, err)
}

func UnmarshalJSONPtr[T any](data []byte) result.Result[*T] {
var t *T
err := json.Unmarshal(data, &t)
return result.Of(t, err)
}

func Jsonify[T any](data T) string {
b, err := json.Marshal(data)
if err != nil {
return ""
}
return string(b)
}

0 comments on commit 5e3d746

Please sign in to comment.