|
| 1 | +package util |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | +) |
| 6 | + |
| 7 | +type TestStruct struct { |
| 8 | + Name string `json:"name"` |
| 9 | + Age int `json:"age"` |
| 10 | + Email string `json:"email"` |
| 11 | + Active bool `json:"active"` |
| 12 | + Details struct { |
| 13 | + City string `json:"city"` |
| 14 | + Country string `json:"country"` |
| 15 | + } `json:"details"` |
| 16 | +} |
| 17 | + |
| 18 | +func TestJSON_Struct2Json(t *testing.T) { |
| 19 | + tests := []struct { |
| 20 | + name string |
| 21 | + obj interface{} |
| 22 | + expectErr bool |
| 23 | + }{ |
| 24 | + { |
| 25 | + "正常结构体", |
| 26 | + TestStruct{ |
| 27 | + Name: "John Doe", |
| 28 | + Age: 30, |
| 29 | + |
| 30 | + Active: true, |
| 31 | + Details: struct { |
| 32 | + City string `json:"city"` |
| 33 | + Country string `json:"country"` |
| 34 | + }{ |
| 35 | + City: "New York", |
| 36 | + Country: "USA", |
| 37 | + }, |
| 38 | + }, |
| 39 | + false, |
| 40 | + }, |
| 41 | + { |
| 42 | + "空结构体", |
| 43 | + TestStruct{}, |
| 44 | + false, |
| 45 | + }, |
| 46 | + { |
| 47 | + "简单类型", |
| 48 | + "hello world", |
| 49 | + false, |
| 50 | + }, |
| 51 | + { |
| 52 | + "数字", |
| 53 | + 123, |
| 54 | + false, |
| 55 | + }, |
| 56 | + { |
| 57 | + "布尔值", |
| 58 | + true, |
| 59 | + false, |
| 60 | + }, |
| 61 | + { |
| 62 | + "切片", |
| 63 | + []string{"a", "b", "c"}, |
| 64 | + false, |
| 65 | + }, |
| 66 | + { |
| 67 | + "映射", |
| 68 | + map[string]interface{}{ |
| 69 | + "key1": "value1", |
| 70 | + "key2": 123, |
| 71 | + "key3": true, |
| 72 | + }, |
| 73 | + false, |
| 74 | + }, |
| 75 | + } |
| 76 | + |
| 77 | + for _, tt := range tests { |
| 78 | + t.Run(tt.name, func(t *testing.T) { |
| 79 | + result, err := JSONUtil.Struct2Json(tt.obj) |
| 80 | + if (err != nil) != tt.expectErr { |
| 81 | + t.Errorf("Struct2Json() error = %v, expectErr %v", err, tt.expectErr) |
| 82 | + return |
| 83 | + } |
| 84 | + if !tt.expectErr && result == "" { |
| 85 | + t.Error("Struct2Json() returned empty string") |
| 86 | + } |
| 87 | + }) |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +func TestJSON_Json2Struct(t *testing.T) { |
| 92 | + tests := []struct { |
| 93 | + name string |
| 94 | + jsonStr string |
| 95 | + obj interface{} |
| 96 | + expectErr bool |
| 97 | + }{ |
| 98 | + { |
| 99 | + "正常JSON", |
| 100 | + `{"name":"John Doe","age":30,"email":"[email protected]","active":true,"details":{"city":"New York","country":"USA"}}`, |
| 101 | + &TestStruct{}, |
| 102 | + false, |
| 103 | + }, |
| 104 | + { |
| 105 | + "空JSON对象", |
| 106 | + `{}`, |
| 107 | + &TestStruct{}, |
| 108 | + false, |
| 109 | + }, |
| 110 | + { |
| 111 | + "简单类型", |
| 112 | + `"hello world"`, |
| 113 | + new(string), |
| 114 | + false, |
| 115 | + }, |
| 116 | + { |
| 117 | + "数字", |
| 118 | + `123`, |
| 119 | + new(int), |
| 120 | + false, |
| 121 | + }, |
| 122 | + { |
| 123 | + "布尔值", |
| 124 | + `true`, |
| 125 | + new(bool), |
| 126 | + false, |
| 127 | + }, |
| 128 | + { |
| 129 | + "切片", |
| 130 | + `["a","b","c"]`, |
| 131 | + new([]string), |
| 132 | + false, |
| 133 | + }, |
| 134 | + { |
| 135 | + "无效JSON", |
| 136 | + `{invalid json}`, |
| 137 | + &TestStruct{}, |
| 138 | + true, |
| 139 | + }, |
| 140 | + { |
| 141 | + "空字符串", |
| 142 | + ``, |
| 143 | + &TestStruct{}, |
| 144 | + true, |
| 145 | + }, |
| 146 | + } |
| 147 | + |
| 148 | + for _, tt := range tests { |
| 149 | + t.Run(tt.name, func(t *testing.T) { |
| 150 | + err := JSONUtil.Json2Struct(tt.jsonStr, tt.obj) |
| 151 | + if (err != nil) != tt.expectErr { |
| 152 | + t.Errorf("Json2Struct() error = %v, expectErr %v", err, tt.expectErr) |
| 153 | + } |
| 154 | + }) |
| 155 | + } |
| 156 | +} |
| 157 | + |
| 158 | +func TestJSON_JsonI2Struct(t *testing.T) { |
| 159 | + tests := []struct { |
| 160 | + name string |
| 161 | + jsonInterface interface{} |
| 162 | + obj interface{} |
| 163 | + expectErr bool |
| 164 | + }{ |
| 165 | + { |
| 166 | + "字符串类型", |
| 167 | + `{"name":"John Doe","age":30}`, |
| 168 | + &TestStruct{}, |
| 169 | + false, |
| 170 | + }, |
| 171 | + { |
| 172 | + "非字符串类型", |
| 173 | + 123, |
| 174 | + &TestStruct{}, |
| 175 | + true, |
| 176 | + }, |
| 177 | + { |
| 178 | + "空接口", |
| 179 | + nil, |
| 180 | + &TestStruct{}, |
| 181 | + true, |
| 182 | + }, |
| 183 | + } |
| 184 | + |
| 185 | + for _, tt := range tests { |
| 186 | + t.Run(tt.name, func(t *testing.T) { |
| 187 | + err := JSONUtil.JsonI2Struct(tt.jsonInterface, tt.obj) |
| 188 | + if (err != nil) != tt.expectErr { |
| 189 | + t.Errorf("JsonI2Struct() error = %v, expectErr %v", err, tt.expectErr) |
| 190 | + } |
| 191 | + }) |
| 192 | + } |
| 193 | +} |
| 194 | + |
| 195 | +func TestJSON_RoundTrip(t *testing.T) { |
| 196 | + original := TestStruct{ |
| 197 | + Name: "John Doe", |
| 198 | + Age: 30, |
| 199 | + |
| 200 | + Active: true, |
| 201 | + Details: struct { |
| 202 | + City string `json:"city"` |
| 203 | + Country string `json:"country"` |
| 204 | + }{ |
| 205 | + City: "New York", |
| 206 | + Country: "USA", |
| 207 | + }, |
| 208 | + } |
| 209 | + |
| 210 | + // 结构体转JSON |
| 211 | + jsonStr, err := JSONUtil.Struct2Json(original) |
| 212 | + if err != nil { |
| 213 | + t.Fatalf("Struct2Json() error = %v", err) |
| 214 | + } |
| 215 | + |
| 216 | + // JSON转结构体 |
| 217 | + var result TestStruct |
| 218 | + err = JSONUtil.Json2Struct(jsonStr, &result) |
| 219 | + if err != nil { |
| 220 | + t.Fatalf("Json2Struct() error = %v", err) |
| 221 | + } |
| 222 | + |
| 223 | + // 比较结果 |
| 224 | + if result.Name != original.Name { |
| 225 | + t.Errorf("Name = %v, want %v", result.Name, original.Name) |
| 226 | + } |
| 227 | + if result.Age != original.Age { |
| 228 | + t.Errorf("Age = %v, want %v", result.Age, original.Age) |
| 229 | + } |
| 230 | + if result.Email != original.Email { |
| 231 | + t.Errorf("Email = %v, want %v", result.Email, original.Email) |
| 232 | + } |
| 233 | + if result.Active != original.Active { |
| 234 | + t.Errorf("Active = %v, want %v", result.Active, original.Active) |
| 235 | + } |
| 236 | + if result.Details.City != original.Details.City { |
| 237 | + t.Errorf("Details.City = %v, want %v", result.Details.City, original.Details.City) |
| 238 | + } |
| 239 | + if result.Details.Country != original.Details.Country { |
| 240 | + t.Errorf("Details.Country = %v, want %v", result.Details.Country, original.Details.Country) |
| 241 | + } |
| 242 | +} |
| 243 | + |
| 244 | +func BenchmarkJSON_Struct2Json(b *testing.B) { |
| 245 | + obj := TestStruct{ |
| 246 | + Name: "John Doe", |
| 247 | + Age: 30, |
| 248 | + |
| 249 | + Active: true, |
| 250 | + Details: struct { |
| 251 | + City string `json:"city"` |
| 252 | + Country string `json:"country"` |
| 253 | + }{ |
| 254 | + City: "New York", |
| 255 | + Country: "USA", |
| 256 | + }, |
| 257 | + } |
| 258 | + |
| 259 | + b.ResetTimer() |
| 260 | + for i := 0; i < b.N; i++ { |
| 261 | + _, err := JSONUtil.Struct2Json(obj) |
| 262 | + if err != nil { |
| 263 | + b.Fatal(err) |
| 264 | + } |
| 265 | + } |
| 266 | +} |
| 267 | + |
| 268 | +func BenchmarkJSON_Json2Struct(b *testing.B) { |
| 269 | + jsonStr := `{"name":"John Doe","age":30,"email":"[email protected]","active":true,"details":{"city":"New York","country":"USA"}}` |
| 270 | + |
| 271 | + b.ResetTimer() |
| 272 | + for i := 0; i < b.N; i++ { |
| 273 | + var obj TestStruct |
| 274 | + err := JSONUtil.Json2Struct(jsonStr, &obj) |
| 275 | + if err != nil { |
| 276 | + b.Fatal(err) |
| 277 | + } |
| 278 | + } |
| 279 | +} |
0 commit comments