MarshalJSON marshals c into JSON with type information included on the interface values.
()
| 140 | // MarshalJSON marshals c into JSON with type information |
| 141 | // included on the interface values. |
| 142 | func (c Checkup) MarshalJSON() ([]byte, error) { |
| 143 | // Start with the fields of c that don't require special |
| 144 | // handling; unfortunately this has to mimic c's definition. |
| 145 | easy := struct { |
| 146 | ConcurrentChecks int `json:"concurrent_checks,omitempty"` |
| 147 | Timestamp time.Time `json:"timestamp,omitempty"` |
| 148 | }{ |
| 149 | ConcurrentChecks: c.ConcurrentChecks, |
| 150 | Timestamp: c.Timestamp, |
| 151 | } |
| 152 | result, err := json.Marshal(easy) |
| 153 | if err != nil { |
| 154 | return result, err |
| 155 | } |
| 156 | |
| 157 | wrap := func(key string, value []byte) { |
| 158 | b := append([]byte{result[0]}, []byte(`"`+key+`":`)...) |
| 159 | b = append(b, value...) |
| 160 | if len(result) > 2 { |
| 161 | b = append(b, ',') |
| 162 | } |
| 163 | result = append(b, result[1:]...) |
| 164 | } |
| 165 | |
| 166 | // Checkers |
| 167 | if len(c.Checkers) > 0 { |
| 168 | var checkers [][]byte |
| 169 | for _, ch := range c.Checkers { |
| 170 | chb, err := json.Marshal(ch) |
| 171 | if err != nil { |
| 172 | return result, err |
| 173 | } |
| 174 | chb = []byte(fmt.Sprintf(`{"type":"%s",%s`, ch.Type(), string(chb[1:]))) |
| 175 | checkers = append(checkers, chb) |
| 176 | } |
| 177 | |
| 178 | allCheckers := []byte{'['} |
| 179 | allCheckers = append([]byte{'['}, bytes.Join(checkers, []byte(","))...) |
| 180 | allCheckers = append(allCheckers, ']') |
| 181 | wrap("checkers", allCheckers) |
| 182 | } |
| 183 | |
| 184 | // Storage |
| 185 | if c.Storage != nil { |
| 186 | sb, err := json.Marshal(c.Storage) |
| 187 | if err != nil { |
| 188 | return result, err |
| 189 | } |
| 190 | sb = []byte(fmt.Sprintf(`{"type":"%s",%s`, c.Storage.Type(), string(sb[1:]))) |
| 191 | wrap("storage", sb) |
| 192 | } |
| 193 | |
| 194 | // Notifiers |
| 195 | if len(c.Notifiers) > 0 { |
| 196 | var checkers [][]byte |
| 197 | for _, ch := range c.Notifiers { |
| 198 | chb, err := json.Marshal(ch) |
| 199 | if err != nil { |