-
Notifications
You must be signed in to change notification settings - Fork 0
/
json.go
32 lines (26 loc) · 904 Bytes
/
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
package restic
import (
"context"
"encoding/json"
"github.com/konidev20/rapi/internal/debug"
"github.com/konidev20/rapi/internal/errors"
)
// LoadJSONUnpacked decrypts the data and afterwards calls json.Unmarshal on
// the item.
func LoadJSONUnpacked(ctx context.Context, repo LoaderUnpacked, t FileType, id ID, item interface{}) (err error) {
buf, err := repo.LoadUnpacked(ctx, t, id)
if err != nil {
return err
}
return json.Unmarshal(buf, item)
}
// SaveJSONUnpacked serialises item as JSON and encrypts and saves it in the
// backend as type t, without a pack. It returns the storage hash.
func SaveJSONUnpacked(ctx context.Context, repo SaverUnpacked, t FileType, item interface{}) (ID, error) {
debug.Log("save new blob %v", t)
plaintext, err := json.Marshal(item)
if err != nil {
return ID{}, errors.Wrap(err, "json.Marshal")
}
return repo.SaveUnpacked(ctx, t, plaintext)
}