bbgo_origin/pkg/util/json.go

38 lines
508 B
Go
Raw 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"
"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 os.WriteFile(p, out, 0644)
2022-05-17 10:23:09 +00:00
}
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
}