@@ -829,6 +829,7 @@ extension TestJSONSerialization {
829
829
return [
830
830
( " test_isValidJSONObjectTrue " , test_isValidJSONObjectTrue) ,
831
831
( " test_isValidJSONObjectFalse " , test_isValidJSONObjectFalse) ,
832
+ ( " test_validNumericJSONObjects " , test_validNumericJSONObjects)
832
833
]
833
834
}
834
835
@@ -924,6 +925,32 @@ extension TestJSONSerialization {
924
925
}
925
926
}
926
927
928
+ func test_validNumericJSONObjects( ) {
929
+ // All of the numeric types supported by JSONSerialization
930
+ XCTAssertTrue ( JSONSerialization . isValidJSONObject ( [ nil , NSNull ( ) ] ) )
931
+ XCTAssertTrue ( JSONSerialization . isValidJSONObject ( [ true , false ] ) )
932
+ XCTAssertTrue ( JSONSerialization . isValidJSONObject ( [ Int . min, Int8 . min, Int16 . min, Int32 . min, Int64 . min] ) )
933
+ XCTAssertTrue ( JSONSerialization . isValidJSONObject ( [ UInt . min, UInt8 . min, UInt16 . min, UInt32 . min, UInt64 . min] ) )
934
+ XCTAssertTrue ( JSONSerialization . isValidJSONObject ( [ Float . leastNonzeroMagnitude, Double . leastNonzeroMagnitude] ) )
935
+
936
+ XCTAssertTrue ( JSONSerialization . isValidJSONObject ( [ NSNumber ( value: true ) , NSNumber ( value: Float . greatestFiniteMagnitude) , NSNumber ( value: Double . greatestFiniteMagnitude) ] ) )
937
+ XCTAssertTrue ( JSONSerialization . isValidJSONObject ( [ NSNumber ( value: Int . max) , NSNumber ( value: Int8 . max) , NSNumber ( value: Int16 . max) , NSNumber ( value: Int32 . max) , NSNumber ( value: Int64 . max) ] ) )
938
+ XCTAssertTrue ( JSONSerialization . isValidJSONObject ( [ NSNumber ( value: UInt . max) , NSNumber ( value: UInt8 . max) , NSNumber ( value: UInt16 . max) , NSNumber ( value: UInt32 . max) , NSNumber ( value: UInt64 . max) ] ) )
939
+ XCTAssertTrue ( JSONSerialization . isValidJSONObject ( [ NSDecimalNumber ( booleanLiteral: true ) , NSDecimalNumber ( decimal: Decimal . greatestFiniteMagnitude) , NSDecimalNumber ( floatLiteral: Double . greatestFiniteMagnitude) , NSDecimalNumber ( integerLiteral: Int . min) ] ) )
940
+ XCTAssertTrue ( JSONSerialization . isValidJSONObject ( [ Decimal ( 123 ) , Decimal ( Double . leastNonzeroMagnitude) ] ) )
941
+
942
+ XCTAssertFalse ( JSONSerialization . isValidJSONObject ( Float . nan) )
943
+ XCTAssertFalse ( JSONSerialization . isValidJSONObject ( Float . infinity) )
944
+ XCTAssertFalse ( JSONSerialization . isValidJSONObject ( NSNumber ( value: Float . nan) ) )
945
+ XCTAssertFalse ( JSONSerialization . isValidJSONObject ( NSNumber ( value: Float . infinity) ) )
946
+
947
+ XCTAssertFalse ( JSONSerialization . isValidJSONObject ( Double . nan) )
948
+ XCTAssertFalse ( JSONSerialization . isValidJSONObject ( Double . infinity) )
949
+ XCTAssertFalse ( JSONSerialization . isValidJSONObject ( NSNumber ( value: Double . nan) ) )
950
+ XCTAssertFalse ( JSONSerialization . isValidJSONObject ( NSNumber ( value: Double . infinity) ) )
951
+
952
+ XCTAssertFalse ( JSONSerialization . isValidJSONObject ( NSDecimalNumber ( decimal: Decimal ( floatLiteral: Double . nan) ) ) )
953
+ }
927
954
}
928
955
929
956
// MARK: - serializationTests
@@ -941,6 +968,14 @@ extension TestJSONSerialization {
941
968
( " test_serialize_IntMin " , test_serialize_IntMin) ,
942
969
( " test_serialize_UIntMax " , test_serialize_UIntMax) ,
943
970
( " test_serialize_UIntMin " , test_serialize_UIntMin) ,
971
+ ( " test_serialize_8BitSizes " , test_serialize_8BitSizes) ,
972
+ ( " test_serialize_16BitSizes " , test_serialize_16BitSizes) ,
973
+ ( " test_serialize_32BitSizes " , test_serialize_32BitSizes) ,
974
+ ( " test_serialize_64BitSizes " , test_serialize_64BitSizes) ,
975
+ ( " test_serialize_Float " , test_serialize_Float) ,
976
+ ( " test_serialize_Double " , test_serialize_Double) ,
977
+ ( " test_serialize_Decimal " , test_serialize_Decimal) ,
978
+ ( " test_serialize_NSDecimalNumber " , test_serialize_NSDecimalNumber) ,
944
979
( " test_serialize_stringEscaping " , test_serialize_stringEscaping) ,
945
980
( " test_jsonReadingOffTheEndOfBuffers " , test_jsonReadingOffTheEndOfBuffers) ,
946
981
( " test_jsonObjectToOutputStreamBuffer " , test_jsonObjectToOutputStreamBuffer) ,
@@ -1172,7 +1207,80 @@ extension TestJSONSerialization {
1172
1207
let json : [ Any ] = [ UInt . min]
1173
1208
XCTAssertEqual ( try trySerialize ( json) , " [ \( UInt . min) ] " )
1174
1209
}
1175
-
1210
+
1211
+ func test_serialize_8BitSizes( ) {
1212
+ let json1 = [ Int8 . min, Int8 ( - 1 ) , Int8 ( 0 ) , Int8 ( 1 ) , Int8 . max]
1213
+ XCTAssertEqual ( try trySerialize ( json1) , " [-128,-1,0,1,127] " )
1214
+ let json2 = [ UInt8 . min, UInt8 ( 0 ) , UInt8 ( 1 ) , UInt8 . max]
1215
+ XCTAssertEqual ( try trySerialize ( json2) , " [0,0,1,255] " )
1216
+ }
1217
+
1218
+ func test_serialize_16BitSizes( ) {
1219
+ let json1 = [ Int16 . min, Int16 ( - 1 ) , Int16 ( 0 ) , Int16 ( 1 ) , Int16 . max]
1220
+ XCTAssertEqual ( try trySerialize ( json1) , " [-32768,-1,0,1,32767] " )
1221
+ let json2 = [ UInt16 . min, UInt16 ( 0 ) , UInt16 ( 1 ) , UInt16 . max]
1222
+ XCTAssertEqual ( try trySerialize ( json2) , " [0,0,1,65535] " )
1223
+ }
1224
+
1225
+ func test_serialize_32BitSizes( ) {
1226
+ let json1 = [ Int32 . min, Int32 ( - 1 ) , Int32 ( 0 ) , Int32 ( 1 ) , Int32 . max]
1227
+ XCTAssertEqual ( try trySerialize ( json1) , " [-2147483648,-1,0,1,2147483647] " )
1228
+ let json2 = [ UInt32 . min, UInt32 ( 0 ) , UInt32 ( 1 ) , UInt32 . max]
1229
+ XCTAssertEqual ( try trySerialize ( json2) , " [0,0,1,4294967295] " )
1230
+ }
1231
+
1232
+ func test_serialize_64BitSizes( ) {
1233
+ let json1 = [ Int64 . min, Int64 ( - 1 ) , Int64 ( 0 ) , Int64 ( 1 ) , Int64 . max]
1234
+ XCTAssertEqual ( try trySerialize ( json1) , " [-9223372036854775808,-1,0,1,9223372036854775807] " )
1235
+ let json2 = [ UInt64 . min, UInt64 ( 0 ) , UInt64 ( 1 ) , UInt64 . max]
1236
+ XCTAssertEqual ( try trySerialize ( json2) , " [0,0,1,18446744073709551615] " )
1237
+ }
1238
+
1239
+ func test_serialize_Float( ) {
1240
+ XCTAssertEqual ( try trySerialize ( [ - Float. leastNonzeroMagnitude, Float . leastNonzeroMagnitude] ) , " [-0,0] " )
1241
+ XCTAssertEqual ( try trySerialize ( [ - Float. greatestFiniteMagnitude] ) , " [-340282346638529000000000000000000000000] " )
1242
+ XCTAssertEqual ( try trySerialize ( [ Float . greatestFiniteMagnitude] ) , " [340282346638529000000000000000000000000] " )
1243
+ XCTAssertEqual ( try trySerialize ( [ Float ( - 1 ) , Float . leastNonzeroMagnitude, Float ( 1 ) ] ) , " [-1,0,1] " )
1244
+ }
1245
+
1246
+ func test_serialize_Double( ) {
1247
+ XCTAssertEqual ( try trySerialize ( [ - Double. leastNonzeroMagnitude, Double . leastNonzeroMagnitude] ) , " [-0,0] " )
1248
+ XCTAssertEqual ( try trySerialize ( [ - Double. leastNormalMagnitude, Double . leastNormalMagnitude] ) , " [-0,0] " )
1249
+ XCTAssertEqual ( try trySerialize ( [ - Double. greatestFiniteMagnitude] ) , " [-179769313486232000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000] " )
1250
+ XCTAssertEqual ( try trySerialize ( [ Double . greatestFiniteMagnitude] ) , " [179769313486232000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000] " )
1251
+ XCTAssertEqual ( try trySerialize ( [ Double ( - 1.0 ) , Double ( 1.0 ) ] ) , " [-1,1] " )
1252
+ }
1253
+
1254
+ func test_serialize_Decimal( ) {
1255
+ XCTAssertEqual ( try trySerialize ( [ - Decimal. leastFiniteMagnitude] ) , " [3402823669209384634633746074317682114550000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000] " )
1256
+ XCTAssertEqual ( try trySerialize ( [ Decimal . leastFiniteMagnitude] ) , " [-3402823669209384634633746074317682114550000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000] " )
1257
+ XCTAssertEqual ( try trySerialize ( [ - Decimal. leastNonzeroMagnitude] ) , " [-0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001] " )
1258
+ XCTAssertEqual ( try trySerialize ( [ Decimal . leastNonzeroMagnitude] ) , " [0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001] " )
1259
+ XCTAssertEqual ( try trySerialize ( [ - Decimal. greatestFiniteMagnitude] ) , " [-3402823669209384634633746074317682114550000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000] " )
1260
+ XCTAssertEqual ( try trySerialize ( [ Decimal . greatestFiniteMagnitude] ) , " [3402823669209384634633746074317682114550000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000] " )
1261
+ XCTAssertEqual ( try trySerialize ( [ Decimal ( Int8 . min) , Decimal ( Int8 ( 0 ) ) , Decimal ( Int8 . max) ] ) , " [-128,0,127] " )
1262
+ XCTAssertEqual ( try trySerialize ( [ Decimal ( string: " -0.0 " ) , Decimal ( string: " 0.000 " ) , Decimal ( string: " 1.0000 " ) ] ) , " [0,0,1] " )
1263
+ }
1264
+
1265
+ func test_serialize_NSDecimalNumber( ) {
1266
+ let dn0 : [ Any ] = [ NSDecimalNumber ( floatLiteral: - Double. leastNonzeroMagnitude) ]
1267
+ let dn1 : [ Any ] = [ NSDecimalNumber ( floatLiteral: Double . leastNonzeroMagnitude) ]
1268
+ let dn2 : [ Any ] = [ NSDecimalNumber ( floatLiteral: - Double. leastNormalMagnitude) ]
1269
+ let dn3 : [ Any ] = [ NSDecimalNumber ( floatLiteral: Double . leastNormalMagnitude) ]
1270
+ let dn4 : [ Any ] = [ NSDecimalNumber ( floatLiteral: - Double. greatestFiniteMagnitude) ]
1271
+ let dn5 : [ Any ] = [ NSDecimalNumber ( floatLiteral: Double . greatestFiniteMagnitude) ]
1272
+
1273
+ XCTAssertEqual ( try trySerialize ( dn0) , " [-0.00000000000000000000000000000000000000000000000000000000000000000004940656458412464128] " )
1274
+ XCTAssertEqual ( try trySerialize ( dn1) , " [0.00000000000000000000000000000000000000000000000000000000000000000004940656458412464128] " )
1275
+ XCTAssertEqual ( try trySerialize ( dn2) , " [-0.0000000000000000000000000000000000000000000000000002225073858507201792] " )
1276
+ XCTAssertEqual ( try trySerialize ( dn3) , " [0.0000000000000000000000000000000000000000000000000002225073858507201792] " )
1277
+ XCTAssertEqual ( try trySerialize ( dn4) , " [-17976931348623167488000000000000000000000000000000000] " )
1278
+ XCTAssertEqual ( try trySerialize ( dn5) , " [17976931348623167488000000000000000000000000000000000] " )
1279
+ XCTAssertEqual ( try trySerialize ( [ NSDecimalNumber ( string: " 0.0001 " ) , NSDecimalNumber ( string: " 0.00 " ) , NSDecimalNumber ( string: " -0.0 " ) ] ) , " [0.0001,0,0] " )
1280
+ XCTAssertEqual ( try trySerialize ( [ NSDecimalNumber ( integerLiteral: Int ( Int16 . min) ) , NSDecimalNumber ( integerLiteral: 0 ) , NSDecimalNumber ( integerLiteral: Int ( Int16 . max) ) ] ) , " [-32768,0,32767] " )
1281
+ XCTAssertEqual ( try trySerialize ( [ NSDecimalNumber ( booleanLiteral: true ) , NSDecimalNumber ( booleanLiteral: false ) ] ) , " [1,0] " )
1282
+ }
1283
+
1176
1284
func test_serialize_stringEscaping( ) {
1177
1285
var json = [ " foo " ]
1178
1286
XCTAssertEqual ( try trySerialize ( json) , " [ \" foo \" ] " )
0 commit comments