-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjson.go
61 lines (49 loc) · 1.25 KB
/
json.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// Copyright (c) 2021 Andrej Giesbrecht
package store
import (
"bytes"
"encoding/json"
"io"
"os"
)
// marshal is a function that marshals the object into an io.Reader.
// By default, it uses the JSON marshaller.
var marshal = func(v interface{}) (io.Reader, error) {
b, err := json.MarshalIndent(v, "", "\t")
if err != nil {
return nil, err
}
return bytes.NewReader(b), nil
}
// unmarshal is a function that unmarshals the data
// from the reader into the specified value.
var unmarshal = func(r io.Reader, v interface{}) error {
return json.NewDecoder(r).Decode(v)
}
// ReadJSON loads the file into v and returns an error, if any.
func ReadJSON(file string, v interface{}) error {
lock.Lock()
defer lock.Unlock()
f, err := os.Open(file)
if err != nil {
return err
}
defer f.Close()
return unmarshal(f, v)
}
// WriteJSON saves a representation of v to the file.
// It returns the number of bytes written and an error, if any.
func WriteJSON(file string, v interface{}) (int64, error) {
lock.Lock()
defer lock.Unlock()
f, err := os.Create(file)
if err != nil {
return 0, err
}
defer f.Close()
r, err := marshal(v)
if err != nil {
return 0, err
}
return io.Copy(f, r)
}