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