Swift JSONEncoder 数字四舍五入

mar*_*rux 6 json swift

与所有 IEEE 7540 系统一样,Swift 中的数字 like4.7被视为类似的值4.7000000000000002。所以这并不奇怪:

% swift
Welcome to Apple Swift version 5.2.4 (swiftlang-1103.0.32.9 clang-1103.0.32.53).
Type :help for assistance.
  1> 4.7
$R0: Double = 4.7000000000000002
  2> 4.7 == 4.7000000000000002
$R1: Bool = true
Run Code Online (Sandbox Code Playgroud)

这是一个很好理解的世界现实,因此不需要通过包含指向浮点精度损失背景文章链接的评论来解决。

当使用内置的 编码这个数字时JSONEncoder,我们看到:

  4> String(data: JSONEncoder().encode([4.7]), encoding: .utf8) 
$R2: String? = "[4.7000000000000002]"
Run Code Online (Sandbox Code Playgroud)

这不是不正确,因为维基百科说有关JSON和浮点数:

JSON 标准对上溢、下溢、精度损失、舍入或带符号零等实现细节没有要求,但它确实建议期望不超过 IEEE 754 binary64 精度以实现“良好的互操作性”。将浮点数的机器级二进制表示(如 binary64)序列化为人类可读的十进制表示(如 JSON 中的数字)并返回时没有固有的精度损失,因为存在已发布的算法来准确地执行此操作和最佳。

但是,其他 JavaScript 环境倾向于对这些数字进行四舍五入。例如使用 JavaScriptCore:

% /System/Library/Frameworks/JavaScriptCore.framework/Versions/A/Helpers/jsc

>>> 4.7 == 4.7000000000000002
true
>>> JSON.stringify([4.7000000000000002])
[4.7]
Run Code Online (Sandbox Code Playgroud)

并与节点:

% node
Welcome to Node.js v13.13.0.
Type ".help" for more information.
> 4.7 == 4.7000000000000002
true
> JSON.stringify([4.7000000000000002])
'[4.7]'
Run Code Online (Sandbox Code Playgroud)

对我来说,问题是我有大量的 Swift doubles 集合,当序列化为 JSON 进行存储和/或传输时,包含大量不必要的碎片(“4.7000000000000002”的字符数是“4.7”的 6 倍),从而扩大了大小序列化数据相当多。

谁能想到一个很好的方法来覆盖 Swift 的数字编码以将双精度序列化为它们的四舍五入等效项,而不是放弃自动合成可编码性并手动重新实现整个类型图的编码?

Leo*_*bus 5

您可以扩展 KeyedEncodingContainer 和 KeyedDecodingContainer 并实现自定义编码和解码方法以将 Decimal 作为纯数据发送。您只需要将编码器/解码器 dataEncodingStrategy 设置为 deferredToData。另一种可能性是编码和解码其 base64Data 或将其编码/解码为纯字符串。

extension Numeric {
    var data: Data {
        var bytes = self
        return .init(bytes: &bytes, count: MemoryLayout<Self>.size)
    }
}
Run Code Online (Sandbox Code Playgroud)
extension DataProtocol {
    func decode<T: Numeric>(_ codingPath: [CodingKey], key: CodingKey) throws -> T {
        var value: T = .zero
        guard withUnsafeMutableBytes(of: &value, copyBytes) == MemoryLayout.size(ofValue: value) else {
            throw DecodingError.dataCorrupted(.init(codingPath: codingPath, debugDescription: "The key \(key) could not be converted to a numeric value: \(Array(self))"))
        }
        return value
    }
}
Run Code Online (Sandbox Code Playgroud)
extension KeyedEncodingContainer {
    mutating func encode(_ value: Decimal, forKey key: K) throws {
        try encode(value.data, forKey: key)
    }
    mutating func encodeIfPresent(_ value: Decimal?, forKey key: K) throws {
        guard let value = value else { return }
        try encode(value, forKey: key)
    }
}
Run Code Online (Sandbox Code Playgroud)
extension KeyedDecodingContainer {
    func decode(_ type: Decimal.Type, forKey key: K) throws -> Decimal {
        try decode(Data.self, forKey: key).decode(codingPath, key: key)
    }
    func decodeIfPresent(_ type: Decimal.Type, forKey key: K) throws -> Decimal? {
        try decodeIfPresent(Data.self, forKey: key)?.decode(codingPath, key: key)
    }
}
Run Code Online (Sandbox Code Playgroud)

游乐场测试:

struct Root: Codable {
    let decimal: Decimal
}

// using the string initializer for decimal is required to maintain precision
let root = Root(decimal: Decimal(string: "0.007")!)

do {
    let encoder = JSONEncoder()
    encoder.dataEncodingStrategy = .deferredToData
    let rootData = try encoder.encode(root)
    let decoder = JSONDecoder()
    decoder.dataDecodingStrategy = .deferredToData
    let root = try decoder.decode(Root.self, from: rootData)
    print(root.decimal) // prints "0.007\n" instead of "0.007000000000000001024\n" without the custom encoding and decoding methods
} catch {
    print(error)
}
Run Code Online (Sandbox Code Playgroud)

为了使数据大小尽可能小,您可以将 Decimal 编码和解码为字符串:

extension String {
    func decimal(_ codingPath: [CodingKey], key: CodingKey) throws -> Decimal {
        guard let decimal = Decimal(string: self) else {
            throw DecodingError.dataCorrupted(.init(codingPath: codingPath, debugDescription: "The key \(key) could not be converted to decimal: \(self)"))
        }
        return decimal
    }

}
Run Code Online (Sandbox Code Playgroud)
extension KeyedEncodingContainer {
    mutating func encode(_ value: Decimal, forKey key: K) throws {
        try encode(String(describing: value), forKey: key)
    }
    mutating func encodeIfPresent(_ value: Decimal?, forKey key: K) throws {
        guard let value = value else { return }
        try encode(value, forKey: key)
    }
}
Run Code Online (Sandbox Code Playgroud)
extension KeyedDecodingContainer {
    func decode(_ type: Decimal.Type, forKey key: K) throws -> Decimal {
        try decode(String.self, forKey: key).decimal(codingPath, key: key)
    }
    func decodeIfPresent(_ type: Decimal.Type, forKey key: K) throws -> Decimal? {
        try decodeIfPresent(String.self, forKey: key)?.decimal(codingPath, key: key)
    }
}
Run Code Online (Sandbox Code Playgroud)

游乐场测试:

struct StringDecimal: Codable {
    let decimal: Decimal
}

let root = StringDecimal(decimal: Decimal(string: "0.007")!)
do {
    let stringDecimalData = try JSONEncoder().encode(root)
    print(String(data: stringDecimalData, encoding: .utf8)!)
    let stringDecimal = try JSONDecoder().decode(StringDecimal.self, from: stringDecimalData)
    print(stringDecimal.decimal) // "0.007\n"
} catch {
    print(error)
}
Run Code Online (Sandbox Code Playgroud)

这将打印

{“十进制”:“0.007”}
0.007

  • 这很好地解决了这个问题,谢谢 Leo!想知道是否值得为此针对 Swift stdlib 提出 PR。 (4认同)