Skip to content

Commit dacb7f7

Browse files
committed
SmartAny中支持是否使用null值
1 parent 3b9084c commit dacb7f7

3 files changed

Lines changed: 24 additions & 17 deletions

File tree

Example/SmartCodable/TestViewController.swift

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -36,27 +36,20 @@ class TestViewController: BaseViewController {
3636

3737

3838
let dict: [String: Any] = [
39-
"age": 0,
39+
"data": NSNull(),
4040
"name": "操作成功",
4141

4242
]
4343

44-
guard let model = SubModel.deserialize(from: dict) else { return }
44+
guard let model = Model.deserialize(from: dict) else { return }
4545
print(model)
4646
}
4747

48-
class Model: SmartCodable {
48+
struct Model: SmartCodable {
4949
var name: String = ""
5050

51-
required init() { }
52-
}
53-
54-
@SmartSubclass
55-
class SubModel: Model {
56-
@objc var age: String = ""
57-
5851
@SmartAny
59-
var location: String = ""
52+
var data: Any?
6053

6154
}
6255
}

Sources/SmartCodable/Core/PropertyWrapper/SmartAny/SmartAny.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,14 @@ extension SmartAny: Codable {
5555
)
5656
}
5757

58+
/// 是否考虑使用null值? 案例:属性类型是Any时,是否将null解析到Any类型中。
59+
if decoder.json == .null && SmartCodableOptions.ignoreNull {
60+
throw DecodingError.typeMismatch(Self.self, DecodingError.Context(
61+
codingPath: decoder.codingPath, debugDescription: "Expected \(Self.self) value,but an exception occurred!")
62+
)
63+
}
64+
65+
5866
if let decoded = try? decoder.unwrap(as: SmartAnyImpl.self), let peel = decoded.peel as? T {
5967
self = .init(wrappedValue: peel)
6068
} else {

Sources/SmartCodable/Core/SmartCodable/SmartCodableConfig.swift renamed to Sources/SmartCodable/Core/SmartCodable/SmartCodableOptions.swift

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,21 @@ public struct SmartCodableOptions {
1919
/// - .rounded: Returns 3 (rounds to nearest)
2020
///
2121
/// - Note: This only affects decoding process
22-
public static var numberStrategy: NumberConversionStrategy {
23-
get { _numberConversionStrategy }
24-
set {_numberConversionStrategy = newValue }
25-
}
22+
public static var numberStrategy: NumberConversionStrategy = .strict
23+
2624

25+
/// Whether to treat JSON `null` as a decoded value for `Any`-typed property wrappers (default: `true`)
26+
///
27+
/// 在对使用 `Any`(或 Any 支持的属性包装器)进行解码时,决定是否把 JSON 中的 `null` 当作可被解码并赋值到 `Any` 的值。
28+
///
29+
/// - Behavior:
30+
/// - 当为 `true`(默认)时:遇到 JSON 字段值为 `null`,属性包装器**不会**把 `NSNull`/`nil` 赋给目标 `Any`,而是跳过赋值(保持属性的默认值或原有值)。
31+
/// - 当为 `false` 时:遇到 JSON 字段值为 `null`,属性包装器会把 `NSNull()`(或解码为 `nil`,取决于你的实现)作为解析结果赋给 `Any`,从而能在运行时检测到该字段为 `null`。
32+
public static var ignoreNull: Bool = true
33+
}
2734

28-
private static var _numberConversionStrategy = NumberConversionStrategy.strict
2935

30-
36+
extension SmartCodableOptions {
3137
/// Numeric type conversion strategy
3238
public enum NumberConversionStrategy {
3339
/// Strict mode: Must match exactly, otherwise returns nil (default)

0 commit comments

Comments
 (0)