@@ -656,6 +656,66 @@ class TestJSONEncoder : XCTestCase {
656
656
}
657
657
}
658
658
659
+ func test_encodeDecodeNumericTypesBaseline( ) throws {
660
+ struct NumericTypesStruct : Codable , Equatable {
661
+ let int8Value : Int8
662
+ let uint8Value : UInt8
663
+ let int16Value : Int16
664
+ let uint16Value : UInt16
665
+ let int32Value : Int32
666
+ let uint32Value : UInt32
667
+ let int64Value : Int64
668
+ let intValue : Int
669
+ let uintValue : UInt
670
+ let uint64Value : UInt64
671
+ let floatValue : Float
672
+ let doubleValue : Double
673
+ let decimalValue : Decimal
674
+ }
675
+
676
+ let source = NumericTypesStruct (
677
+ int8Value: - 12 ,
678
+ uint8Value: 34 ,
679
+ int16Value: - 5678 ,
680
+ uint16Value: 9011 ,
681
+ int32Value: - 12141516 ,
682
+ uint32Value: 17181920 ,
683
+ int64Value: - 21222324252627 ,
684
+ intValue: - 2829303132 ,
685
+ uintValue: 33343536 ,
686
+ uint64Value: 373839404142 ,
687
+ floatValue: 1.234 ,
688
+ doubleValue: 5.101520 ,
689
+ decimalValue: Decimal ( 10 ) )
690
+
691
+ let data = try JSONEncoder ( ) . encode ( source)
692
+ let destination = try JSONDecoder ( ) . decode ( NumericTypesStruct . self, from: data)
693
+ XCTAssertEqual ( source, destination)
694
+
695
+ // Ensure that if a value is expressed as a floating point number, it casts correctly into the underlying type.
696
+
697
+ let json = """
698
+ {
699
+ " int8Value " : -12.0,
700
+ " uint8Value " : 34.0,
701
+ " int16Value " : -5678.0,
702
+ " uint16Value " : 9011.0,
703
+ " int32Value " : -12141516.0,
704
+ " uint32Value " : 17181920.0,
705
+ " int64Value " : -21222324252627.0,
706
+ " intValue " : -2829303132.0,
707
+ " uintValue " : 33343536.0,
708
+ " uint64Value " : 373839404142.0,
709
+ " floatValue " : 1.234,
710
+ " doubleValue " : 5.101520,
711
+ " decimalValue " : 10.0
712
+ }
713
+ """
714
+
715
+ let destination2 = try JSONDecoder ( ) . decode ( NumericTypesStruct . self, from: Data ( json. utf8) )
716
+ XCTAssertEqual ( source, destination2)
717
+ }
718
+
659
719
func test_numericLimits( ) {
660
720
struct DataStruct : Codable {
661
721
let int8Value : Int8 ?
@@ -1479,6 +1539,7 @@ extension TestJSONEncoder {
1479
1539
( " test_encodingCustomDataEmpty " , test_encodingCustomDataEmpty) ,
1480
1540
( " test_encodingNonConformingFloats " , test_encodingNonConformingFloats) ,
1481
1541
( " test_encodingNonConformingFloatStrings " , test_encodingNonConformingFloatStrings) ,
1542
+ ( " test_encodeDecodeNumericTypesBaseline " , test_encodeDecodeNumericTypesBaseline) ,
1482
1543
( " test_nestedContainerCodingPaths " , test_nestedContainerCodingPaths) ,
1483
1544
( " test_superEncoderCodingPaths " , test_superEncoderCodingPaths) ,
1484
1545
( " test_codingOfBool " , test_codingOfBool) ,
0 commit comments