Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SR-11274] JSONEncoder encode invalid Double value #3986

Open
swift-ci opened this issue Aug 8, 2019 · 3 comments
Open

[SR-11274] JSONEncoder encode invalid Double value #3986

swift-ci opened this issue Aug 8, 2019 · 3 comments

Comments

@swift-ci
Copy link
Contributor

swift-ci commented Aug 8, 2019

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

}

@theblixguy
Copy link

cc @itaiferber

@swift-ci
Copy link
Contributor Author

swift-ci commented Oct 9, 2019

Comment by Ben D. Jones (JIRA)

@swift-cisync

@swift-ci
Copy link
Contributor Author

Comment by Ben D. Jones (JIRA)

@swift-ci sync

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
@shahmishal shahmishal transferred this issue from apple/swift May 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants