You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

45 lines
783 B

package main
import (
"bytes"
"encoding/json"
"io"
"os"
)
func loadFromReader(r io.Reader, obj interface{}) error {
buf := new(bytes.Buffer)
if _, err := buf.ReadFrom(r); err != nil {
return err
}
return json.Unmarshal(buf.Bytes(), obj)
}
func loadFromFile(path string, obj interface{}) error {
f, err := os.Open(path)
if err != nil {
return err
}
defer f.Close()
return loadFromReader(f, obj)
}
func saveToWriter(w io.Writer, obj interface{}) error {
b, err := json.MarshalIndent(obj, "", " ")
if err != nil {
return err
}
if _, err := w.Write(b); err != nil {
return err
}
return nil
}
func saveToFile(path string, obj interface{}) error {
f, err := os.Create(path)
if err != nil {
return err
}
defer f.Close()
return saveToWriter(f, obj)
}