Skip to content

Commit

Permalink
fix(sqlxx): improve nulltime json unmarshal (#195)
Browse files Browse the repository at this point in the history
  • Loading branch information
aeneasr authored Aug 31, 2020
1 parent a15b885 commit 902fc12
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
7 changes: 6 additions & 1 deletion sqlxx/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
15 changes: 15 additions & 0 deletions sqlxx/types_test.go
Original file line number Diff line number Diff line change
@@ -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))
}

0 comments on commit 902fc12

Please sign in to comment.