-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathurl.go
38 lines (31 loc) · 831 Bytes
/
url.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
package jsonutil
import (
"fmt"
"net/url"
"github.com/go-json-experiment/json"
"github.com/go-json-experiment/json/jsontext"
)
// URLMarshal is a custom marshaler for URL values, marshaling them as strings.
func URLMarshal(enc *jsontext.Encoder, u url.URL, opts json.Options) error {
return enc.WriteToken(jsontext.String(u.String()))
}
// URLUnmarshal is a custom unmarshaler for URL values, unmarshaling them from strings.
func URLUnmarshal(dec *jsontext.Decoder, u *url.URL, opts json.Options) error {
tkn, err := dec.ReadToken()
if err != nil {
return err
}
switch tkn.Kind() {
case '"':
parsed, err := url.Parse(tkn.String())
if err != nil {
return err
}
*u = *parsed
return nil
case 'n': // null
return nil // no url given
default:
return fmt.Errorf("expected string, got %s", tkn)
}
}