@@ -1224,4 +1224,110 @@ 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 TestInt64OrUInteger64BitToString (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 : "normal_struct" ,
1260+ val : struct {
1261+ Int int `json:"int"`
1262+ Int64 int64 `json:"int64"`
1263+ Uint64 uint64 `json:"uint64"`
1264+ }{
1265+ Int : int (12 ),
1266+ Int64 : int64 (34 ),
1267+ Uint64 : uint64 (56 ),
1268+ },
1269+ exceptTrue : `{"int":12,"int64":"34","uint64":"56"}` ,
1270+ exceptFalse : `{"int":12,"int64":34,"uint64":56}` ,
1271+ },
1272+ {
1273+ name : "normal_slice" ,
1274+ val : []any {
1275+ int (12 ), int64 (34 ), uint64 (56 ),
1276+ },
1277+ exceptTrue : `[12,"34","56"]` ,
1278+ exceptFalse : `[12,34,56]` ,
1279+ },
1280+ {
1281+ name : "single_int64" ,
1282+ val : int64 (34 ),
1283+ exceptTrue : `"34"` ,
1284+ exceptFalse : `34` ,
1285+ },
1286+ {
1287+ name : "single_uint64" ,
1288+ val : uint64 (56 ),
1289+ exceptTrue : `"56"` ,
1290+ exceptFalse : `56` ,
1291+ },
1292+ {
1293+ name : "int64ptr" ,
1294+ val : struct {
1295+ Map map [string ]any
1296+ }{map [string ]any {"val" : struct {
1297+ Int64Ptr any
1298+ Uint64Ptr any
1299+ Int64 any
1300+ Uint64 any
1301+ }{
1302+ Int64Ptr : & int64ptr ,
1303+ Uint64Ptr : & uint64ptr ,
1304+ Int64 : int64 (123 ),
1305+ Uint64 : uint64 (456 ),
1306+ }}},
1307+ exceptTrue : `{"Map":{"val":{"Int64Ptr":"432556670863027541",` +
1308+ `"Uint64Ptr":"12372850276778298372","Int64":"123","Uint64":"456"}}}` ,
1309+ exceptFalse : `{"Map":{"val":{"Int64Ptr":432556670863027541,` +
1310+ `"Uint64Ptr":12372850276778298372,"Int64":123,"Uint64":456}}}` ,
1311+ },
1312+ }
1313+
1314+ check := func (t * testing.T , except string , testRes []byte ) {
1315+ var tmp1 any
1316+ assert .Nil (t , Unmarshal ([]byte (testRes ), & tmp1 ))
1317+ var tmp2 any
1318+ assert .Nil (t , Unmarshal ([]byte (except ), & tmp2 ))
1319+ assert .Equal (t , tmp2 , tmp1 )
1320+ }
1321+
1322+ for _ , c := range cases {
1323+ t .Run (c .name , func (t * testing.T ) {
1324+ b , e := Config {Integer64BitToString : true }.Froze ().Marshal (c .val )
1325+ assert .Nil (t , e )
1326+ check (t , c .exceptTrue , b )
1327+
1328+ b , e = Config {Integer64BitToString : false }.Froze ().Marshal (c .val )
1329+ assert .Nil (t , e )
1330+ check (t , c .exceptFalse , b )
1331+ })
1332+ }
12271333}
0 commit comments