Open
Description
Previous ID | SR-10596 |
Radar | rdar://problem/50371749 |
Original Reporter | jozen (JIRA User) |
Type | Bug |
Status | In Progress |
Resolution |
Attachment: Download
Additional Detail from JIRA
Votes | 0 |
Component/s | Foundation |
Labels | Bug, Codable |
Assignee | hungtruong (JIRA) |
Priority | Medium |
md5: bc6d6a7e328908f8a562ab76fbb1e965
Issue Description:
Decodable should cause run time errors when attempting to parse Floats as Integers. However, this does not occur when the Float value is a whole number. Because Decodable will only fail at runtime, it is prudent that it correctly fails on these simpler cases as it can mask issues
import Foundation
struct Swifter: Decodable {
let floatParsedAsInt: Int
}
let json = """
{
"floatParsedAsInt": 1.0,
}
""".data(using: .utf8)!
let myStruct = try JSONDecoder().decode(Swifter.self, from: json)
print(myStruct.floatParsedAsInt) //output 1
By comparison the swift compiler would reject the follow code:
var intValue: Int = 1.0 // Error: cannot convert value of type 'Double' to specified type 'Int'
or
struct JSONStructureWithInt: Decodable {
let intAsString: Int
}
let json = """
{
"intAsString": "1",
}
""".data(using: .utf8)!
let myStruct1 = try JSONDecoder().decode(JSONStructureWithString.self, from: json) // Error: Expected to decode Int but found a string/data instead
print(myStruct1.intAsString)