panel/shared/json.go
2024-11-03 15:33:08 -05:00

44 lines
708 B
Go

package shared
import (
"bytes"
"io"
"io/fs"
"os"
"github.com/goccy/go-json"
)
func LoadJSONFileFS(fsys fs.FS, fileName string, value interface{}) error {
file, err := fs.ReadFile(fsys, fileName)
if err != nil {
return err
}
fileBuffer := bytes.NewReader(file)
if err := json.NewDecoder(fileBuffer).Decode(&value); err != nil && err != io.EOF {
return err
}
clear(file)
return nil
}
func LoadJSONFile(fileName string, value interface{}) error {
file, err := os.ReadFile(fileName)
if err != nil {
return err
}
fileBuffer := bytes.NewReader(file)
if err := json.NewDecoder(fileBuffer).Decode(&value); err != nil && err != io.EOF {
return err
}
clear(file)
return nil
}