aboutsummaryrefslogtreecommitdiffsponsor
path: root/internal/service/service_json.go
diff options
context:
space:
mode:
Diffstat (limited to 'internal/service/service_json.go')
-rw-r--r--internal/service/service_json.go64
1 files changed, 64 insertions, 0 deletions
diff --git a/internal/service/service_json.go b/internal/service/service_json.go
new file mode 100644
index 0000000..571eb30
--- /dev/null
+++ b/internal/service/service_json.go
@@ -0,0 +1,64 @@
+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
+}