-
Notifications
You must be signed in to change notification settings - Fork 3
/
json.go
42 lines (35 loc) · 896 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
33
34
35
36
37
38
39
40
41
42
package marcel
import (
"bytes"
"encoding/json"
"io/ioutil"
)
func (attachment *Attachment) MarshalJSON() ([]byte, error) {
data, err := ioutil.ReadAll(attachment.Data)
if err != nil {
return []byte{}, err
}
return json.Marshal(&struct {
ContentType string `json:"content_type"`
Data []byte `json:"data"`
Filename string `json:"filename"`
}{
ContentType: attachment.ContentType,
Data: data,
Filename: attachment.Filename,
})
}
func (attachment *Attachment) UnmarshalJSON(data []byte) error {
inner := struct {
ContentType string `json:"content_type"`
Data []byte `json:"data"`
Filename string `json:"filename"`
}{}
if err := json.Unmarshal(data, &inner); err != nil {
return err
}
attachment.ContentType = inner.ContentType
attachment.Filename = inner.Filename
attachment.Data = bytes.NewReader(inner.Data)
return nil
}