File tree 4 files changed +97
-4
lines changed 4 files changed +97
-4
lines changed Original file line number Diff line number Diff line change @@ -26,12 +26,17 @@ TakePointerValue: //取指针的值
26
26
}
27
27
}
28
28
29
- if typeOf . String () == timeType { //是time.Time类型或者底层是time.Time类型
29
+ if valueOf . CanConvert ( timeTypes ) { //是time.Time类型或者底层是time.Time类型
30
30
t .Key = key
31
31
t .Val = valueOf .Interface ()
32
- return
33
32
}
34
33
34
+ //if typeOf.String() == timeType { //是time.Time类型或者底层是time.Time类型
35
+ // t.Key = key
36
+ // t.Val = valueOf.Interface()
37
+ // return
38
+ //}
39
+
35
40
if typeOf .NumField () == 0 { //如果是一个struct{}{}类型的字段或者是一个空的自定义结构体编码为{}
36
41
t .Key = key
37
42
t .Val = struct {}{}
Original file line number Diff line number Diff line change 1
1
package filter
2
2
3
3
import (
4
+ "fmt"
4
5
"reflect"
6
+ "time"
5
7
)
6
8
7
- var nilSlice = make ([]int , 0 , 0 )
9
+ var (
10
+ nilSlice = make ([]int , 0 , 0 )
11
+ timeTypes = reflect .TypeOf (time .Now ())
12
+ )
8
13
9
14
const (
10
15
timeType = "time.Time"
@@ -33,12 +38,19 @@ TakePointerValue: //取指针的值
33
38
}
34
39
}
35
40
36
- if typeOf .String () == timeType { //是time.Time类型或者底层是time.Time类型
41
+ fmt .Println (valueOf .CanConvert (timeTypes ))
42
+ if valueOf .CanConvert (timeTypes ) { //是time.Time类型或者底层是time.Time类型
37
43
t .Key = key
38
44
t .Val = valueOf .Interface ()
39
45
return
40
46
}
41
47
48
+ //if typeOf.String() == timeType { //是time.Time类型或者底层是time.Time类型
49
+ // t.Key = key
50
+ // t.Val = valueOf.Interface()
51
+ // return
52
+ //}
53
+
42
54
if typeOf .NumField () == 0 { //如果是一个struct{}{}类型的字段或者是一个空的自定义结构体编码为{}
43
55
t .Key = key
44
56
t .Val = struct {}{}
Original file line number Diff line number Diff line change
1
+ package filter
2
+
3
+ import (
4
+ "fmt"
5
+ "time"
6
+ )
7
+
8
+ type Time time.Time
9
+
10
+ const timeFmt = "2006-01-02 15:04:05"
11
+
12
+ func (t Time ) MarshalJSON () ([]byte , error ) {
13
+ fmtTime := time .Time (t )
14
+ formatted := fmt .Sprintf ("\" %s\" " , fmtTime .Format (timeFmt ))
15
+ return []byte (formatted ), nil
16
+ }
17
+
18
+ func (t * Time ) UnmarshalJSON (data []byte ) error {
19
+ if string (data ) == "null" {
20
+ return nil
21
+ }
22
+ var err error
23
+ var timeTmp time.Time
24
+ timeTmp , err = time .Parse (`"` + timeFmt + `"` , string (data ))
25
+ * t = Time (timeTmp )
26
+ return err
27
+ }
28
+
29
+ func (t Time ) String () string {
30
+ return time .Time (t ).Format (timeFmt )
31
+ }
Original file line number Diff line number Diff line change
1
+ package filter
2
+
3
+ import (
4
+ "encoding/json"
5
+ "fmt"
6
+ "reflect"
7
+ "testing"
8
+ "time"
9
+ )
10
+
11
+ type UserTime struct {
12
+ ID int `json:"id,select(list)"`
13
+ BirthTime Time `json:"birth_time,select(list)"`
14
+ BirthTime2 * Time `json:"birth_time2,select(list)"`
15
+ Timer time.Time `json:"timer,select($any)"`
16
+ }
17
+
18
+ func TestTime (t * testing.T ) {
19
+ now := time .Now ()
20
+ user := UserTime {
21
+ ID : 111 ,
22
+ BirthTime : Time (now ),
23
+ BirthTime2 : (* Time )(& now ),
24
+ Timer : time .Now (),
25
+ }
26
+ v := reflect .ValueOf (Time {})
27
+ val , ok := v .Interface ().(json.Marshaler )
28
+ fmt .Println (val , ok )
29
+
30
+ fmt .Println (SelectMarshal ("list" , user ).MustJSON ())
31
+
32
+ u2 := UserTime {}
33
+ marshal , err := json .Marshal (user )
34
+ if err != nil {
35
+ panic (err )
36
+ }
37
+
38
+ err = json .Unmarshal (marshal , & u2 )
39
+ if err != nil {
40
+ panic (err )
41
+ }
42
+
43
+ fmt .Println (u2 )
44
+ fmt .Println (u2 .BirthTime )
45
+ }
You can’t perform that action at this time.
0 commit comments