@@ -1224,4 +1224,120 @@ func TestMarshalInfOrNan(t *testing.T) {
12241224 assert .NotNil (t , err )
12251225 assert .True (t , strings .Contains (err .Error (), "json: unsupported value: NaN or ±Infinite" ))
12261226 }
1227+ }
1228+
1229+ func TestUint64ToString (t * testing.T ) {
1230+ int64ptr := int64 (432556670863027541 )
1231+ uint64ptr := uint64 (12372850276778298372 )
1232+ cases := []struct {
1233+ name string
1234+ val any
1235+ exceptTrue string
1236+ exceptFalse string
1237+ }{
1238+ {
1239+ name : "normal_map" ,
1240+ val : map [string ]any {
1241+ "int" : int (12 ),
1242+ "int64" : int64 (34 ),
1243+ "uint64" : uint64 (56 ),
1244+ },
1245+ exceptTrue : `{"int":12,"int64":34,"uint64":"56"}` ,
1246+ exceptFalse : `{"int":12,"int64":34,"uint64":56}` ,
1247+ },
1248+ {
1249+ name : "int_key_map" ,
1250+ val : map [int64 ]any {
1251+ int64 (12 ): int (12 ),
1252+ int64 (34 ): int64 (34 ),
1253+ int64 (56 ): uint64 (56 ),
1254+ },
1255+ exceptTrue : `{"12":12,"34":34,"56":"56"}` ,
1256+ exceptFalse : `{"12":12,"34":34,"56":56}` ,
1257+ },
1258+ {
1259+ name : "uint_key_map" ,
1260+ val : map [uint64 ]any {
1261+ uint64 (12 ): int (12 ),
1262+ uint64 (34 ): int64 (34 ),
1263+ uint64 (56 ): uint64 (56 ),
1264+ },
1265+ exceptTrue : `{"12":12,"34":34,"56":"56"}` ,
1266+ exceptFalse : `{"12":12,"34":34,"56":56}` ,
1267+ },
1268+ {
1269+ name : "normal_struct" ,
1270+ val : struct {
1271+ Int int `json:"int"`
1272+ Int64 int64 `json:"int64"`
1273+ Uint64 uint64 `json:"uint64"`
1274+ }{
1275+ Int : int (12 ),
1276+ Int64 : int64 (34 ),
1277+ Uint64 : uint64 (56 ),
1278+ },
1279+ exceptTrue : `{"int":12,"int64":34,"uint64":"56"}` ,
1280+ exceptFalse : `{"int":12,"int64":34,"uint64":56}` ,
1281+ },
1282+ {
1283+ name : "normal_slice" ,
1284+ val : []any {
1285+ int (12 ), int64 (34 ), uint64 (56 ),
1286+ },
1287+ exceptTrue : `[12,34,"56"]` ,
1288+ exceptFalse : `[12,34,56]` ,
1289+ },
1290+ {
1291+ name : "single_int64" ,
1292+ val : int64 (34 ),
1293+ exceptTrue : `34` ,
1294+ exceptFalse : `34` ,
1295+ },
1296+ {
1297+ name : "single_uint64" ,
1298+ val : uint64 (56 ),
1299+ exceptTrue : `"56"` ,
1300+ exceptFalse : `56` ,
1301+ },
1302+ {
1303+ name : "int64ptr" ,
1304+ val : struct {
1305+ Map map [string ]any
1306+ }{map [string ]any {"val" : struct {
1307+ Int64Ptr any
1308+ Uint64Ptr any
1309+ Int64 any
1310+ Uint64 any
1311+ }{
1312+ Int64Ptr : & int64ptr ,
1313+ Uint64Ptr : & uint64ptr ,
1314+ Int64 : int64 (123 ),
1315+ Uint64 : uint64 (456 ),
1316+ }}},
1317+ exceptTrue : `{"Map":{"val":{"Int64Ptr":432556670863027541,` +
1318+ `"Uint64Ptr":"12372850276778298372","Int64":123,"Uint64":"456"}}}` ,
1319+ exceptFalse : `{"Map":{"val":{"Int64Ptr":432556670863027541,` +
1320+ `"Uint64Ptr":12372850276778298372,"Int64":123,"Uint64":456}}}` ,
1321+ },
1322+ }
1323+
1324+ check := func (t * testing.T , except string , testRes []byte ) {
1325+ var tmp1 any
1326+ assert .Nil (t , Unmarshal ([]byte (testRes ), & tmp1 ))
1327+ var tmp2 any
1328+ assert .Nil (t , Unmarshal ([]byte (except ), & tmp2 ))
1329+ assert .Equal (t , tmp2 , tmp1 )
1330+ }
1331+
1332+ for _ , c := range cases {
1333+ t .Run (c .name , func (t * testing.T ) {
1334+ b , e := Config {Uint64ToString : true }.Froze ().Marshal (c .val )
1335+ assert .Nil (t , e )
1336+ check (t , c .exceptTrue , b )
1337+
1338+ b , e = Config {Uint64ToString : false }.Froze ().Marshal (c .val )
1339+ assert .Nil (t , e )
1340+ check (t , c .exceptFalse , b )
1341+ })
1342+ }
12271343}
0 commit comments