Skip to content

JSONSerialization re-encodes Decimal as Double #1115

@blindspotbounty

Description

@blindspotbounty

I am using JSONSerialization to sort keys for arbitrary JSON represented as String:

func sortedJSON(_ string: String) -> String? {
    guard let data = string.data(using: .utf8),
          let json = try? JSONSerialization.jsonObject(with: data, options: []),
          let sortedData = try? JSONSerialization.data(withJSONObject: json, options: [.sortedKeys])
    else {
        return nil
    }

    return .init(data: sortedData, encoding: .utf8)
}

/// example of usage
enum DecimalContainting: Codable {
    case decimal(Decimal)
}

let decimal = DecimalContainting.decimal(Decimal(string: "129.9")!)
let jsonEncoder = JSONEncoder()
let data = try jsonEncoder.encode(decimal)

let jsonUnsortedString = String(data: data, encoding: .utf8)

let jsonSortedString = jsonUnsortedString.flatMap { sortedJSON($0) }
print(jsonUnsortedString)
print(jsonSortedString)

The output is surprisingly different for sorted and unsorted data:

Optional("{\"decimal\":{\"_0\":129.9}}")
Optional("{\"decimal\":{\"_0\":129.90000000000001}}")

Notably, this happens on MacOS but if I run the same code on linux, the output is the same for both:

Optional("{\"decimal\":{\"_0\":129.9}}")
Optional("{\"decimal\":{\"_0\":129.9}}")

Swift version macOS:

$ swift --version
swift-driver version: 1.115.1 Apple Swift version 6.0.3 (swiftlang-6.0.3.1.10 clang-1600.0.30.1)

Swift version Linux:

$ swift --version
Swift version 6.0.1 (swift-6.0.1-RELEASE)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions