package service import ( "encoding/json" "net/url" ) type JsonURL struct { *url.URL } func (m JsonURL) MarshalJSON() ([]byte, error) { str := m.String() return json.Marshal(str) } func (m *JsonURL) UnmarshalJSON(buf []byte) error { var str string err := json.Unmarshal(buf, &str) if err != nil { return err } m.URL, err = url.Parse(str) return err } func (m Mirror) MarshalJSON() ([]byte, error) { type Alias Mirror m2 := struct { *Alias From JsonURL `json:"from"` To JsonURL `json:"to"` }{ Alias: (*Alias)(&m), From: JsonURL{m.From}, To: JsonURL{m.To}, } return json.Marshal(m2) } func (m *Mirror) UnmarshalJSON(buf []byte) error { type Alias Mirror var m2 struct { *Alias From JsonURL `json:"from"` To JsonURL `json:"to"` } m2.Alias = (*Alias)(m) err := json.Unmarshal(buf, &m2) if err != nil { return err } m.From = m2.From.URL m.To = m2.To.URL return nil }