-
Notifications
You must be signed in to change notification settings - Fork 18k
encoding/json: omitempty does not work with time.Time values #42735
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
I see an old issue from 2014: #5902, that appeared to touch on this but little discussion. |
Thank you for raising this issue. I would argue that the code above is not correct as it is ignoring the error value. The general approach to error handling in the majority of Go code is, if there is an error, the state of any other return values is undefined, and shouldn’t be consulted if err != nil |
The following updated example code clarifies the intent of using the json omitempty tag and verifies that a Zero time value is encoded without error:
Returns:
|
Dave,
Thank you for your prompt reply.
When I run an updated version of the sample code, with the error return value captured, it shows that the time Zero value is Marshaled without error. I have added this updated sample to the issue.
Have a nice evening.
Keefe
package main
import "fmt"
import "time"
import "encoding/json"
type data struct {
Timestamp time.Time `json:",omitempty"`
}
func main() {
d := data{}
buf, err := json.Marshal(d)
fmt.Printf("%s %v\n", buf, err)
}
Returns:
{"Timestamp":"0001-01-01T00:00:00Z"} <nil>
On Nov 19, 2020, at 9:11 PM, Dave Cheney <notifications@github.com> wrote:
Thank you for raising this issue. I would argue that the code above is not correct as it is ignoring the error value. The general approach to error handling in the majority of Go code is, if there is an error, the state of any other return values is undefined, and shouldn’t be consulted if err != nil
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub <#42735 (comment)>, or unsubscribe <https://github.com/notifications/unsubscribe-auth/ABEKCQVAII3VUFQ2NW7U2TTSQXF5NANCNFSM4T4DWVZA>.
|
Thank you for clarifying. Would you consider renaming your issue to something like “encoding/json: omitempty does not work with time.Time” values? |
Although it is closed, #4357 (comment), seems to capture your issue. |
Thank you for this link. It references another couple of issues, including this lengthy one #11939 which will take me a bit to digest. A lot of good discussion there.
On Nov 19, 2020, at 9:33 PM, Dave Cheney <notifications@github.com> wrote:
Although it is closed, #4357 (comment) <#4357 (comment)>, seems to capture your issue.
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub <#42735 (comment)>, or unsubscribe <https://github.com/notifications/unsubscribe-auth/ABEKCQUXXBSEUZJVQV2UNUTSQXIPBANCNFSM4T4DWVZA>.
|
This phrasing is much clearer. Thank you for the excellent suggestion.
On Nov 19, 2020, at 9:31 PM, Dave Cheney <notifications@github.com> wrote:
encoding/json: omitempty does not work with time.Time” values
|
Closing as a dup of #11939. |
Daniel, Dave,
Thank you for your guidance here, I appreciate your input.
I will plug into #11939 to lend my support to the discussion.
Take care and stay safe.
Sent from Keefe’s iPad
On Nov 20, 2020, at 5:26 AM, Daniel Martí <notifications@github.com> wrote:
Closing as a dup of #11939.
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub, or unsubscribe.
|
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
Yes
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
Added
json:",omitempty"
tag to a time.Time field of a structure. Was hoping to omit the value from JSON when the time value is uninitialized. Got this instead:What did you expect to see?
{}
What did you see instead?
{"Timestamp":"0001-01-01T00:00:00Z"}
The text was updated successfully, but these errors were encountered: