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

JSON encoding of Double differs between platforms #358

Open
amomchilov opened this issue Dec 22, 2023 · 1 comment
Open

JSON encoding of Double differs between platforms #358

amomchilov opened this issue Dec 22, 2023 · 1 comment
Assignees

Comments

@amomchilov
Copy link

amomchilov commented Dec 22, 2023

I opened this issue kishikawakatsumi/swift-power-assert#436 about some failing unit tests, and it turns out to be platform dependant. From that thread:

iOS 17, Sonoma and Linux

{"foundingYear":1957,"location":{"longitude":139.745438,"latitude":35.658581},"name":"Tokyo > Tower"}

iOS 16.x and macOS Monterey

{"name":"Tokyo Tower","location":{"longitude":139.74543800000001,"latitude":35.658580999999998},"foundingYear":1957}

Given the OSes impacted, I'm guessing this was causing by the transition to the Swift-native Foundation impl on Apple platforms.

Is this difference in Double formatting intentional?

SwiftFiddle

demo.swift
import Foundation

struct Coordinate: Codable {
  var latitude: Double
  var longitude: Double
}

struct Landmark: Codable {
  var name: String
  var foundingYear: Int
  var location: Coordinate
}

let landmark = Landmark(
  name: "Tokyo Tower",
  foundingYear: 1957,
  location: Coordinate(latitude: 35.658581, longitude: 139.745438)
)

let data = try JSONEncoder().encode(landmark)
print(String(data: data, encoding: .utf8)!)
print(data.count)
print(data as NSData) // NSData gives a better description
@iCharlesHu iCharlesHu assigned iCharlesHu and kperryua and unassigned iCharlesHu Jan 9, 2024
@iCharlesHu
Copy link
Contributor

@kperryua could you please take a look?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants