github.com/wangyougui/gf/v2@v2.6.5/os/gtime/gtime_z_unit_feature_json_test.go (about)

     1  // Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
     2  //
     3  // This Source Code Form is subject to the terms of the MIT License.
     4  // If a copy of the MIT was not distributed with this file,
     5  // You can obtain one at https://github.com/wangyougui/gf.
     6  
     7  package gtime_test
     8  
     9  import (
    10  	"testing"
    11  
    12  	"github.com/wangyougui/gf/v2/frame/g"
    13  	"github.com/wangyougui/gf/v2/internal/json"
    14  	"github.com/wangyougui/gf/v2/os/gtime"
    15  	"github.com/wangyougui/gf/v2/test/gtest"
    16  )
    17  
    18  func Test_Json_Pointer(t *testing.T) {
    19  	// Marshal
    20  	gtest.C(t, func(t *gtest.T) {
    21  		type MyTime struct {
    22  			MyTime *gtime.Time
    23  		}
    24  		b, err := json.Marshal(MyTime{
    25  			MyTime: gtime.NewFromStr("2006-01-02 15:04:05"),
    26  		})
    27  		t.AssertNil(err)
    28  		t.Assert(b, `{"MyTime":"2006-01-02 15:04:05"}`)
    29  	})
    30  	gtest.C(t, func(t *gtest.T) {
    31  		b, err := json.Marshal(g.Map{
    32  			"MyTime": gtime.NewFromStr("2006-01-02 15:04:05"),
    33  		})
    34  		t.AssertNil(err)
    35  		t.Assert(b, `{"MyTime":"2006-01-02 15:04:05"}`)
    36  	})
    37  	gtest.C(t, func(t *gtest.T) {
    38  		b, err := json.Marshal(g.Map{
    39  			"MyTime": *gtime.NewFromStr("2006-01-02 15:04:05"),
    40  		})
    41  		t.AssertNil(err)
    42  		t.Assert(b, `{"MyTime":"2006-01-02 15:04:05"}`)
    43  	})
    44  	// Marshal nil
    45  	gtest.C(t, func(t *gtest.T) {
    46  		type MyTime struct {
    47  			MyTime *gtime.Time
    48  		}
    49  		b, err := json.Marshal(&MyTime{})
    50  		t.AssertNil(err)
    51  		t.Assert(b, `{"MyTime":null}`)
    52  	})
    53  	// Marshal nil with json omitempty
    54  	gtest.C(t, func(t *gtest.T) {
    55  		type MyTime struct {
    56  			MyTime *gtime.Time `json:"time,omitempty"`
    57  		}
    58  		b, err := json.Marshal(&MyTime{})
    59  		t.AssertNil(err)
    60  		t.Assert(b, `{}`)
    61  	})
    62  	// Unmarshal
    63  	gtest.C(t, func(t *gtest.T) {
    64  		var (
    65  			myTime gtime.Time
    66  			err    = json.UnmarshalUseNumber([]byte(`"2006-01-02 15:04:05"`), &myTime)
    67  		)
    68  		t.AssertNil(err)
    69  		t.Assert(myTime.String(), "2006-01-02 15:04:05")
    70  	})
    71  }
    72  
    73  func Test_Json_Struct(t *testing.T) {
    74  	// Marshal struct.
    75  	gtest.C(t, func(t *gtest.T) {
    76  		type MyTime struct {
    77  			MyTime gtime.Time
    78  		}
    79  		b, err := json.Marshal(MyTime{
    80  			MyTime: *gtime.NewFromStr("2006-01-02 15:04:05"),
    81  		})
    82  		t.AssertNil(err)
    83  		t.Assert(b, `{"MyTime":"2006-01-02 15:04:05"}`)
    84  	})
    85  	// Marshal pointer.
    86  	gtest.C(t, func(t *gtest.T) {
    87  		type MyTime struct {
    88  			MyTime gtime.Time
    89  		}
    90  		b, err := json.Marshal(&MyTime{
    91  			MyTime: *gtime.NewFromStr("2006-01-02 15:04:05"),
    92  		})
    93  		t.AssertNil(err)
    94  		t.Assert(b, `{"MyTime":"2006-01-02 15:04:05"}`)
    95  	})
    96  	// Marshal nil
    97  	gtest.C(t, func(t *gtest.T) {
    98  		type MyTime struct {
    99  			MyTime gtime.Time
   100  		}
   101  		b, err := json.Marshal(MyTime{})
   102  		t.AssertNil(err)
   103  		t.Assert(b, `{"MyTime":""}`)
   104  	})
   105  	// Marshal nil omitempty
   106  	gtest.C(t, func(t *gtest.T) {
   107  		type MyTime struct {
   108  			MyTime gtime.Time `json:"time,omitempty"`
   109  		}
   110  		b, err := json.Marshal(MyTime{})
   111  		t.AssertNil(err)
   112  		t.Assert(b, `{"time":""}`)
   113  	})
   114  
   115  }