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: can't omit empty value with type time.Time while encoding struct to json payload? #15719

Closed
thinkhy opened this issue May 17, 2016 · 1 comment

Comments

Projects
None yet
3 participants
@thinkhy
Copy link

commented May 17, 2016

Please answer these questions before submitting your issue. Thanks!

  1. What version of Go are you using (go version)?
    go version go1.6 darwin/amd64
  2. What operating system and processor architecture are you using (go env)?
    GOARCH="amd64"
    GOBIN=""
    GOEXE=""
    GOHOSTARCH="amd64"
    GOHOSTOS="darwin"
    GOOS="darwin"
    GOPATH="/Users/thinkhy/go"
    GORACE=""
    GOROOT="/usr/local/Cellar/go/1.6/libexec"
    GOTOOLDIR="/usr/local/Cellar/go/1.6/libexec/pkg/tool/darwin_amd64"
    GO15VENDOREXPERIMENT="1"
    CC="clang"
    GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fno-common"
    CXX="clang++"
    CGO_ENABLED="1"
  3. What did you do?
    If possible, provide a recipe for reproducing the error.
    A complete runnable program is good.
    A link on play.golang.org is best.
import (
        "encoding/json"
        "time"
        "os"
)


type Response struct {
        Name     string    `json:"name,omitempty"`
        Counter  int       `json:"counter,omitempty"`
        CreateAt time.Time `json:"createat,omitempty"`
}


func main() {
        var r Response

        enc := json.NewEncoder(os.Stdout)
        enc.Encode(r)
}
  1. What did you expect to see?

Seems the expected result should be "{}" because the field createat is empty with ZERO value.

  1. What did you see instead?

The above code will output:

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

@thinkhy thinkhy changed the title How to omit empty value with type time.Time when encoding struct to json payload? encoding/json: can't omit empty value with type time.Time while encoding struct to json payload? May 17, 2016

@bradfitz

This comment has been minimized.

Copy link
Member

commented May 17, 2016

Dup of #4357, etc

@bradfitz bradfitz closed this May 17, 2016

@golang golang locked and limited conversation to collaborators May 17, 2017

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
You can’t perform that action at this time.