Skip to content

Commit

Permalink
优化浮点数转String类型精度问题
Browse files Browse the repository at this point in the history
  • Loading branch information
intsig171 committed Jul 5, 2024
1 parent ba3d6ab commit 7c20dd2
Show file tree
Hide file tree
Showing 16 changed files with 336 additions and 192 deletions.
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.1.0)
- SmartCodable (4.1.1)
- SnapKit (5.6.0)

DEPENDENCIES:
Expand Down Expand Up @@ -39,7 +39,7 @@ SPEC CHECKSUMS:
CleanJSON: 910a36465ce4829e264a902ccf6d1455fdd9f980
FBSnapshotTestCase: 094f9f314decbabe373b87cc339bea235a63e07a
HandyJSON: 582477127ab3ab65bd2e471815f1a7b846856978
SmartCodable: 04f61a30bd6567fc15b71e718e1aee4766956e55
SmartCodable: 092fd8c698dc958fc95e1d93baf3b497f9ab1715
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.

5 changes: 5 additions & 0 deletions Example/SmartCodable/Base/NSDictionary+Extension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ extension Dictionary {
return nil
}
}

func toData() -> Data? {
guard JSONSerialization.isValidJSONObject(self) else { return nil }
return try? JSONSerialization.data(withJSONObject: self)
}
}


Expand Down
80 changes: 19 additions & 61 deletions Example/SmartCodable/Test2ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,76 +18,34 @@ class Test2ViewController: BaseViewController {

override func viewDidLoad() {
super.viewDidLoad()


SmartConfig.debugMode = .verbose

let dict1: [String: Any] = [
:
]

let dict2: [String: Any] = [
"arr": NSNull(),
"optionalArr": NSNull(),
"arr1": NSNull(),
"optionalArr1": NSNull()
]

let dict3: [String: Any] = [
"arr": 1,
"optionalArr": 2,
"arr1": 3,
"optionalArr1": 4
]

let dict4: [String: Any] = [
"arr": [[ "name": "mccc1" ], [ "name": "mccc2" ]],
"optionalArr": [[ "name": "mccc1" ], [ "name": "mccc2" ]],
let dict: [String: Any] = [
"key": 4.99
]

let data = dict.toData()!

let decoder = JSONDecoder()

print("-------------- 无key")
if let model = FeedModel.deserialize(from: dict1) {
smartPrint(value: model)
}

print("-------------- null")
if let model = FeedModel.deserialize(from: dict2) {
smartPrint(value: model)
do {
let model = try decoder.decode(Model.self, from: data)
print(model)
} catch {
print(error)
}

print("-------------- 类型错误")
if let model = FeedModel.deserialize(from: dict3) {
smartPrint(value: model)
}

print("-------------- 解析正确的")
if let model = FeedModel.deserialize(from: dict4) {
smartPrint(value: model)
}

}
//模型
struct FeedModel: SmartCodable {
// var arr: [Int] = []
// var optionalArr: [Int]?
// var optionalArr: [Int]? = [1, 1, 1]

// var arr: [Int?] = []
// var optionalArr: [Int?]?
// var optionalArr: [Int?]? = [1, 1, 1]

// @SmartAny
// var arr: [Any] = []
// @SmartAny
// var optionalArr: [Any]?


var arr: [SubModel] = []
var optionalArr: [SubModel]?
}

struct SubModel: SmartCodable {
var name: String?
struct Model: Codable {
var key: String?

init(from decoder: any Decoder) throws {
let container: KeyedDecodingContainer<Test2ViewController.Model.CodingKeys> = try decoder.container(keyedBy: Test2ViewController.Model.CodingKeys.self)
let double = try container.decodeIfPresent(Double.self, forKey: Test2ViewController.Model.CodingKeys.key)

self.key = "\(String(describing: double))"
}
}
}
28 changes: 12 additions & 16 deletions Example/SmartCodable/Test3ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,30 +19,26 @@ class Test3ViewController: BaseViewController {
override func viewDidLoad() {
super.viewDidLoad()

SmartConfig.debugMode = .none

let dict: [String: Any] = [
"subModel": [
"name": NSNull()
],
"dict": ["a":1, "b": NSNull(), "c": ["aa": 2]],
"arr": [1, "2", "Mccc", [1,2,3]]
"key": 5.1,
"key1":5.2,
"key2": 1.99,
"key3": 4.99,
"key4": 99.99,
]
if let model = Model.deserialize(from: dict) {
smartPrint(value: model)
}
}

struct Model: SmartCodable {
@SmartAny
var dict: Any?

@SmartAny
var arr: Any?

// var subModel: SubModel?
}

struct SubModel: SmartCodable {
var name: String?
var key: String = ""
var key1: String = ""
var key2: String = ""
var key3: String = ""
var key4: String = ""
}
}

Expand Down
Loading

0 comments on commit 7c20dd2

Please sign in to comment.