Implementation of Go issue #11939 that uses memory hack known as "monkey patching" presented by Bouke van der Bijl.
go get github.com/lokhman/json-zeroer
package main
import (
"encoding/json"
"time"
_ "github.com/lokhman/json-zeroer"
)
type Object struct {
X string `json:"x,omitempty"`
Y time.Time `json:"y,omitempty"`
Z time.Time `json:"z,omitempty"`
}
func main() {
o := Object{Y: time.Now()}
data, err := json.Marshal(o)
if err != nil {
panic(err)
}
println(string(data)) // {"y":"2018-03-15T19:44:50Z"}
}
See examples
for more examples.
Use go test
for testing.
Library should work in Linux and Windows OS both x32 and x64. As library uses direct memory access, I would recommend to use it with caution. Other limitations are listed here: https://github.com/bouk/monkey#notes.
Library is available under the MIT license. The included LICENSE file describes this in detail.