forked from teambition/rrule-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjson.go
44 lines (39 loc) · 914 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
43
44
package rrule
import "encoding/json"
// MarshalJSON serializes the given RRule in string format
func (r *RRule) MarshalJSON() ([]byte, error) {
return json.Marshal(r.String())
}
// UnmarshalJSON reads an RRule from it's JSON representation
func (r *RRule) UnmarshalJSON(data []byte) error {
var str string
err := json.Unmarshal(data, &str)
if err != nil {
return err
}
rule, err := StrToRRule(str)
if err != nil {
return err
}
r.dupFrom(rule)
return nil
}
// MarshalJSON serializes the given Set in []string format
func (s *Set) MarshalJSON() ([]byte, error) {
return json.Marshal(s.spec())
}
// UnmarshalJSON reads a Set from it's JSON representation
func (s *Set) UnmarshalJSON(data []byte) error {
var lines []string
err := json.Unmarshal(data, &lines)
if err != nil {
return err
}
for _, line := range lines {
err = s.add(line)
if err != nil {
return err
}
}
return nil
}