-
Notifications
You must be signed in to change notification settings - Fork 1
/
serialise.go
45 lines (40 loc) · 918 Bytes
/
serialise.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
package datapackage
import (
"encoding/json"
"time"
)
const ISO8601Format1 = "2006-01-02"
const ISO8601Format2 = "2006-01-02T15:04:05Z"
func (t *ISO8601) UnmarshalJSON(data []byte) error {
var raw string
if err := json.Unmarshal(data, &raw); err != nil {
return err
}
t.Raw = raw
pt, err := time.Parse(ISO8601Format1, raw)
if err != nil {
if pt, err = time.Parse(ISO8601Format2, raw); err != nil {
if pt, err = time.Parse(time.RFC3339, raw); err != nil {
return err
}
}
}
t.Time = pt
return nil
}
func (t *ISO8601) MarshalJSON() ([]byte, error) {
s := t.Time.UTC().Format(ISO8601Format2)
return json.Marshal(s)
}
func (pk *PKSpec) UnmarshalJSON(data []byte) error {
if len(data) > 0 && data[0] != '[' {
var s string
e := json.Unmarshal(data, &s)
if e != nil {
return e
}
pk.Keys = append(pk.Keys, s)
return nil
}
return json.Unmarshal(data, (*[]string)(&pk.Keys))
}