Skip to content
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

encoding/json: omitempty does not work with time.Time values #42735

Closed
zosmac opened this issue Nov 20, 2020 · 10 comments
Closed

encoding/json: omitempty does not work with time.Time values #42735

zosmac opened this issue Nov 20, 2020 · 10 comments

Comments

@zosmac
Copy link

zosmac commented Nov 20, 2020

What version of Go are you using (go version)?

$ go version
go version go1.15.5 darwin/amd64

Does this issue reproduce with the latest release?

Yes

What operating system and processor architecture are you using (go env)?

go env Output
```
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/keefe/Library/Caches/go-build"
GOENV="/Users/keefe/Library/Application Support/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOINSECURE=""
GOMODCACHE="/Users/keefe/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Users/keefe/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/5g/n63n_wf97rx7td0yzcj3k5sc0000gn/T/go-build072440525=/tmp/go-build -gno-record-gcc-switches -fno-common"
```

What 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:

"Timestamp": "0001-01-01T00:00:00Z",
package main

import "fmt"
import "time"
import "encoding/json"

type data struct {
	Timestamp time.Time
}
func main() {
	d := data{}
	buf, _ := json.Marshal(d)
	fmt.Printf("%s\n", buf)
}

What did you expect to see?

{}

What did you see instead?

{"Timestamp":"0001-01-01T00:00:00Z"}

@zosmac
Copy link
Author

zosmac commented Nov 20, 2020

I see an old issue from 2014: #5902, that appeared to touch on this but little discussion.

@davecheney
Copy link
Contributor

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

@zosmac
Copy link
Author

zosmac commented Nov 20, 2020

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:

package main

import (
	"fmt"
	"time"
	"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>

@zosmac
Copy link
Author

zosmac commented Nov 20, 2020 via email

@davecheney
Copy link
Contributor

Thank you for clarifying. Would you consider renaming your issue to something like “encoding/json: omitempty does not work with time.Time” values?

@davecheney
Copy link
Contributor

Although it is closed, #4357 (comment), seems to capture your issue.

@zosmac
Copy link
Author

zosmac commented Nov 20, 2020 via email

@zosmac
Copy link
Author

zosmac commented Nov 20, 2020 via email

@zosmac zosmac changed the title return nil or zero length []byte slice from time.MarshalJSON if time.IsZero to honor json tag omitempty encoding/json: omitempty does not work with time.Time values Nov 20, 2020
@mvdan
Copy link
Member

mvdan commented Nov 20, 2020

Closing as a dup of #11939.

@mvdan mvdan closed this as completed Nov 20, 2020
@zosmac
Copy link
Author

zosmac commented Nov 20, 2020 via email

@golang golang locked and limited conversation to collaborators Nov 20, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants