Skip to content

Commit

Permalink
encode 的options实现
Browse files Browse the repository at this point in the history
  • Loading branch information
Mccc committed Sep 27, 2024
1 parent 1d21d70 commit 56cccc3
Show file tree
Hide file tree
Showing 14 changed files with 45 additions and 282 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.11-beta.2)
- SmartCodable (4.1.11-beta.3)
- SnapKit (5.6.0)

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

221 changes: 0 additions & 221 deletions Example/SmartCodable/TestViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,228 +30,7 @@ class TestViewController: BaseViewController {
override func viewDidLoad() {
super.viewDidLoad()

SmartConfig.debugMode = .none



let json = """
{
"baseAppDrainage": "",
"adChannel": "0",
"versionInfo": {
"version": "1.0.0",
"build": 1,
"createTime": "2024-07-02 18:59:28",
"id": 1257772650835476480,
"packageName": "com.elevrin.app"
},
"configDefault": {
"IPFiltration": [
"CHINA",
"CHINESE",
"CINEMA",
"CN",
"COMIC",
"CONSTANTIN",
"DISNEY",
"DREAMWORK",
"ENTERTAINMENT",
"EUROPACORP",
"FILM",
"FOCUS",
"GAUMONT",
"GOOGLE",
"HEIDI",
"HOLLYWOOD",
"HULU",
"IMAGE",
"IQIYI",
"LARICK",
"LEGENDARY",
"LIONSGATE",
"MARVEL",
"MEDIA",
"MIRAMAX",
"NETFLIX",
"NEW LINE",
"PARAMOUNT",
"PEACOCK",
"PHILO",
"PICTURE",
"PIXAR",
"SONY",
"STARLIGHT",
"STARZ",
"STUDIO",
"STX",
"TENCENT",
"TOUCHSTONE",
"TUBI",
"TV",
"UNIVERSAL",
"CHINANET",
"VIKI",
"ZEUS",
"VUDU",
"WARNER",
"WEINSTEIN"
]
},
"eventConfig": {
"returnDetail": [],
"eventType": "",
"returnDetailAd": "",
"adType": []
},
"messagePush": "",
"com.elevrin.app": [],
"Home_Data": [
{
"data": [
{
"vodeo": 28407,
"type": "movie"
},
{
"vodeo": 28493,
"type": "movie"
},
{
"vodeo": 24179,
"type": "movie"
},
{
"vodeo": 28367,
"type": "movie"
},
{
"vodeo": 20791,
"type": "movie"
}
],
"level": 0,
"mode": 0,
"name": "Home Carousel"
},
{
"data": [
{
"vodeo": 20791,
"type": "movie"
},
{
"vodeo": 28303,
"type": "movie"
},
{
"vodeo": 28211,
"type": "movie"
},
{
"vodeo": 28236,
"type": "movie"
},
{
"vodeo": 28220,
"type": "movie"
}
],
"level": 1,
"mode": 0,
"name": "Trending",
"id": 1
},
{
"data": [
{
"vodeo": 14064,
"type": "tv"
},
{
"vodeo": 14053,
"type": "tv"
},
{
"vodeo": 14056,
"type": "tv"
},
{
"vodeo": 14043,
"type": "tv"
},
{
"vodeo": 14052,
"type": "tv"
}
],
"level": 2,
"mode": 0,
"name": "Popular TV Shows",
"id": 2
},
{
"data": [
{
"vodeo": 27915,
"type": "movie"
},
{
"vodeo": 25650,
"type": "movie"
},
{
"vodeo": 28380,
"type": "movie"
},
{
"vodeo": 28383,
"type": "movie"
},
{
"vodeo": 28381,
"type": "movie"
}
],
"level": 3,
"mode": 0,
"name": "Popular Movies",
"id": 3
}
],
"playSubTypeRatio": [],
"privacyLink": "https://catfight.top"
}
"""


if let model = NNInfos.deserialize(from: json) {
smartPrint(value: model)
}

}
struct NNInfos: SmartCodable {
var version: String = ""
var build: Int = -1
var ipfilters: [String] = []
var privacyLink: String = ""

static func mappingForKey() -> [SmartKeyTransformer]? {
let versionKey = "versionInfo".fixValue + "." + "version".fixValue
let buildKey = "versionInfo".fixValue + "." + "build".fixValue
let ipfilesKey = "configDefault".fixValue + "." + "IPFiltration".fixValue
return [
CodingKeys.version <--- versionKey,
CodingKeys.build <--- buildKey,
CodingKeys.ipfilters <--- ipfilesKey,
CodingKeys.privacyLink <--- "privacyLink".fixValue
]
}
}
}
extension String {
var fixValue: String {
return self
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,8 @@ class SpecialData_dataViewController: BaseCompatibilityViewController {
test()
}

func getStrategy() -> JSONDecoder.DataDecodingStrategy {
// let strategy: JSONDecoder.DataDecodingStrategy = .base64

let strategy: JSONDecoder.DataDecodingStrategy = .custom({ decoder -> Data in
let container = try decoder.singleValueContainer()
let string = try container.decode(String.self)
guard let data = string.data(using: .utf8) else {
throw DecodingError.dataCorruptedError(in: container, debugDescription: "无法转换为Data类型")
}
return data
})
return strategy
func getStrategy() -> JSONDecoder.SmartDataDecodingStrategy {
return .base64
}

func test() {
Expand All @@ -38,7 +28,11 @@ class SpecialData_dataViewController: BaseCompatibilityViewController {
if let model = DataModel.deserialize(from: dict, options: [.data(strategy)]) {
print(model)
print(model.aData.toString() ?? "")

print(model.toDictionary())
}


}
}

Expand All @@ -58,7 +52,7 @@ extension SpecialData_dataViewController {

func getDictData(
mode: Mode,
strategy: JSONDecoder.DataDecodingStrategy) -> [String: Any] {
strategy: JSONDecoder.SmartDataDecodingStrategy) -> [String: Any] {
switch mode {
case .keyless:
return [:]
Expand All @@ -80,22 +74,13 @@ extension SpecialData_dataViewController {


switch strategy {
// 由于 JSON 标准本身不支持直接的二进制数据表示,我们通常不会在实践中看到 deferredToData 的直接应用。
case .deferredToData:
return [
:
]


case .base64:
return [
"aData": "aHR0cHM6Ly93d3cucWl4aW4uY29t",
"bData": "aHR0cHM6Ly93d3cucWl4aW4uY29t",
]
case .custom(_):
return [
"aData": "Hello, world!",
"bData": "Hello, world!",
]
}

}
Expand Down
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.1.11-beta.2'
s.version = '4.1.11-beta.4'
s.summary = '数据解析库'

s.homepage = 'https://github.com/intsig171'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,9 +196,6 @@ extension JSONDecoderImpl {
}

switch self.options.dataDecodingStrategy {
case .deferredToData:
return try Data(from: self)

case .base64:
let container = SingleValueContainer(impl: self, codingPath: self.codingPath, json: self.json)
let string = try container.decode(String.self)
Expand All @@ -208,11 +205,6 @@ extension JSONDecoderImpl {
}

return data

case .custom(let closure):
return try closure(self)
@unknown default:
throw DecodingError.dataCorrupted(DecodingError.Context(codingPath: self.codingPath, debugDescription: "Encountered Data is not valid , unknown anomaly"))
}
}

Expand Down
Loading

0 comments on commit 56cccc3

Please sign in to comment.