You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
If you need to support custom types (e.g. for custom time formats), you'll need to implement the json.Marshaler and json.Unmarshaler interfaces on the type.
349
+
350
+
```go
351
+
// MyTimeFormat is a custom format I invented for fun
352
+
constMyTimeFormat = "The time is 15:04:05. The year is 2006, and it is day 2 of January."
353
+
354
+
// MyTime is a custom type used to handle the custom time format
355
+
typeMyTimestruct {
356
+
time.Time
357
+
}
358
+
359
+
// UnmarshalJSON to implement the json.Unmarshaler interface
360
+
func(m *MyTime) UnmarshalJSON(b []byte) error {
361
+
t, err:= time.Parse(MyTimeFormat, string(b))
362
+
if err != nil {
363
+
return err
364
+
}
365
+
366
+
m.Time = t
367
+
368
+
returnnil
369
+
}
370
+
371
+
// MarshalJSON to implement the json.Marshaler interface
372
+
func(m *MyTime) MarshalJSON() ([]byte, error) {
373
+
return json.Marshal(m.Time.Format(MyTimeFormat))
374
+
}
375
+
```
376
+
346
377
### Errors
347
378
This package also implements support for JSON API compatible `errors` payloads using the following types.
0 commit comments