swift - 如何将@propertyWrapper 用于带有可选键的Decodable?
问题描述
我正在使用属性包装器将字符串“true”和“false”解码为布尔值。我还想让密钥成为可选的。因此,如果 JSON 中缺少密钥,则应将其解码为 nil。不幸的是,添加属性包装器会破坏这一点,Swift.DecodingError.keyNotFound
而是抛出 a 。
@propertyWrapper
struct SomeKindOfBool: Decodable {
var wrappedValue: Bool?
init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()
if let stringifiedValue = try? container.decode(String.self) {
switch stringifiedValue.lowercased() {
case "false": wrappedValue = false
case "true": wrappedValue = true
default: wrappedValue = nil
}
} else {
wrappedValue = try? container.decode(Bool.self)
}
}
}
public struct MyType: Decodable {
@SomeKindOfBool var someKey: Bool?
}
let jsonData = """
[
{ "someKey": true },
{ "someKey": "false" },
{}
]
""".data(using: .utf8)!
let decodedJSON = try! JSONDecoder().decode([MyType].self, from: jsonData)
for decodedType in decodedJSON {
print(decodedType.someKey ?? "nil")
}
知道如何解决这个问题吗?
解决方案
当类型为可选时,init(from:)
通常使用的合成代码。decodeIfPresent
但是,属性包装器始终是非可选的,并且只能使用可选作为其基础值。这就是为什么合成器总是使用法线decode
,如果键不存在则失败(Swift 论坛中的一篇很好的文章)。
我通过使用优秀的CodableWrappers 包解决了这个问题:
public struct NonConformingBoolStaticDecoder: StaticDecoder {
public static func decode(from decoder: Decoder) throws -> Bool {
if let stringValue = try? String(from: decoder) {
switch stringValue.lowercased() {
case "false", "no", "0": return false
case "true", "yes", "1": return true
default:
throw DecodingError.valueNotFound(self, DecodingError.Context(
codingPath: decoder.codingPath,
debugDescription: "Expected true/false, yes/no or 0/1 but found \(stringValue) instead"))
}
} else {
return try Bool(from: decoder)
}
}
}
typealias NonConformingBoolDecoding = DecodingUses<NonConformingBoolStaticDecoder>
然后我可以像这样定义我的可解码结构:
public struct MyType: Decodable {
@OptionalDecoding<NonConformingBoolDecoding> var someKey: Bool?
}
推荐阅读
- mapkit - 在 mapkit.js 中获取地图的边界
- c# - 迭代上下文类型的查询结果时发生异常。连接已关闭
- c++ - SFML 在随机固定路径中移动精灵
- c++ - 如何在不从活动窗口中窃取焦点的情况下保持表单始终位于顶部?
- python - tkinter python网格功能无法正常工作
- python - 为什么要在 Pytorch 中为自定义层定义后向方法?
- regex - 处理复杂的正则表达式
- python - django.template.exceptions.TemplateDoesNotExist:/hello/
- c# - AWS Lambda 在 C# 中与 CodeCommit 交互
- angular - 为什么我的 Auth-guard 在未完成可观察代码的情况下停止?