Skip to content

atomic.Time: add MarshalJSON / UnmarshalJSON#208

Open
c-tonneslan wants to merge 1 commit into
uber-go:masterfrom
c-tonneslan:feat/time-json
Open

atomic.Time: add MarshalJSON / UnmarshalJSON#208
c-tonneslan wants to merge 1 commit into
uber-go:masterfrom
c-tonneslan:feat/time-json

Conversation

@c-tonneslan
Copy link
Copy Markdown

Closes #124.

`atomic.Time` had no JSON methods, so `json.Marshal(t)` returned `"{}"` and any wrapped time silently disappeared on the wire. Other wrapped types in this package (`Duration`, `Bool`, `Pointer`) all have these, so this just fills in the gap.

Implementation matches `duration.go`: delegate to `json.Marshal` / `json.Unmarshal` on the loaded value. I added the methods directly in `time_ext.go` rather than touching the generator template for a single type, but the long-term home is probably re-running `gen-atomicwrapper` with `-json` like the others.

Test plan

```
make test
```

Added `TestTimeJSON` covering `MarshalJSON`, `UnmarshalJSON`, and a struct round-trip that reproduces the issue (so `atomic.Time` inside a struct now serializes as the real time value instead of `{}`).

Changelog entry added under Unreleased / Added.

atomic.Time has no JSON methods, so json.Marshal(t) returns "{}" and the
wrapped time is lost on the wire. Other wrapped types in this package
(Duration, Bool, Pointer) all have these methods. Adding them to Time
fixes uber-go#124.

Implementation mirrors duration.go (delegate to json.Marshal /
json.Unmarshal on the wrapped value). Did this in time_ext.go rather
than the generated time.go so I didn't have to touch the gen-atomic
template just for one type, but the long-term home is probably
re-running gen-atomicwrapper with -json.

Added TestTimeJSON covering MarshalJSON, UnmarshalJSON, and a struct
round-trip that demonstrates the issue from the report.

Signed-off-by: Charlie Tonneslan <cst0520@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

atomic.Time not being marshaled when calling json.Marshal

1 participant