File tree Expand file tree Collapse file tree 2 files changed +10
-9
lines changed Expand file tree Collapse file tree 2 files changed +10
-9
lines changed Original file line number Diff line number Diff line change @@ -284,6 +284,8 @@ internal extension JSONSerialization {
284
284
//MARK: - JSONSerializer
285
285
private struct JSONWriter {
286
286
287
+ private let maxUIntLength = String ( describing: UInt . max) . characters. count
288
+ private let maxIntLength = String ( describing: Int . max) . characters. count
287
289
var indent = 0
288
290
let pretty : Bool
289
291
let writer : ( String ? ) -> Void
@@ -334,8 +336,8 @@ private struct JSONWriter {
334
336
var array : [ UInt ] = [ ]
335
337
var stringResult = " "
336
338
//Maximum length of an UInt
337
- array. reserveCapacity ( 20 )
338
- stringResult. reserveCapacity ( 20 )
339
+ array. reserveCapacity ( maxUIntLength )
340
+ stringResult. reserveCapacity ( maxUIntLength )
339
341
var number = value
340
342
341
343
while number != 0 {
@@ -375,10 +377,9 @@ private struct JSONWriter {
375
377
}
376
378
var array : [ Int ] = [ ]
377
379
var stringResult = " "
378
- //Maximum length of an Int
379
- array. reserveCapacity ( 19 )
380
+ array. reserveCapacity ( maxIntLength)
380
381
//Account for a negative sign
381
- stringResult. reserveCapacity ( 20 )
382
+ stringResult. reserveCapacity ( maxIntLength + 1 )
382
383
var number = value
383
384
384
385
while number != 0 {
Original file line number Diff line number Diff line change @@ -1060,22 +1060,22 @@ extension TestNSJSONSerialization {
1060
1060
1061
1061
func test_serialize_IntMax( ) {
1062
1062
let json : [ Any ] = [ Int . max]
1063
- XCTAssertEqual ( try trySerialize ( json) , " [9223372036854775807 ] " )
1063
+ XCTAssertEqual ( try trySerialize ( json) , " [ \( Int . max ) ] " )
1064
1064
}
1065
1065
1066
1066
func test_serialize_IntMin( ) {
1067
1067
let json : [ Any ] = [ Int . min]
1068
- XCTAssertEqual ( try trySerialize ( json) , " [-9223372036854775808 ] " )
1068
+ XCTAssertEqual ( try trySerialize ( json) , " [ \( Int . min ) ] " )
1069
1069
}
1070
1070
1071
1071
func test_serialize_UIntMax( ) {
1072
1072
let json : [ Any ] = [ UInt . max]
1073
- XCTAssertEqual ( try trySerialize ( json) , " [18446744073709551615 ] " )
1073
+ XCTAssertEqual ( try trySerialize ( json) , " [ \( Int . max ) ] " )
1074
1074
}
1075
1075
1076
1076
func test_serialize_UIntMin( ) {
1077
1077
let json : [ Any ] = [ UInt . min]
1078
- XCTAssertEqual ( try trySerialize ( json) , " [0 ] " )
1078
+ XCTAssertEqual ( try trySerialize ( json) , " [ \( Int . min ) ] " )
1079
1079
}
1080
1080
1081
1081
func test_serialize_stringEscaping( ) {
You can’t perform that action at this time.
0 commit comments