A tool for archiving & converting scans of postcards, and information about them.
1package types
2
3import (
4 "encoding/json"
5 "fmt"
6 "time"
7)
8
9func (poly *Polygon) UnmarshalJSON(b []byte) error {
10 return poly.multiPolygonUnmarshaller(func(into interface{}) error {
11 return json.Unmarshal(b, into)
12 })
13}
14
15func (d *Date) UnmarshalJSON(b []byte) (err error) {
16 str := string(b)
17 if str == "null" {
18 d.Time = time.Time{}
19 return nil
20 }
21
22 d.Time, err = time.Parse(`"2006-01-02"`, str)
23 return err
24}
25
26func (d Date) MarshalJSON() ([]byte, error) {
27 if d.Time.IsZero() {
28 return []byte("null"), nil
29 }
30 return []byte(d.Time.Format(`"2006-01-02"`)), nil
31}
32
33func (c Color) MarshalJSON() ([]byte, error) {
34 return []byte(fmt.Sprintf(`"#%02X%02X%02X"`, c.R, c.G, c.B)), nil
35}
36
37func (c *Color) UnmarshalJSON(bb []byte) error {
38 r, g, b, err := rgbFromString(string(bb))
39 if err != nil {
40 return err
41 }
42
43 c.R, c.G, c.B = r, g, b
44 return nil
45}
46
47var _ json.Marshaler = (*Date)(nil)
48var _ json.Unmarshaler = (*Date)(nil)
49var _ json.Unmarshaler = (*Polygon)(nil)
50var _ json.Marshaler = (*Color)(nil)
51var _ json.Unmarshaler = (*Color)(nil)