proposal: time: add MarshalJSON
and UnmarshalJSON
support
#65501
Labels
Milestone
MarshalJSON
and UnmarshalJSON
support
#65501
Proposal Details
I propose enhancing the
time.Duration
type withMarshalJSON
andUnmarshalJSON
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:
time.Time
type is effectively marshaled and unmarshaled despite the lack of a native datetime type in JSON. This inconsistency suggests that enhancingtime.Duration
in a similar manner would align with the existing practice and not breach the principles of JSON handling in Go.time.Time
already 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:
The text was updated successfully, but these errors were encountered: