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
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.Time type is effectively marshaled and unmarshaled despite the lack of a native datetime type in JSON. This inconsistency suggests that enhancing time.Duration in 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.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.
gearnode
changed the title
proposal: time: add MarshalText and UnmarshalText support
proposal: time: add MarshalJSON and UnmarshalJSON support
Feb 3, 2024
gearnode
changed the title
proposal: time: add MarshalJSON and UnmarshalJSON support
proposal: time: add MarshalJSON and UnmarshalJSON support
Feb 3, 2024
The policy for packages maintained by the Go project is to only allow the addition of marshaling functions if no existing, reasonable marshaling exists.
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: