bbgo_origin/pkg/util/json.go

39 lines
525 B
Go
Raw Permalink Normal View History

2022-05-17 10:23:09 +00:00
package util
import (
"encoding/json"
2023-07-11 02:23:54 +00:00
"io"
2022-05-17 10:23:09 +00:00
"io/ioutil"
2023-07-11 02:23:54 +00:00
"os"
2022-05-17 10:23:09 +00:00
)
func WriteJsonFile(p string, obj interface{}) error {
out, err := json.Marshal(obj)
2022-05-17 10:23:09 +00:00
if err != nil {
return err
}
return ioutil.WriteFile(p, out, 0644)
}
2023-07-11 02:23:54 +00:00
func ReadJsonFile(file string, obj interface{}) error {
f, err := os.Open(file)
if err != nil {
return err
}
defer f.Close()
byteResult, err := io.ReadAll(f)
if err != nil {
return err
}
err = json.Unmarshal([]byte(byteResult), obj)
if err != nil {
return err
}
return nil
}