github.com/jxskiss/gopkg/v2@v2.14.9-0.20240514120614-899f3e7952b4/perf/json/json_test.go (about)

     1  package json
     2  
     3  import (
     4  	"math"
     5  	"net"
     6  	"testing"
     7  
     8  	"github.com/stretchr/testify/assert"
     9  )
    10  
    11  type testSSMap map[string]string
    12  
    13  var testStringMap = map[string]string{
    14  	"id":         "1234567",
    15  	"first_name": "Jeanette",
    16  	"last_name":  "Penddreth",
    17  	"email":      "jpenddreth0@census.gov",
    18  	"gender":     "Female",
    19  	"ip_address": "26.58.193.2",
    20  	"html_tag":   "<html></html>",
    21  	"chinese":    "北京欢迎你!",
    22  	`a:\b":"\"c`: `d\"e:f`,
    23  }
    24  
    25  var testStringInterfaceMap = map[string]any{
    26  	"id":         12345,
    27  	"id2":        uint(12345),
    28  	"first_name": "Jeanette",
    29  	"last_name":  "Penddreth",
    30  	"email":      "jpenddreth0@census.gov",
    31  	"gender":     "Female",
    32  	"ip_address": net.ParseIP("26.58.193.2"),
    33  	"html_tag":   "<html></html>",
    34  	"chinese":    []string{"北京欢迎你! ", "Bejing welcome you!\t\n"},
    35  	`a:\b":"\"c`: `d\"e:f`,
    36  	"some_struct": struct {
    37  		A int32  `json:"a_i32,omitempty"`
    38  		B int64  `json:"b_i64,omitempty"`
    39  		C string `json:"c_str,omitempty"`
    40  	}{
    41  		B: 456,
    42  		C: "foobar",
    43  	},
    44  	"some_byte_slice": []byte("北京欢迎你!Bejing welcome hou!"),
    45  
    46  	"int_slice1":  []int{1, 2, 3},
    47  	"int_slice2":  []int32{4, 5, 6},
    48  	"int_slice3":  []int64{7, 8, 9},
    49  	"uint_slice4": []uint8{1, 127, 253},
    50  	"uint_slice5": []uint64{math.MaxUint64},
    51  
    52  	"nil_value1": []int(nil),
    53  	"nil_value2": []uint16(nil),
    54  	"nil_value5": testSSMap(nil),
    55  
    56  	"empty_value1": []int64{},
    57  	"empty_value4": testSSMap{},
    58  
    59  	"typ_ss_map": testSSMap{"a": "1", "b": "2", "c": "3"},
    60  
    61  	"typ_struct_slice": []struct {
    62  		A string `json:"a"`
    63  		B int    `json:"b"`
    64  		C bool   `json:"c"`
    65  	}{
    66  		{"1", 1, true},
    67  		{"2", 2, false},
    68  		{"3", 3, true},
    69  		{"4", 4, false},
    70  	},
    71  
    72  	"bool1": true,
    73  	"bool2": ptr(false),
    74  	"bool3": (*bool)(nil),
    75  
    76  	"integer1": int32(1234),
    77  	"integer2": ptr(int32(1234)),
    78  	"integer3": (*int16)(nil),
    79  
    80  	"slice_fast_nil0": [][]int32(nil),
    81  	"slice_fast_nil1": [][]string(nil),
    82  	"slice_fast_nil2": []map[string]string(nil),
    83  	"slice_fast_nil3": []map[string]any(nil),
    84  
    85  	"slice_fast_typ0": [][]int32{
    86  		{1, 2, 3},
    87  		{4, 5, 6},
    88  	},
    89  	"slice_fast_typ1": [][]string{
    90  		{"a", "b", "c"},
    91  		{"foo", "bar"},
    92  	},
    93  	"slice_fast_typ2": []map[string]string{
    94  		{"a": "1"},
    95  		{"b": "2"},
    96  	},
    97  	"slice_fast_typ3": []map[string]any{
    98  		{"a": "1", "b": 1},
    99  		{"c": "2", "d": 2},
   100  		{"e": int64(3), "d": ptr(uint64(3))},
   101  		{"f": true, "g": ptr(false)},
   102  	},
   103  	"slice_fast_typ4": []map[string]any{},
   104  
   105  	"slice_fast_typ5": []bool{true, false},
   106  	"slice_fast_typ6": []*bool{ptr(true), ptr(false), nil},
   107  	"slice_fast_typ7": []int16{7, 8},
   108  	"slice_fast_typ8": []*int16{ptr(int16(8)), ptr(int16(9)), nil},
   109  }
   110  
   111  func TestStringConversion(t *testing.T) {
   112  	want := testStringMap
   113  	str, err := MarshalToString(testStringMap)
   114  	assert.Nil(t, err)
   115  
   116  	var got map[string]string
   117  	err = UnmarshalFromString(str, &got)
   118  	assert.Nil(t, err)
   119  	assert.Equal(t, want, got)
   120  }
   121  
   122  func TestCompatibility(t *testing.T) {
   123  	stdOutput, err := StdImpl.Marshal(testStringInterfaceMap)
   124  	assert.Nil(t, err)
   125  
   126  	jsoniterOutput, err := DefaultJSONIteratorImpl.Marshal(testStringInterfaceMap)
   127  	assert.Nil(t, err)
   128  	assert.Equal(t, stdOutput, jsoniterOutput)
   129  
   130  	var got1 map[string]any
   131  	var got2 map[string]any
   132  	err = StdImpl.Unmarshal(stdOutput, &got1)
   133  	assert.Nil(t, err)
   134  	err = Unmarshal(jsoniterOutput, &got2)
   135  	assert.Nil(t, err)
   136  	assert.Equal(t, got1, got2)
   137  }
   138  
   139  func ptr[T any](v T) *T {
   140  	return &v
   141  }