panel/shared/json.go

45 lines
708 B
Go
Raw Normal View History

2024-11-03 21:33:08 +01:00
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
}