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 {
|
2022-07-23 04:16:06 +00:00
|
|
|
out, err := json.Marshal(obj)
|
2022-05-17 10:23:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-10-02 15:40:28 +00:00
|
|
|
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
|
|
|
|
}
|