github.com/bingoohuang/gg@v0.0.0-20240325092523-45da7dee9335/pkg/jsoni/api_tests/config_test.go (about) 1 package test 2 3 import ( 4 "context" 5 "encoding/json" 6 "testing" 7 8 "github.com/bingoohuang/gg/pkg/jsoni" 9 "github.com/stretchr/testify/require" 10 ) 11 12 func Test_use_number_for_unmarshal(t *testing.T) { 13 should := require.New(t) 14 api := jsoni.Config{UseNumber: true}.Froze() 15 var obj interface{} 16 should.Nil(api.UnmarshalFromString(nil, "123", &obj)) 17 should.Equal(json.Number("123"), obj) 18 } 19 20 func Test_customize_float_marshal(t *testing.T) { 21 should := require.New(t) 22 json := jsoni.Config{MarshalFloatWith6Digits: true}.Froze() 23 str, err := json.MarshalToString(nil, float32(1.23456789)) 24 should.Nil(err) 25 should.Equal("1.234568", str) 26 } 27 28 func Test_customize_tag_key(t *testing.T) { 29 type TestObject struct { 30 Field string `orm:"field"` 31 } 32 33 should := require.New(t) 34 json := jsoni.Config{TagKey: "orm"}.Froze() 35 str, err := json.MarshalToString(nil, TestObject{"hello"}) 36 should.Nil(err) 37 should.Equal(`{"field":"hello"}`, str) 38 } 39 40 func Test_read_large_number_as_interface(t *testing.T) { 41 should := require.New(t) 42 var val interface{} 43 err := jsoni.Config{UseNumber: true}.Froze().UnmarshalFromString(context.Background(), `123456789123456789123456789`, &val) 44 should.Nil(err) 45 output, err := jsoni.MarshalToString(val) 46 should.Nil(err) 47 should.Equal(`123456789123456789123456789`, output) 48 } 49 50 type caseSensitiveStruct struct { 51 A string `json:"a"` 52 B string `json:"b,omitempty"` 53 C *C `json:"C,omitempty"` 54 } 55 56 type C struct { 57 D int64 `json:"D,omitempty"` 58 E *E `json:"e,omitempty"` 59 } 60 61 type E struct { 62 F string `json:"F,omitempty"` 63 } 64 65 func Test_CaseSensitive(t *testing.T) { 66 should := require.New(t) 67 68 testCases := []struct { 69 input string 70 expectedOutput string 71 caseSensitive bool 72 }{ 73 { 74 input: `{"A":"foo","B":"bar"}`, 75 expectedOutput: `{"a":"foo","b":"bar"}`, 76 caseSensitive: false, 77 }, 78 { 79 input: `{"a":"foo","b":"bar"}`, 80 expectedOutput: `{"a":"foo","b":"bar"}`, 81 caseSensitive: true, 82 }, 83 { 84 input: `{"a":"foo","b":"bar","C":{"D":10}}`, 85 expectedOutput: `{"a":"foo","b":"bar","C":{"D":10}}`, 86 caseSensitive: true, 87 }, 88 { 89 input: `{"a":"foo","B":"bar","c":{"d":10}}`, 90 expectedOutput: `{"a":"foo"}`, 91 caseSensitive: true, 92 }, 93 { 94 input: `{"a":"foo","C":{"d":10}}`, 95 expectedOutput: `{"a":"foo","C":{}}`, 96 caseSensitive: true, 97 }, 98 { 99 input: `{"a":"foo","C":{"D":10,"e":{"f":"baz"}}}`, 100 expectedOutput: `{"a":"foo","C":{"D":10,"e":{}}}`, 101 caseSensitive: true, 102 }, 103 { 104 input: `{"a":"foo","C":{"D":10,"e":{"F":"baz"}}}`, 105 expectedOutput: `{"a":"foo","C":{"D":10,"e":{"F":"baz"}}}`, 106 caseSensitive: true, 107 }, 108 { 109 input: `{"A":"foo","c":{"d":10,"E":{"f":"baz"}}}`, 110 expectedOutput: `{"a":"foo","C":{"D":10,"e":{"F":"baz"}}}`, 111 caseSensitive: false, 112 }, 113 } 114 for _, tc := range testCases { 115 val := caseSensitiveStruct{} 116 err := jsoni.Config{CaseSensitive: tc.caseSensitive}.Froze().UnmarshalFromString(nil, tc.input, &val) 117 should.Nil(err) 118 119 output, err := jsoni.MarshalToString(val) 120 should.Nil(err) 121 should.Equal(tc.expectedOutput, output) 122 } 123 } 124 125 type structWithElevenFields struct { 126 A string `json:"A,omitempty"` 127 B string `json:"B,omitempty"` 128 C string `json:"C,omitempty"` 129 D string `json:"d,omitempty"` 130 E string `json:"e,omitempty"` 131 F string `json:"f,omitempty"` 132 G string `json:"g,omitempty"` 133 H string `json:"h,omitempty"` 134 I string `json:"i,omitempty"` 135 J string `json:"j,omitempty"` 136 K string `json:"k,omitempty"` 137 } 138 139 func Test_CaseSensitive_MoreThanTenFields(t *testing.T) { 140 should := require.New(t) 141 142 testCases := []struct { 143 input string 144 expectedOutput string 145 caseSensitive bool 146 }{ 147 { 148 input: `{"A":"1","B":"2","C":"3","d":"4","e":"5","f":"6","g":"7","h":"8","i":"9","j":"10","k":"11"}`, 149 expectedOutput: `{"A":"1","B":"2","C":"3","d":"4","e":"5","f":"6","g":"7","h":"8","i":"9","j":"10","k":"11"}`, 150 caseSensitive: true, 151 }, 152 { 153 input: `{"a":"1","b":"2","c":"3","D":"4","E":"5","F":"6"}`, 154 expectedOutput: `{"A":"1","B":"2","C":"3","d":"4","e":"5","f":"6"}`, 155 caseSensitive: false, 156 }, 157 { 158 input: `{"A":"1","b":"2","d":"4","E":"5"}`, 159 expectedOutput: `{"A":"1","d":"4"}`, 160 caseSensitive: true, 161 }, 162 } 163 for _, tc := range testCases { 164 val := structWithElevenFields{} 165 err := jsoni.Config{CaseSensitive: tc.caseSensitive}.Froze().UnmarshalFromString(nil, tc.input, &val) 166 should.Nil(err) 167 168 output, err := jsoni.MarshalToString(val) 169 should.Nil(err) 170 should.Equal(tc.expectedOutput, output) 171 } 172 } 173 174 type onlyTaggedFieldStruct struct { 175 A string `json:"a"` 176 B string 177 FSimpl F `json:"f_simpl"` 178 ISimpl I 179 FPtr *F `json:"f_ptr"` 180 IPtr *I 181 F 182 *I 183 } 184 185 type F struct { 186 G string `json:"g"` 187 H string 188 } 189 190 type I struct { 191 J string `json:"j"` 192 K string 193 } 194 195 func Test_OnlyTaggedField(t *testing.T) { 196 should := require.New(t) 197 198 obj := onlyTaggedFieldStruct{ 199 A: "a", 200 B: "b", 201 FSimpl: F{G: "g", H: "h"}, 202 ISimpl: I{J: "j", K: "k"}, 203 FPtr: &F{G: "g", H: "h"}, 204 IPtr: &I{J: "j", K: "k"}, 205 F: F{G: "g", H: "h"}, 206 I: &I{J: "j", K: "k"}, 207 } 208 output, err := jsoni.Config{OnlyTaggedField: true}.Froze().Marshal(nil, obj) 209 should.Nil(err) 210 211 m := make(map[string]interface{}) 212 err = jsoni.Unmarshal(output, &m) 213 should.Nil(err) 214 215 should.Equal(map[string]interface{}{ 216 "a": "a", 217 "f_simpl": map[string]interface{}{ 218 "g": "g", 219 }, 220 "f_ptr": map[string]interface{}{ 221 "g": "g", 222 }, 223 "g": "g", 224 "j": "j", 225 }, m) 226 }