/
encoding.go
49 lines (41 loc) · 1012 Bytes
/
encoding.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
package common
import (
"bytes"
"encoding/ascii85"
"encoding/json"
"github.com/fxamacker/cbor/v2"
)
func (app *App) MarshalJSON(x interface{}) ([]byte, error) {
return json.Marshal(x)
}
func (app *App) UnmarshalJSON(b []byte, dst interface{}) error {
return json.Unmarshal(b, dst)
}
func (app *App) MarshalCBOR(x interface{}) ([]byte, error) {
return app.cbor.Marshal(x)
}
func (app *App) UnmarshalCBOR(b []byte, dst interface{}) error {
return cbor.Unmarshal(b, dst)
}
func (app *App) CompactSerial(x interface{}) (string, error) {
b, err := app.MarshalCBOR(x)
if err != nil {
return "", nil
}
buf := bytes.NewBuffer(nil)
enc := ascii85.NewEncoder(buf)
enc.Write(b)
enc.Close()
return string(buf.Bytes()), nil
}
func (app *App) ExpandSerial(serial []byte, dst interface{}) error {
out := make([]byte, len(serial)*2)
d, _, err := ascii85.Decode(out, serial, true)
if err != nil {
return err
}
if err := app.UnmarshalCBOR(out[:d], dst); err != nil {
return err
}
return nil
}