-
Notifications
You must be signed in to change notification settings - Fork 34
/
jsontime_test.go
44 lines (40 loc) · 890 Bytes
/
jsontime_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
package mapper
import (
"encoding/json"
"testing"
"time"
)
type Person struct {
Id int64 `json:"id"`
Name string `json:"name"`
Birthday JSONTime `json:"birthday"`
}
func TestTimeJson(t *testing.T) {
now := JSONTime(time.Now())
t.Log(now)
src := `{"id":5,"name":"xiaoming","birthday":"2016-06-30 16:09:51"}`
p := new(Person)
err := json.Unmarshal([]byte(src), p)
if err != nil {
t.Fatal(err)
}
t.Log(p)
t.Log(time.Time(p.Birthday))
js, _ := json.Marshal(p)
t.Log(string(js))
}
func TestTimeJson_NewFormat(t *testing.T) {
now := JSONTime(time.Now())
t.Log(now)
SetTimeJSONFormat("2006-01-02T15:04:05")
src := `{"id":5,"name":"xiaoming","birthday":"2016-06-30T16:09:51"}`
p := new(Person)
err := json.Unmarshal([]byte(src), p)
if err != nil {
t.Fatal(err)
}
t.Log(p)
t.Log(time.Time(p.Birthday))
js, _ := json.Marshal(p)
t.Log(string(js))
}