diff --git a/sqlxx/types.go b/sqlxx/types.go index 39954460..dde1028c 100644 --- a/sqlxx/types.go +++ b/sqlxx/types.go @@ -97,7 +97,12 @@ func (ns *NullTime) Scan(value interface{}) error { // MarshalJSON returns m as the JSON encoding of m. func (ns NullTime) MarshalJSON() ([]byte, error) { - return json.Marshal(time.Time(ns)) + var t *time.Time + if !time.Time(ns).IsZero() { + tt := time.Time(ns) + t = &tt + } + return json.Marshal(t) } // UnmarshalJSON sets *m to a copy of data. diff --git a/sqlxx/types_test.go b/sqlxx/types_test.go new file mode 100644 index 00000000..3cb4195c --- /dev/null +++ b/sqlxx/types_test.go @@ -0,0 +1,15 @@ +package sqlxx + +import ( + "encoding/json" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestNullTime(t *testing.T) { + out, err := json.Marshal(NullTime{}) + require.NoError(t, err) + assert.EqualValues(t, "null", string(out)) +}