-
Notifications
You must be signed in to change notification settings - Fork 18.8k
Closed as not planned
Labels
Milestone
Description
Proposal Details
I propose enhancing the time.Duration type with MarshalJSON and UnmarshalJSON functions to enable direct marshaling and unmarshaling with standard JSON library. Go developers already benefit from convenient marshaling and unmarshaling capabilities for time.Time. Extending this convenience to time.Duration could significantly improve developer experience, especially in configuration and data interchange scenarios where duration representations are common.
This proposal revisits an earlier suggestion (#16039) made in 2016, which was not adopted for two main reasons:
- Concern about JSON Specification Compatibility:The initial rejection stated that the JSON specification does not explicitly support duration types. However, this overlooks the fact that the
time.Timetype is effectively marshaled and unmarshaled despite the lack of a native datetime type in JSON. This inconsistency suggests that enhancingtime.Durationin a similar manner would align with the existing practice and not breach the principles of JSON handling in Go. - Concern about Adding Complexity to the Time Package: The previous feedback mentioned concerns about introducing JSON-aware functionality into the relatively low-level time package, potentially complicating its interface. While it is a valid consideration, these interfaces are redundant as
time.Timealready supports them. Therefore, adding them would align with the existing practice.
Adding marshaling and unmarshaling support to time.Duration would reduce boilerplate code and streamline JSON configuration and data exchange.
Proposed implementation:
func (d Duration) MarshalJSON() ([]byte, error) {
return json.Marshal(d.String())
}
func (d *Duration) UnmarshalJSON(b []byte) error {
var v any
if err := json.Unmarshal(b, &v); err != nil {
return err
}
switch t := v.(type) {
case string:
tmp, err := ParseDuration(t)
if err != nil {
return err
}
*d = tmp
return nil
default:
return errors.New("invalid duration")
}
}pavelnikolov and todaychi