Open
Description
Previous ID | SR-11274 |
Radar | None |
Original Reporter | ryanwu (JIRA User) |
Type | Bug |
Environment
Swift 5.0
Additional Detail from JIRA
Votes | 0 |
Component/s | Foundation |
Labels | Bug, Codable |
Assignee | bendjones (JIRA) |
Priority | Medium |
md5: e20ec7a1e322c9f209b388883b194590
Issue Description:
When encode a codable to json string, the double value is changed to a long decimal value.
JSONDecoder works fine!
Decimal is working fine!
// Swift 5.0
var json = "{ \"value\": 3.58, \"amount\": 3.58 }"
struct Model: Codable {
var value: Decimal?
var amount: Double?
}
let data = json.data(using: .utf8)
let jsonDecoder = JSONDecoder()
let model = try? jsonDecoder.decode(Model.self, from: data!)
let value: Decimal = model?.value ?? 0
let amount: Double = model?.amount ?? 0
print(value)
print(amount)
print(String(describing: model))
let encoder = JSONEncoder()
encoder.outputFormatting = .prettyPrinted
let newData = try? encoder.encode(model)
print(String(data: newData!, encoding: .utf8)!)
print out
{
"value" : 3.58,
"amount" : 3.5800000000000001
}