github.com/wangyougui/gf/v2@v2.6.5/container/gvar/gvar_z_unit_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 gvar_test
     8  
     9  import (
    10  	"math"
    11  	"testing"
    12  
    13  	"github.com/wangyougui/gf/v2/container/gvar"
    14  	"github.com/wangyougui/gf/v2/internal/json"
    15  	"github.com/wangyougui/gf/v2/test/gtest"
    16  )
    17  
    18  func TestVar_Json(t *testing.T) {
    19  	// Marshal
    20  	gtest.C(t, func(t *gtest.T) {
    21  		s := "i love gf"
    22  		v := gvar.New(s)
    23  		b1, err1 := json.Marshal(v)
    24  		b2, err2 := json.Marshal(s)
    25  		t.Assert(err1, err2)
    26  		t.Assert(b1, b2)
    27  	})
    28  
    29  	gtest.C(t, func(t *gtest.T) {
    30  		s := int64(math.MaxInt64)
    31  		v := gvar.New(s)
    32  		b1, err1 := json.Marshal(v)
    33  		b2, err2 := json.Marshal(s)
    34  		t.Assert(err1, err2)
    35  		t.Assert(b1, b2)
    36  	})
    37  
    38  	// Unmarshal
    39  	gtest.C(t, func(t *gtest.T) {
    40  		s := "i love gf"
    41  		v := gvar.New(nil)
    42  		b, err := json.Marshal(s)
    43  		t.AssertNil(err)
    44  
    45  		err = json.UnmarshalUseNumber(b, v)
    46  		t.AssertNil(err)
    47  		t.Assert(v.String(), s)
    48  	})
    49  
    50  	gtest.C(t, func(t *gtest.T) {
    51  		var v gvar.Var
    52  		s := "i love gf"
    53  		b, err := json.Marshal(s)
    54  		t.AssertNil(err)
    55  
    56  		err = json.UnmarshalUseNumber(b, &v)
    57  		t.AssertNil(err)
    58  		t.Assert(v.String(), s)
    59  	})
    60  }