gf/os/gtime/gtime_z_unit_feature_json_test.go
2022-01-19 18:23:04 +08:00

116 lines
2.7 KiB
Go

// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
package gtime_test
import (
"testing"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/internal/json"
"github.com/gogf/gf/v2/os/gtime"
"github.com/gogf/gf/v2/test/gtest"
)
func Test_Json_Pointer(t *testing.T) {
// Marshal
gtest.C(t, func(t *gtest.T) {
type MyTime struct {
MyTime *gtime.Time
}
b, err := json.Marshal(MyTime{
MyTime: gtime.NewFromStr("2006-01-02 15:04:05"),
})
t.AssertNil(err)
t.Assert(b, `{"MyTime":"2006-01-02 15:04:05"}`)
})
gtest.C(t, func(t *gtest.T) {
b, err := json.Marshal(g.Map{
"MyTime": gtime.NewFromStr("2006-01-02 15:04:05"),
})
t.AssertNil(err)
t.Assert(b, `{"MyTime":"2006-01-02 15:04:05"}`)
})
gtest.C(t, func(t *gtest.T) {
b, err := json.Marshal(g.Map{
"MyTime": *gtime.NewFromStr("2006-01-02 15:04:05"),
})
t.AssertNil(err)
t.Assert(b, `{"MyTime":"2006-01-02 15:04:05"}`)
})
// Marshal nil
gtest.C(t, func(t *gtest.T) {
type MyTime struct {
MyTime *gtime.Time
}
b, err := json.Marshal(&MyTime{})
t.AssertNil(err)
t.Assert(b, `{"MyTime":null}`)
})
// Marshal nil with json omitempty
gtest.C(t, func(t *gtest.T) {
type MyTime struct {
MyTime *gtime.Time `json:"time,omitempty"`
}
b, err := json.Marshal(&MyTime{})
t.AssertNil(err)
t.Assert(b, `{}`)
})
// Unmarshal
gtest.C(t, func(t *gtest.T) {
var (
myTime gtime.Time
err = json.UnmarshalUseNumber([]byte(`"2006-01-02 15:04:05"`), &myTime)
)
t.AssertNil(err)
t.Assert(myTime.String(), "2006-01-02 15:04:05")
})
}
func Test_Json_Struct(t *testing.T) {
// Marshal struct.
gtest.C(t, func(t *gtest.T) {
type MyTime struct {
MyTime gtime.Time
}
b, err := json.Marshal(MyTime{
MyTime: *gtime.NewFromStr("2006-01-02 15:04:05"),
})
t.AssertNil(err)
t.Assert(b, `{"MyTime":"2006-01-02 15:04:05"}`)
})
// Marshal pointer.
gtest.C(t, func(t *gtest.T) {
type MyTime struct {
MyTime gtime.Time
}
b, err := json.Marshal(&MyTime{
MyTime: *gtime.NewFromStr("2006-01-02 15:04:05"),
})
t.AssertNil(err)
t.Assert(b, `{"MyTime":"2006-01-02 15:04:05"}`)
})
// Marshal nil
gtest.C(t, func(t *gtest.T) {
type MyTime struct {
MyTime gtime.Time
}
b, err := json.Marshal(MyTime{})
t.AssertNil(err)
t.Assert(b, `{"MyTime":""}`)
})
// Marshal nil omitempty
gtest.C(t, func(t *gtest.T) {
type MyTime struct {
MyTime gtime.Time `json:"time,omitempty"`
}
b, err := json.Marshal(MyTime{})
t.AssertNil(err)
t.Assert(b, `{"time":""}`)
})
}