-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathurl.go
More file actions
36 lines (30 loc) · 733 Bytes
/
url.go
File metadata and controls
36 lines (30 loc) · 733 Bytes
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
package jsonutil
import (
"encoding/json/jsontext"
"fmt"
"net/url"
)
// URLMarshal is a custom marshaler for URL values, marshaling them as strings.
func URLMarshal(enc *jsontext.Encoder, u url.URL) 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) 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)
}
}