Skip to content

Commit

Permalink
Merge pull request #30 from nicholas-wright-bjss/master
Browse files Browse the repository at this point in the history
Fix for nil *time.Time serialisation
  • Loading branch information
kungfusheep authored Aug 15, 2023
2 parents 1ae2e35 + 4071220 commit 293c1dd
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 12 deletions.
22 changes: 13 additions & 9 deletions jingo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,10 +314,12 @@ func BenchmarkUnicodeLargeStdLib(b *testing.B) {
}

type TimeObject struct {
Time time.Time `json:"time"`
PtrTime *time.Time `json:"ptrTime"`
SliceTime []time.Time `json:"sliceTime"`
PtrSliceTime []*time.Time `json:"ptrSliceTime"`
Time time.Time `json:"time"`
PtrTime *time.Time `json:"ptrTime"`
NullPtrTime *time.Time `json:"nullPtrTime"`
SliceTime []time.Time `json:"sliceTime"`
PtrSliceTime []*time.Time `json:"ptrSliceTime"`
NullPtrSliceTime []*time.Time `json:"nullPtrSliceTime"`
}

func Test_Time(t *testing.T) {
Expand All @@ -328,13 +330,15 @@ func Test_Time(t *testing.T) {
d3 := time.Date(2003, 9, 17, 20, 4, 26, 0, time.UTC)

to := TimeObject{
Time: d0,
PtrTime: &d1,
SliceTime: []time.Time{d2},
PtrSliceTime: []*time.Time{&d3},
Time: d0,
PtrTime: &d1,
NullPtrTime: nil,
SliceTime: []time.Time{d2},
PtrSliceTime: []*time.Time{&d3},
NullPtrSliceTime: []*time.Time{nil},
}

wantJSON := `{"time":"2000-09-17T20:04:26Z","ptrTime":"2001-09-17T20:04:26Z","sliceTime":["2002-09-17T20:04:26Z"],"ptrSliceTime":["2003-09-17T20:04:26Z"]}`
wantJSON := `{"time":"2000-09-17T20:04:26Z","ptrTime":"2001-09-17T20:04:26Z","nullPtrTime":null,"sliceTime":["2002-09-17T20:04:26Z"],"ptrSliceTime":["2003-09-17T20:04:26Z"],"nullPtrSliceTime":[null]}`

var enc = NewStructEncoder(TimeObject{})

Expand Down
4 changes: 1 addition & 3 deletions structencoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,7 @@ func NewStructEncoder(t interface{}) *StructEncoder {
e.val(ptrTimeToBuf)
e.chunk(`"`)
case e.f.Type.Kind() == reflect.Ptr && timeType == reflect.TypeOf(e.t).Field(e.i).Type.Elem():
e.chunk(`"`)
e.ptrval(ptrTimeToBuf)
e.chunk(`"`)
e.ptrstringval(ptrTimeToBuf)

// write the value instruction depending on type
case e.f.Type.Kind() == reflect.Ptr:
Expand Down

0 comments on commit 293c1dd

Please sign in to comment.