Skip to content

Commit

Permalink
新增plat
Browse files Browse the repository at this point in the history
  • Loading branch information
intsig171 committed Jun 21, 2024
1 parent 9ea0c60 commit 3c822c1
Show file tree
Hide file tree
Showing 10 changed files with 122 additions and 54 deletions.
34 changes: 34 additions & 0 deletions Document/README/Usages.md
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,40 @@ class Model: SmartDecodable {



### extend(继承)

Inheritance does not have a particularly good implementation in Codable, but SmartCodable provides a combination (@SmartFlat) that indirectly implements inheritance requirements.

继承在Codable中没有特别好的实现方案, SmartCodable提供了组合方式(@SmartFlat)用来间接实现继承的需求。

```
let jsonString = """
{
"a": "aa",
"b": 100,
"longitude": 3,
"latitude": 4
}
"""
if let model = SubModel.deserialize(from: jsonString) {
smartPrint(value: model.c)
}
struct SuperModel: SmartCodable {
var longitude: Double?
var latitude: Double?
}
struct SubModel: SmartCodable {
var a: String?
var b: Int?
@SmartFlat
var c: SuperModel?
}
```



### Update Existing Model(更新现有模型)

```
Expand Down
49 changes: 42 additions & 7 deletions Document/Suggest/suggest4.md
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ struct SmartModel: SmartCodable {
// case ignoreKey
}
static func mapping() -> [MappingRelationship]? {
static func mappingForKey() -> [SmartKeyTransformer]? {
[
CodingKeys.name <--- ["nick_name", "realName"],
CodingKeys.age <--- "self_age"
Expand Down Expand Up @@ -261,13 +261,16 @@ print(handyModel.dict)

```
struct SmartModel: SmartCodable {
var name: SmartAny?
var dict: [String: SmartAny] = [:]
@SmartAny
var name: Any?
@SmartAny
var dict: [String: Any] = [:]
}
guard let smartModel = SmartModel.deserialize(from: dict) else { return }
print(smartModel.name?.peel)
print(smartModel.dict.peel)
print(smartModel.name)
print(smartModel.dict)
```


Expand All @@ -293,7 +296,7 @@ class HandyModel: HandyBaseModel {

### SmartCodable

需要子类重写 `required init(from decoder: Decoder) throws` 方法。
方案1: 需要子类重写 `required init(from decoder: Decoder) throws` 方法。

```
class SmartBaseModel: SmartCodable {
Expand Down Expand Up @@ -322,7 +325,7 @@ class SmartModel: SmartBaseModel {



### 使用协议替代继承
方案2: 使用协议替代继承

继承代表着Model必须是 **class** 。这并不可取。完全可以使用更轻量化的 **struct**

Expand All @@ -348,6 +351,38 @@ extension ReplaceHandyJSON_5ViewController {



方案3: 使用组合代替继承

继承在Codable中没有特别好的实现方案, SmartCodable提供了组合方式(@SmartFlat)用来间接实现继承的需求。

```
let jsonString = """
{
"a": "aa",
"b": 100,
"longitude": 3,
"latitude": 4
}
"""
if let model = SubModel.deserialize(from: jsonString) {
smartPrint(value: model.superModel)
}
struct SuperModel: SmartCodable {
var longitude: Double?
var latitude: Double?
}
struct SubModel: SmartCodable {
var a: String?
var b: Int?
@SmartFlat
var superModel: SuperModel?
}
```



## 八. 枚举的解析

需要做一定的兼容。
Expand Down
4 changes: 2 additions & 2 deletions Example/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ PODS:
- FBSnapshotTestCase/SwiftSupport (2.1.4):
- FBSnapshotTestCase/Core
- HandyJSON (5.0.0-beta.1)
- SmartCodable (4.0.3.1)
- SmartCodable (4.0.4)
- SnapKit (5.6.0)

DEPENDENCIES:
Expand Down Expand Up @@ -39,7 +39,7 @@ SPEC CHECKSUMS:
CleanJSON: 910a36465ce4829e264a902ccf6d1455fdd9f980
FBSnapshotTestCase: 094f9f314decbabe373b87cc339bea235a63e07a
HandyJSON: 582477127ab3ab65bd2e471815f1a7b846856978
SmartCodable: d46b8eb3cff493e282840a955031c414ce50c039
SmartCodable: 91f469249cf9cd8c1ef4777bffe902e167293846
SnapKit: e01d52ebb8ddbc333eefe2132acf85c8227d9c25

PODFILE CHECKSUM: 7f3af03f81934df0c035518074a7abbec8fa9d3f
Expand Down
4 changes: 2 additions & 2 deletions Example/Pods/Local Podspecs/SmartCodable.podspec.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Example/Pods/Manifest.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Example/SmartCodable/Test2ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class Test2ViewController: BaseViewController {
}
}
struct OptionalArrayElementsExample: SmartCodable {
// @SmartFlat
@SmartFlat
var array: [ArrayElement] = []
}

Expand Down
22 changes: 9 additions & 13 deletions Example/SmartCodable/Test3ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,31 +23,27 @@ class Test3ViewController: BaseViewController {
{
"a": "aa",
"b": 100,
"c": {
"longitude": 300,
"latitude": 400
},
"longitude": 3,
"longitude": "3",
"latitude": 4
}
"""
if let model = SubClass.deserialize(from: jsonString) {
if let model = SubModel.deserialize(from: jsonString) {
smartPrint(value: model.c)
}
}
struct SuperClass: SmartCodable {
struct SuperModel: SmartCodable {
var longitude: Double?
var latitude: Double?

var a: String?
var b: Int?
}
struct SubClass: SmartCodable {
struct SubModel: SmartCodable {
var a: String?
var b: Int?

@SmartFlat
var c: SuperClass?
var c: SuperModel?
}
}




53 changes: 28 additions & 25 deletions Example/SmartCodable/TestViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,37 +13,40 @@ import HandyJSON
import CleanJSON
import BTPrint

import SmartCodable


/** todo
1. 验证plat的准确性
2. 验证解析容器/容器模型的准确性。
*/


class TestViewController: BaseViewController {

override func viewDidLoad() {
super.viewDidLoad()

let dict: [String: Any] = [
"name": [
1, 2, 3
]
]

if let model = Model.deserialize(from: dict) {
print(model)
print("\n")
print(model.toDictionary())


let jsonString = """
{
"a": "aa",
"b": 100,
"c": {
"longitude": 300,
"latitude": 400
},
"longitude": 3,
"latitude": 4
}
"""
if let model = SubClass.deserialize(from: jsonString) {
smartPrint(value: model.c)
}
}

struct Model: SmartCodable {
var name: [String] = []
struct SuperClass: SmartCodable {
var longitude: Double?
var latitude: Double?

var a: String?
var b: Int?
}
struct SubClass: SmartCodable {
var a: String?
var b: Int?

@SmartFlat
var c: SuperClass?
}
}

2 changes: 1 addition & 1 deletion SmartCodable.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

Pod::Spec.new do |s|
s.name = 'SmartCodable'
s.version = '4.0.3.1'
s.version = '4.0.4'
s.summary = '数据解析库'

s.homepage = 'https://github.com/intsig171'
Expand Down

0 comments on commit 3c822c1

Please sign in to comment.