如何在同一结构中对多种日期格式进行 JSON 编码



需要编码成JSON,一个具有2个Date实例变量(日期和时间)的结构,但是,我需要用不同的格式对每个日期实例变量进行编码,即。 对于"天":"yyyy-M-d"和"时间":"H:m:s"。

编写了一个自定义解码器,可以正常工作。但不确定如何编写所需的自定义编码器来解决此问题。

例如,我可以解码以下 JSON 字符串:{ "生物识别" : [ {"biometricId":1,"amount":2.1,"source":"Alderaan","day":"2019-1-3","time":"11-3-3","unitId":2}, {"biometricId":10,"amount":3.1,"source":"Endoor","day":"2019-2-4","time":"11-4-4","unitId":20}]}

但是,当我对其进行编码时,我只能以单个日期格式对其进行编码:(帮助,将不胜感激。谢谢。

import UIKit
let biometricsJson = """
{ "biometrics" : [
    {"biometricId":1,"amount":2.1,"source":"Alderaan","day":"2019-1-3","time":"11-3-3","unitId":2},
    {"biometricId":10,"amount":3.1,"source":"Endoor","day":"2019-2-4","time":"11-4-4","unitId":20}]
}
"""
struct Biometrics: Codable {
    var biometrics: [Biometric]
}
struct Biometric: Codable {
    var biometricId: Int
    var unitId: Int
    var source: String?
    var amount: Double
    var day: Date
    var time: Date
    init(biometricId: Int, unitId: Int, source: String, amount: Double, day: Date, time: Date){
        self.biometricId = biometricId
        self.unitId = unitId
        self.source = source
        self.amount = amount
        self.day = day
        self.time = time
    }
}
extension Biometric {
    static let decoder: JSONDecoder = {
        let decoder = JSONDecoder()
        decoder.dateDecodingStrategy = .custom { decoder in
            let container = try decoder.singleValueContainer()
            let dateString = try container.decode(String.self)
            let formatter = DateFormatter()
            formatter.timeZone = TimeZone.current
            formatter.dateFormat = "H:m:s"
            if let date = formatter.date(from: dateString) {
                return date
            }
            formatter.dateFormat = "yyyy-M-d"
            if let date = formatter.date(from: dateString) {
                return date
            }
            throw DecodingError.dataCorruptedError(in: container,
                                                   debugDescription: "Cannot decode date string (dateString)")
        }
        return decoder
    }()
}
let biometrics = try Biometric.decoder.decode(Biometrics.self, from:biometricsJson.data(using: .utf8)!)
let jsonEncoder = JSONEncoder()
let encodedJson = try jsonEncoder.encode(biometrics)
let jsonString = String(data: encodedJson, encoding: .utf8)
if biometricsJson != jsonString {
    print("error: decoding, then encoding does not give the same string")
    print("biometricsJson: (biometricsJson)")
    print("jsonString: (jsonString!)")
}

我希望编码的 JSON 可由解码器解码。即生物识别Json == jsonString

在自定义encode(to:)中,只需使用所需的格式化程序将每个编码为字符串即可。JSON 中没有"日期"类型;它只是一个字符串。大致如下:

enum CodingKeys: CodingKey {
    case biometricId, amount, source, day, time, unitId
}
func encode(to encoder: Encoder) throws {
    var container = encoder.container(keyedBy: CodingKeys.self)
    try container.encode(biometricId, forKey: .biometricId)
    try container.encode(unitId, forKey: .unitId)
    try container.encode(source, forKey: .source)
    try container.encode(amount, forKey: .amount)
    let formatter = DateFormatter()
    formatter.timeZone = TimeZone.current
    formatter.dateFormat = "H:m:s"
    let timeString = formatter.string(from: time)
    try container.encode(timeString, forKey: .time)
    formatter.dateFormat = "yyyy-M-d"
    let dayString = formatter.string(from: day)
    try container.encode(dayString, forKey: .day)
}

但请注意,您无法测试等效的字符串。JSON 词典不是顺序保存的,因此无法保证逐个字符匹配。

请注意,如果您真的想要日期和时间,您应该考虑DateComponents而不是Date。日期是时间中的特定实例;它不在任何时区,也不能只是一小时、一分钟和一秒。

此外,您使用 Double 会导致舍入差异。因此,2.1 将被编码为 2.1000000000000001。如果这是一个问题,您应该将Decimal用于amount而不是Double

相关内容

  • 没有找到相关文章

最新更新