From 0b6f0a54d1ca74db19b20144868bee7c8743f83e Mon Sep 17 00:00:00 2001 From: Mccc <> Date: Sat, 12 Oct 2024 19:13:36 +0800 Subject: [PATCH] bugfix --- Example/Podfile.lock | 4 +- .../Local Podspecs/SmartCodable.podspec.json | 4 +- Example/Pods/Manifest.lock | 4 +- .../SmartCodable/SmartCodable-Info.plist | 2 +- .../SmartCodable/Test2ViewController.swift | 32 ++++++++++++-- Example/SmartCodable/TestViewController.swift | 33 +++++++++++++- SmartCodable.podspec | 2 +- .../Impl/JSONDecoderImpl+KeyedContainer.swift | 19 ++++---- .../JSONDecoder/Decoder/KeysMapper.swift | 2 +- .../Classes/Transformer/Transformer.swift | 43 +++++++++++++++++++ 10 files changed, 123 insertions(+), 22 deletions(-) diff --git a/Example/Podfile.lock b/Example/Podfile.lock index e5de221..118835d 100644 --- a/Example/Podfile.lock +++ b/Example/Podfile.lock @@ -8,7 +8,7 @@ PODS: - FBSnapshotTestCase/SwiftSupport (2.1.4): - FBSnapshotTestCase/Core - HandyJSON (5.0.0-beta.1) - - SmartCodable (4.2.2) + - SmartCodable (4.2.3) - SnapKit (5.6.0) DEPENDENCIES: @@ -39,7 +39,7 @@ SPEC CHECKSUMS: CleanJSON: 910a36465ce4829e264a902ccf6d1455fdd9f980 FBSnapshotTestCase: 094f9f314decbabe373b87cc339bea235a63e07a HandyJSON: 582477127ab3ab65bd2e471815f1a7b846856978 - SmartCodable: 4f52d801dbd15856fabb69110d24f31f49145734 + SmartCodable: f01c43e62a8867828fb9f51f4b354b657dafc37d SnapKit: e01d52ebb8ddbc333eefe2132acf85c8227d9c25 PODFILE CHECKSUM: 7f3af03f81934df0c035518074a7abbec8fa9d3f diff --git a/Example/Pods/Local Podspecs/SmartCodable.podspec.json b/Example/Pods/Local Podspecs/SmartCodable.podspec.json index 79211bd..43608c3 100644 --- a/Example/Pods/Local Podspecs/SmartCodable.podspec.json +++ b/Example/Pods/Local Podspecs/SmartCodable.podspec.json @@ -1,6 +1,6 @@ { "name": "SmartCodable", - "version": "4.2.2", + "version": "4.2.3", "summary": "数据解析库", "homepage": "https://github.com/intsig171", "license": { @@ -12,7 +12,7 @@ }, "source": { "git": "https://github.com/intsig171/SmartCodable.git", - "tag": "4.2.2" + "tag": "4.2.3" }, "platforms": { "ios": "11.0", diff --git a/Example/Pods/Manifest.lock b/Example/Pods/Manifest.lock index e5de221..118835d 100644 --- a/Example/Pods/Manifest.lock +++ b/Example/Pods/Manifest.lock @@ -8,7 +8,7 @@ PODS: - FBSnapshotTestCase/SwiftSupport (2.1.4): - FBSnapshotTestCase/Core - HandyJSON (5.0.0-beta.1) - - SmartCodable (4.2.2) + - SmartCodable (4.2.3) - SnapKit (5.6.0) DEPENDENCIES: @@ -39,7 +39,7 @@ SPEC CHECKSUMS: CleanJSON: 910a36465ce4829e264a902ccf6d1455fdd9f980 FBSnapshotTestCase: 094f9f314decbabe373b87cc339bea235a63e07a HandyJSON: 582477127ab3ab65bd2e471815f1a7b846856978 - SmartCodable: 4f52d801dbd15856fabb69110d24f31f49145734 + SmartCodable: f01c43e62a8867828fb9f51f4b354b657dafc37d SnapKit: e01d52ebb8ddbc333eefe2132acf85c8227d9c25 PODFILE CHECKSUM: 7f3af03f81934df0c035518074a7abbec8fa9d3f diff --git a/Example/Pods/Target Support Files/SmartCodable/SmartCodable-Info.plist b/Example/Pods/Target Support Files/SmartCodable/SmartCodable-Info.plist index 370c557..efb5db3 100644 --- a/Example/Pods/Target Support Files/SmartCodable/SmartCodable-Info.plist +++ b/Example/Pods/Target Support Files/SmartCodable/SmartCodable-Info.plist @@ -15,7 +15,7 @@ CFBundlePackageType FMWK CFBundleShortVersionString - 4.2.2 + 4.2.3 CFBundleSignature ???? CFBundleVersion diff --git a/Example/SmartCodable/Test2ViewController.swift b/Example/SmartCodable/Test2ViewController.swift index e8fcac0..e32d479 100644 --- a/Example/SmartCodable/Test2ViewController.swift +++ b/Example/SmartCodable/Test2ViewController.swift @@ -16,17 +16,43 @@ class Test2ViewController: BaseViewController { override func viewDidLoad() { super.viewDidLoad() - let dict = [ - "name": 1.22222 + let dict: [String: Any] = [ + "name": "mccc", + "subModel": "mccc111", + ] let model = Model.deserialize(from: dict) - print(model) + print(model?.subModel?.rawValue) + + let dict1 = model?.toDictionary() + print(dict1) } struct Model: SmartCodable { var name: String = "" + @SmartPublished + var subModel: TestEnum? + static func mappingForValue() -> [SmartValueTransformer]? { + [ + CodingKeys.name <--- FastTransformer(fromJSON: { json in + "abc" + }), + CodingKeys.subModel <--- FastTransformer(fromJSON: { json in + TestEnum.man + }), + ] + } + } + + enum TestEnum: String, SmartCaseDefaultable { + case man + } + + struct SubModel: SmartCodable { + var name: String = "" + } } diff --git a/Example/SmartCodable/TestViewController.swift b/Example/SmartCodable/TestViewController.swift index 0d67a2a..9b49447 100644 --- a/Example/SmartCodable/TestViewController.swift +++ b/Example/SmartCodable/TestViewController.swift @@ -14,7 +14,6 @@ import CleanJSON import BTPrint - /** 字典的值情况 1. @Published 修饰的属性的解析。 2. 继承关系!!!! @@ -35,6 +34,38 @@ class TestViewController: BaseViewController { override func viewDidLoad() { super.viewDidLoad() + let jsonString = """ +{ + + "enjoyCount": 462, + "talkCount": null, +} + +""" + + guard let model = RecommendModel.deserialize(from: jsonString) else { + return + } + + print(model) + } + + + struct RecommendModel: SmartCodable { + + /// 点赞数 + var enjoyCount: Int = 0 + /// 评论数 + var commentCount: Int = 0 + + + static func mappingForKey() -> [SmartKeyTransformer]? { + [ + CodingKeys.commentCount <--- ["commentCount","talkCount","postCommentCount","topicCommentCount","topicTalkCount", "articleCommentCount"], + CodingKeys.enjoyCount <--- ["articleEnjoyCount","enjoyCount","topicEnjoyCount","postEnjoyCount"], + ] + } } + } diff --git a/SmartCodable.podspec b/SmartCodable.podspec index 8ce0dd1..44e9ba5 100644 --- a/SmartCodable.podspec +++ b/SmartCodable.podspec @@ -12,7 +12,7 @@ Pod::Spec.new do |s| s.name = 'SmartCodable' - s.version = '4.2.2' + s.version = '4.2.3' s.summary = '数据解析库' s.homepage = 'https://github.com/intsig171' diff --git a/SmartCodable/Classes/JSONDecoder/Decoder/Impl/JSONDecoderImpl+KeyedContainer.swift b/SmartCodable/Classes/JSONDecoder/Decoder/Impl/JSONDecoderImpl+KeyedContainer.swift index a41ed25..080dadd 100644 --- a/SmartCodable/Classes/JSONDecoder/Decoder/Impl/JSONDecoderImpl+KeyedContainer.swift +++ b/SmartCodable/Classes/JSONDecoder/Decoder/Impl/JSONDecoderImpl+KeyedContainer.swift @@ -290,15 +290,16 @@ extension JSONDecoderImpl.KeyedContainer { // 如果值可以被成功获取 if let value = try? getValue(forKey: key) { - if let decoded = impl.cache.tranform(value: value, for: key) { - - // 检查 SmartPublished 包装器类型 - if let publishedType = T.self as? any SmartPublishedProtocol.Type, - let publishedValue = publishedType.createInstance(with: decoded) as? T { - return publishedValue - } - } - } + if let decoded = impl.cache.tranform(value: value, for: key) { + if let tTypeValue = decoded as? T { + return tTypeValue + } else if let publishedType = T.self as? any SmartPublishedProtocol.Type, + let publishedValue = publishedType.createInstance(with: decoded) as? T { + // // 检查 SmartPublished 包装器类型 + return publishedValue + } + } + } diff --git a/SmartCodable/Classes/JSONDecoder/Decoder/KeysMapper.swift b/SmartCodable/Classes/JSONDecoder/Decoder/KeysMapper.swift index 98c5138..af59319 100644 --- a/SmartCodable/Classes/JSONDecoder/Decoder/KeysMapper.swift +++ b/SmartCodable/Classes/JSONDecoder/Decoder/KeysMapper.swift @@ -44,7 +44,7 @@ struct KeysMapper { type.mappingForKey()?.forEach { mapping in for oldKey in mapping.from { let newKey = mapping.to.stringValue - if let value = newDict[oldKey], !(value is NSNull) { + if let value = newDict[oldKey] as? JSONValue, value != .null { newDict[newKey] = newDict[oldKey] break } else { // Handles the case of a custom parsing path. diff --git a/SmartCodable/Classes/Transformer/Transformer.swift b/SmartCodable/Classes/Transformer/Transformer.swift index 8d8f91c..eb17eae 100644 --- a/SmartCodable/Classes/Transformer/Transformer.swift +++ b/SmartCodable/Classes/Transformer/Transformer.swift @@ -53,3 +53,46 @@ public protocol ValueTransformable { public func <---(location: CodingKey, tranformer: any ValueTransformable) -> SmartValueTransformer { SmartValueTransformer.init(location: location, tranformer: tranformer) } + + + +/** 便捷的Transformer + static func mappingForValue() -> [SmartValueTransformer]? { + [ + CodingKeys.name <--- FastTransformer(fromJSON: { json in + "abc" + }, toJSON: { object in + "123" + }), + CodingKeys.subModel <--- FastTransformer(fromJSON: { json in + TestEnum.man + }, toJSON: { object in + object?.rawValue + }), + ] + } + */ + +public struct FastTransformer: ValueTransformable { + + private let fromJSON: (JSON?) -> Object? + private let toJSON: ((Object?) -> JSON?)? + + + /// 便捷的转换器 + /// - Parameters: + /// - fromJSON: json 转 object + /// - toJSON: object 转 json, 如果需要转json,可以不实现。 + public init(fromJSON: @escaping (JSON?) -> Object?, toJSON: ((Object?) -> JSON?)? = nil) { + self.fromJSON = fromJSON + self.toJSON = toJSON + } + + public func transformFromJSON(_ value: Any) -> Object? { + return fromJSON(value as? JSON) + } + + public func transformToJSON(_ value: Object) -> JSON? { + return toJSON?(value) + } +}