-
Notifications
You must be signed in to change notification settings - Fork 318
/
issue_recurse_test.go
61 lines (49 loc) · 1.32 KB
/
issue_recurse_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package issue_test
import (
`encoding/json`
`fmt`
`reflect`
`strconv`
`testing`
`time`
`github.com/bytedance/sonic`
`github.com/davecgh/go-spew/spew`
`github.com/stretchr/testify/require`
)
func TestPointerValueRecurseMarshal(t *testing.T) {
info := &TestStruct1{
StartTime: JSONTime(time.Now()),
}
infos := &[]*TestStruct1{info}
bytes, err1 := json.Marshal(infos)
fmt.Printf("%+v\n", string(bytes))
spew.Dump(bytes, err1)
jbytes, err2 := sonic.Marshal(infos)
fmt.Printf("%+v\n", string(jbytes))
spew.Dump(jbytes, err2)
require.Equal(t, bytes, jbytes)
}
func TestPointerValueRecursePretouch(t *testing.T) {
info := &TestStruct2{
StartTime: JSONTime(time.Now()),
}
infos := &[]*TestStruct2{info}
bytes, err1 := json.Marshal(infos)
fmt.Printf("%+v\n", string(bytes))
spew.Dump(bytes, err1)
sonic.Pretouch(reflect.TypeOf(infos))
jbytes, err2 := sonic.Marshal(infos)
fmt.Printf("%+v\n", string(jbytes))
spew.Dump(jbytes, err2)
require.Equal(t, bytes, jbytes)
}
type TestStruct1 struct {
StartTime JSONTime
}
type TestStruct2 struct {
StartTime JSONTime
}
type JSONTime time.Time
func (t *JSONTime) MarshalJSON() ([]byte, error) {
return []byte(strconv.FormatInt(time.Time(*t).Unix(), 10)), nil
}